-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement Closures\NoParameterWithNullDefaultValueRule
- Loading branch information
1 parent
244a35f
commit c1a0f06
Showing
13 changed files
with
212 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller. | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/phpstan-rules | ||
*/ | ||
|
||
namespace Localheinz\PHPStan\Rules\Closures; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
|
||
final class NoParameterWithNullDefaultValueRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\Closure::class; | ||
} | ||
|
||
/** | ||
* @param Node\Stmt\Function_ $node | ||
* @param Scope $scope | ||
* | ||
* @return array | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (0 === \count($node->params)) { | ||
return []; | ||
} | ||
|
||
$params = \array_filter($node->params, static function (Node\Param $node): bool { | ||
if (!$node->default instanceof Node\Expr\ConstFetch) { | ||
return false; | ||
} | ||
|
||
return 'null' === $node->default->name->toLowerString(); | ||
}); | ||
|
||
if (0 === \count($params)) { | ||
return []; | ||
} | ||
|
||
return \array_map(static function (Node\Param $node): string { | ||
/** @var Node\Expr\Variable $variable */ | ||
$variable = $node->var; | ||
|
||
/** @var string $parameterName */ | ||
$parameterName = $variable->name; | ||
|
||
return \sprintf( | ||
'Parameter "$%s" of closure should not have null as default value.', | ||
$parameterName | ||
); | ||
}, $params); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ameterWithNullDefaultValueRule/Failure/closure-with-parameter-with-null-default-value.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Closures\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function ($bar = null) { | ||
return $bar; | ||
}; |
9 changes: 9 additions & 0 deletions
9
...Rule/Failure/closure-with-parameter-with-root-namespace-referenced-null-default-value.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Closures\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function ($bar = null) { | ||
return $bar; | ||
}; |
9 changes: 9 additions & 0 deletions
9
...tValueRule/Failure/closure-with-parameter-with-wrongly-capitalized-null-default-value.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Closures\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function ($bar = null) { | ||
return $bar; | ||
}; |
9 changes: 9 additions & 0 deletions
9
...erWithNullDefaultValueRule/Success/closure-with-parameter-with-non-null-default-value.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Closures\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function ($bar = true) { | ||
return $bar; | ||
}; |
9 changes: 9 additions & 0 deletions
9
...arameterWithNullDefaultValueRule/Success/closure-with-parameter-without-default-value.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Closures\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function ($bar) { | ||
return $bar; | ||
}; |
8 changes: 8 additions & 0 deletions
8
...xture/Closures/NoParameterWithNullDefaultValueRule/Success/closure-without-parameters.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Closures\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function () { | ||
}; |
78 changes: 78 additions & 0 deletions
78
test/Integration/Closures/NoParameterWithNullDefaultValueRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller. | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/phpstan-rules | ||
*/ | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Integration\Closures; | ||
|
||
use Localheinz\PHPStan\Rules\Closures\NoParameterWithNullDefaultValueRule; | ||
use Localheinz\PHPStan\Rules\Test\Integration\AbstractTestCase; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class NoParameterWithNullDefaultValueRuleTest extends AbstractTestCase | ||
{ | ||
public function providerAnalysisSucceeds(): \Generator | ||
{ | ||
$paths = [ | ||
'closure-with-parameter-with-non-null-default-value' => __DIR__ . '/../../Fixture/Closures/NoParameterWithNullDefaultValueRule/Success/closure-with-parameter-with-non-null-default-value.php', | ||
'closure-with-parameter-without-default-value' => __DIR__ . '/../../Fixture/Closures/NoParameterWithNullDefaultValueRule/Success/closure-with-parameter-without-default-value.php', | ||
'closure-with-parameter-without-parameters' => __DIR__ . '/../../Fixture/Closures/NoParameterWithNullDefaultValueRule/Success/closure-without-parameters.php', | ||
]; | ||
|
||
foreach ($paths as $description => $path) { | ||
yield $description => [ | ||
$path, | ||
]; | ||
} | ||
} | ||
|
||
public function providerAnalysisFails(): \Generator | ||
{ | ||
$paths = [ | ||
'closure-with-parameter-with-null-default-value' => [ | ||
__DIR__ . '/../../Fixture/Closures/NoParameterWithNullDefaultValueRule/Failure/closure-with-parameter-with-null-default-value.php', | ||
[ | ||
'Parameter "$bar" of closure should not have null as default value.', | ||
7, | ||
], | ||
], | ||
'closure-with-parameter-with-root-namespace-referenced-null-default-value' => [ | ||
__DIR__ . '/../../Fixture/Closures/NoParameterWithNullDefaultValueRule/Failure/closure-with-parameter-with-root-namespace-referenced-null-default-value.php', | ||
[ | ||
'Parameter "$bar" of closure should not have null as default value.', | ||
7, | ||
], | ||
], | ||
'closure-with-parameter-with-wrongly-capitalized-null-default-value' => [ | ||
__DIR__ . '/../../Fixture/Closures/NoParameterWithNullDefaultValueRule/Failure/closure-with-parameter-with-wrongly-capitalized-null-default-value.php', | ||
[ | ||
'Parameter "$bar" of closure should not have null as default value.', | ||
7, | ||
], | ||
], | ||
]; | ||
|
||
foreach ($paths as $description => [$path, $error]) { | ||
yield $description => [ | ||
$path, | ||
$error, | ||
]; | ||
} | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new NoParameterWithNullDefaultValueRule(); | ||
} | ||
} |