-
-
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\NoParameterWithNullableTypeDeclaratio…
…nRule
- Loading branch information
1 parent
161caaa
commit f5263d6
Showing
9 changed files
with
174 additions
and
0 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
60 changes: 60 additions & 0 deletions
60
src/Closures/NoParameterWithNullableTypeDeclarationRule.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,60 @@ | ||
<?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 NoParameterWithNullableTypeDeclarationRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\Closure::class; | ||
} | ||
|
||
/** | ||
* @param Node\Expr\Closure $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 { | ||
return $node->type instanceof Node\NullableType; | ||
}); | ||
|
||
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 a nullable type declaration.', | ||
$parameterName | ||
); | ||
}, $params); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ableTypeDeclarationRule/Failure/closure-with-parameter-with-nullable-type-declaration.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 (?string $bar) { | ||
return $bar; | ||
}; |
9 changes: 9 additions & 0 deletions
9
...rWithNullableTypeDeclarationRule/Success/closure-with-parameter-with-type-declaration.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 (string $bar) { | ||
return $bar; | ||
}; |
9 changes: 9 additions & 0 deletions
9
...thNullableTypeDeclarationRule/Success/closure-with-parameter-without-type-declaration.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
...losures/NoParameterWithNullableTypeDeclarationRule/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 () { | ||
}; |
64 changes: 64 additions & 0 deletions
64
test/Integration/Closures/NoParameterWithNullableTypeDeclarationRuleTest.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,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\Test\Integration\Closures; | ||
|
||
use Localheinz\PHPStan\Rules\Closures\NoParameterWithNullableTypeDeclarationRule; | ||
use Localheinz\PHPStan\Rules\Test\Integration\AbstractTestCase; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class NoParameterWithNullableTypeDeclarationRuleTest extends AbstractTestCase | ||
{ | ||
public function providerAnalysisSucceeds(): \Generator | ||
{ | ||
$paths = [ | ||
'closure-with-parameter-with-type-declaration' => __DIR__ . '/../../Fixture/Closures/NoParameterWithNullableTypeDeclarationRule/Success/closure-with-parameter-with-type-declaration.php', | ||
'closure-with-parameter-without-type-declaration' => __DIR__ . '/../../Fixture/Closures/NoParameterWithNullableTypeDeclarationRule/Success/closure-with-parameter-without-type-declaration.php', | ||
'closure-without-parameters' => __DIR__ . '/../../Fixture/Closures/NoParameterWithNullableTypeDeclarationRule/Success/closure-without-parameters.php', | ||
]; | ||
|
||
foreach ($paths as $description => $path) { | ||
yield $description => [ | ||
$path, | ||
]; | ||
} | ||
} | ||
|
||
public function providerAnalysisFails(): \Generator | ||
{ | ||
$paths = [ | ||
'closure-with-parameter-with-nullable-type-declaration' => [ | ||
__DIR__ . '/../../Fixture/Closures/NoParameterWithNullableTypeDeclarationRule/Failure/closure-with-parameter-with-nullable-type-declaration.php', | ||
[ | ||
'Parameter "$bar" of closure should not have a nullable type declaration.', | ||
7, | ||
], | ||
], | ||
]; | ||
|
||
foreach ($paths as $description => [$path, $error]) { | ||
yield $description => [ | ||
$path, | ||
$error, | ||
]; | ||
} | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new NoParameterWithNullableTypeDeclarationRule(); | ||
} | ||
} |