Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EnsureExplicitNullableTypes #1716

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Rule/EnsureExplicitNullableTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ public function check(Lines $lines, int $number, string $filename): ViolationInt
$lines->next();

while ($lines->valid() && !$lines->current()->isDirective()) {
++$number;

if (!str_contains((string) $lines->current()->clean(), ' = null')) {
$lines->next();

continue;
}

$pattern = '/([?]?\w+)\s+\$(\w+)\s*=\s*null(?=\s*[,\)])/';
$pattern = '#((?:\??\\\\?\w+[|]?)+)\s+\$(\w+)\s*=\s*null(?=\s*[,\)])#siu';

if ($matches = $lines->current()->clean()->match($pattern, \PREG_SET_ORDER)) {
foreach ($matches as $match) {
Expand All @@ -65,6 +67,11 @@ public function check(Lines $lines, int $number, string $filename): ViolationInt
continue;
}

// mixed $id = null
if ('mixed' === $types) {
continue;
}

// int|string|null $id = null
$types = explode('|', $types);

Expand Down
19 changes: 16 additions & 3 deletions tests/Rule/EnsureExplicitNullableTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,21 @@ public static function validProvider(): iterable
$validCases = [
'function foo(int $bar = 23)',
'function foo(?int $bar = null)',
'function foo(?\\Throwable $exception = null)',
'function foo(\\Throwable|null $exception = null)',
'function foo(?Foo\\Bar $bar = null)',
'function foo(mixed $bar = null)',
'function foo(int|null $bar = null)',
'function foo(int|string|null $bar = null)',
];

foreach ($validCases as $validCase) {
yield $validCase => [
NullViolation::create(),
new RstSample(['.. code-block:: php', $validCase]),
new RstSample([
'.. code-block:: php',
' '.$validCase,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for clarification, this indention is needed, otherwise the code does not belong to the code block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok ! Thanks for the explanation :)

]),
];
}
}
Expand All @@ -63,6 +70,9 @@ public static function invalidProvider(): iterable
{
$invalidCases = [
'public function foo(int $bar = null)',
'public function foo(Throwable $bar = null)',
'public function foo(Foo\\Bar $bar = null)',
'public function foo(\\Throwable $bar = null)',
'public function foo(int|string $bar = null)',
'function foo(int $foo, int $bar = null)',
'function foo(int $foo, int $bar = null, int $baz)',
Expand All @@ -76,10 +86,13 @@ public static function invalidProvider(): iterable
Violation::from(
'Please use explicit nullable types.',
'filename',
1,
2,
$invalidCase,
),
new RstSample(['.. code-block:: php', $invalidCase]),
new RstSample([
'.. code-block:: php',
' '.$invalidCase,
]),
];
}
}
Expand Down
Loading