-
-
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 NoParameterWithNullDefaultValueRule
- Loading branch information
1 parent
c887188
commit ac220a0
Showing
44 changed files
with
823 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
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,67 @@ | ||
<?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\Functions; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
|
||
final class NoParameterWithNullDefaultValueRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Function_::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 []; | ||
} | ||
|
||
$functionName = $node->namespacedName; | ||
|
||
return \array_map(static function (Node\Param $node) use ($functionName): string { | ||
/** @var Node\Expr\Variable $variable */ | ||
$variable = $node->var; | ||
|
||
/** @var string $parameterName */ | ||
$parameterName = $variable->name; | ||
|
||
return \sprintf( | ||
'Parameter "$%s" of function "%s()" should not have null as default value.', | ||
$parameterName, | ||
$functionName | ||
); | ||
}, $params); | ||
} | ||
} |
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,90 @@ | ||
<?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\Methods; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection; | ||
use PHPStan\Rules\Rule; | ||
|
||
final class NoParameterWithNullDefaultValueRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\ClassMethod::class; | ||
} | ||
|
||
/** | ||
* @param Node\Stmt\ClassMethod $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 []; | ||
} | ||
|
||
$methodName = $node->name->toString(); | ||
|
||
/** @var Reflection\ClassReflection $classReflection */ | ||
$classReflection = $scope->getClassReflection(); | ||
|
||
if ($classReflection->isAnonymous()) { | ||
return \array_map(static function (Node\Param $node) use ($methodName): string { | ||
/** @var Node\Expr\Variable $variable */ | ||
$variable = $node->var; | ||
|
||
/** @var string $parameterName */ | ||
$parameterName = $variable->name; | ||
|
||
return \sprintf( | ||
'Parameter "$%s" of method "%s()" in anonymous class should not have null as default value.', | ||
$parameterName, | ||
$methodName | ||
); | ||
}, $params); | ||
} | ||
|
||
$className = $classReflection->getName(); | ||
|
||
return \array_map(static function (Node\Param $node) use ($className, $methodName): string { | ||
/** @var Node\Expr\Variable $variable */ | ||
$variable = $node->var; | ||
|
||
/** @var string $parameterName */ | ||
$parameterName = $variable->name; | ||
|
||
return \sprintf( | ||
'Parameter "$%s" of method "%s::%s()" should not have null as default value.', | ||
$parameterName, | ||
$className, | ||
$methodName | ||
); | ||
}, $params); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ithNullDefaultValueRule/Failure/named-function-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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Failure; | ||
|
||
function foo($bar = null) | ||
{ | ||
return $bar; | ||
} |
10 changes: 10 additions & 0 deletions
10
...ilure/named-function-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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Failure; | ||
|
||
function foo($bar = \null) | ||
{ | ||
return $bar; | ||
} |
10 changes: 10 additions & 0 deletions
10
...ule/Failure/named-function-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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Failure; | ||
|
||
function foo($bar = nUlL) | ||
{ | ||
return $bar; | ||
} |
9 changes: 9 additions & 0 deletions
9
...efaultValueRule/Success/anonymous-function-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\Functions\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function ($bar = true) { | ||
return $bar; | ||
}; |
9 changes: 9 additions & 0 deletions
9
...ullDefaultValueRule/Success/anonymous-function-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\Functions\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function ($bar = null) { | ||
return $bar; | ||
}; |
9 changes: 9 additions & 0 deletions
9
...s/anonymous-function-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\Functions\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function ($bar = null) { | ||
return $bar; | ||
}; |
9 changes: 9 additions & 0 deletions
9
...Success/anonymous-function-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\Functions\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function ($bar = null) { | ||
return $bar; | ||
}; |
9 changes: 9 additions & 0 deletions
9
...hNullDefaultValueRule/Success/anonymous-function-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\Functions\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function ($bar) { | ||
return $bar; | ||
}; |
8 changes: 8 additions & 0 deletions
8
...ons/NoParameterWithNullDefaultValueRule/Success/anonymous-function-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\Functions\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
$foo = function () { | ||
}; |
10 changes: 10 additions & 0 deletions
10
...ullDefaultValueRule/Success/named-function-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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
function foo($bar = true) | ||
{ | ||
return $bar; | ||
} |
10 changes: 10 additions & 0 deletions
10
...rWithNullDefaultValueRule/Success/named-function-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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
function foo($bar) | ||
{ | ||
return $bar; | ||
} |
9 changes: 9 additions & 0 deletions
9
...nctions/NoParameterWithNullDefaultValueRule/Success/named-function-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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Success; | ||
|
||
function foo() | ||
{ | ||
} |
13 changes: 13 additions & 0 deletions
13
...ameterWithNullDefaultValueRule/Failure/MethodInClassWithParameterWithNullDefaultValue.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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullDefaultValueRule\Failure; | ||
|
||
final class MethodInClassWithParameterWithNullDefaultValue | ||
{ | ||
public function foo($bar = null) | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...lueRule/Failure/MethodInClassWithParameterWithRootNamespaceReferencedNullDefaultValue.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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullDefaultValueRule\Failure; | ||
|
||
final class MethodInClassWithParameterWithRootNamespaceReferencedNullDefaultValue | ||
{ | ||
public function foo($bar = \null) | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ultValueRule/Failure/MethodInClassWithParameterWithWronglyCapitalizedNullDefaultValue.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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullDefaultValueRule\Failure; | ||
|
||
final class MethodInClassWithParameterWithWronglyCapitalizedNullDefaultValue | ||
{ | ||
public function foo($bar = NuLl) | ||
{ | ||
return $bar; | ||
} | ||
} |
Oops, something went wrong.