Skip to content

Commit 051fa78

Browse files
committed
Whitelist PHP methods for no return or arg type
See #11
1 parent 24b5698 commit 051fa78

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# Changelog
22

3+
## Not released
4+
- Whitelist PHP core methods for not having return or argument type declaration.
5+
36
## 0.12.0
47
- Fix bug in `Psr4Sniff` when class has not namespace.
5-
- Allow @wp-hook ignore of return type for private and protected methods
6-
- Only check public accessors (by default)
7-
- Fix bug in checking return type
8-
- Allow filters callbacks to return `null` on purpose
8+
- Allow @wp-hook ignore of return type for private and protected methods.
9+
- Only check public accessors (by default).
10+
- Fix bug in checking return type.
11+
- Allow filters callbacks to return `null` on purpose.
912

1013
## 0.11.0
1114
- Fix false positive in `ReturnTypeDeclarationSniff` with nullable types.
@@ -15,7 +18,7 @@
1518
- Added `VariablesNameSniff`.
1619
- Improved `PhpcsHelpers::variableIsProperty()`.
1720
- Improved failure handling in FixturesTest.
18-
- Use NeutronStandard by opting-in rules instead of opting-out
21+
- Use NeutronStandard by opting-in rules instead of opting-out.
1922
- Properly handle Generators and return types.
2023

2124
## 0.10.0

Inpsyde/Sniffs/CodeQuality/ArgumentTypeDeclarationSniff.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class ArgumentTypeDeclarationSniff implements Sniff
2828
T_SELF,
2929
];
3030

31+
const METHODS_WHITELIST = [
32+
'unserialize',
33+
'seek',
34+
];
35+
3136
/**
3237
* @inheritdoc
3338
*/
@@ -44,6 +49,10 @@ public function process(File $file, $position)
4449
if (PhpcsHelpers::functionIsArrayAccess($file, $position)
4550
|| PhpcsHelpers::isHookClosure($file, $position)
4651
|| PhpcsHelpers::isHookFunction($file, $position)
52+
|| (
53+
PhpcsHelpers::functionIsMethod($file, $position)
54+
&& in_array($file->getDeclarationName($position), self::METHODS_WHITELIST, true)
55+
)
4756
) {
4857
return;
4958
}

Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ class ReturnTypeDeclarationSniff implements Sniff
2929
T_SELF,
3030
];
3131

32+
const METHODS_WHITELIST = [
33+
'__to_string',
34+
'serialize',
35+
'jsonSerialize',
36+
'getIterator',
37+
'getInnerIterator',
38+
'getChildren',
39+
'current',
40+
'key',
41+
'valid',
42+
'count',
43+
];
44+
3245
/**
3346
* @inheritdoc
3447
*/
@@ -144,6 +157,12 @@ private function maybeErrors(
144157
return;
145158
}
146159

160+
if (PhpcsHelpers::functionIsMethod($file, $position)
161+
&& in_array($file->getDeclarationName($position), self::METHODS_WHITELIST, true)
162+
) {
163+
return;
164+
}
165+
147166
if ($hasNoReturnType) {
148167
$file->addWarning('Return type is missing', $position, 'NoReturnType');
149168
}

tests/fixtures/argument-type-declaration.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,15 @@ public function offsetUnset($offset)
243243
{
244244
}
245245
}
246+
247+
class SerializeTest implements \Serializable {
248+
249+
public function serialize()
250+
{
251+
return '';
252+
}
253+
254+
public function unserialize($serialized)
255+
{
256+
}
257+
}

tests/fixtures/return-type-declaration.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,20 @@ public function iReturnWrongNull(): \ArrayAccess
409409
return new \ArrayObject();
410410
}
411411
}
412+
413+
class JsonSerializeTest implements \JsonSerializable, \Serializable {
414+
415+
public function jsonSerialize()
416+
{
417+
return '';
418+
}
419+
420+
public function serialize()
421+
{
422+
return '';
423+
}
424+
425+
public function unserialize($serialized)
426+
{
427+
}
428+
}

0 commit comments

Comments
 (0)