-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement NoParameterPassedByReferenceRule
- Loading branch information
1 parent
4de8c33
commit 32b3d0a
Showing
31 changed files
with
784 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018-2025 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/phpstan-rules | ||
*/ | ||
|
||
namespace Ergebnis\PHPStan\Rules\Closures; | ||
|
||
use Ergebnis\PHPStan\Rules\ErrorIdentifier; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser; | ||
use PHPStan\Rules; | ||
|
||
/** | ||
* @implements Rules\Rule<Node\Expr\Closure> | ||
*/ | ||
final class NoParameterPassedByReferenceRule implements Rules\Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\Closure::class; | ||
} | ||
|
||
public function processNode( | ||
Node $node, | ||
Analyser\Scope $scope | ||
): array { | ||
if (0 === \count($node->params)) { | ||
return []; | ||
} | ||
|
||
$parametersPassedByReference = \array_values(\array_filter($node->params, static function (Node\Param $parameter): bool { | ||
return $parameter->byRef; | ||
})); | ||
|
||
if (0 === \count($parametersPassedByReference)) { | ||
return []; | ||
} | ||
|
||
return \array_map(static function (Node\Param $parameterPassedByReference): Rules\RuleError { | ||
/** @var Node\Expr\Variable $variable */ | ||
$variable = $parameterPassedByReference->var; | ||
|
||
/** @var string $parameterName */ | ||
$parameterName = $variable->name; | ||
|
||
$message = \sprintf( | ||
'Closure has parameter $%s that is passed by reference.', | ||
$parameterName, | ||
); | ||
|
||
return Rules\RuleErrorBuilder::message($message) | ||
->identifier(ErrorIdentifier::noParameterPassedByReference()->toString()) | ||
->build(); | ||
}, $parametersPassedByReference); | ||
} | ||
} |
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-2025 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/phpstan-rules | ||
*/ | ||
|
||
namespace Ergebnis\PHPStan\Rules\Functions; | ||
|
||
use Ergebnis\PHPStan\Rules\ErrorIdentifier; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser; | ||
use PHPStan\Rules; | ||
|
||
/** | ||
* @implements Rules\Rule<Node\Stmt\Function_> | ||
*/ | ||
final class NoParameterPassedByReferenceRule implements Rules\Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Function_::class; | ||
} | ||
|
||
public function processNode( | ||
Node $node, | ||
Analyser\Scope $scope | ||
): array { | ||
if (0 === \count($node->params)) { | ||
return []; | ||
} | ||
|
||
$parametersPassedByReference = \array_values(\array_filter($node->params, static function (Node\Param $parameter): bool { | ||
return $parameter->byRef; | ||
})); | ||
|
||
if (0 === \count($parametersPassedByReference)) { | ||
return []; | ||
} | ||
|
||
$functionName = $node->namespacedName; | ||
|
||
return \array_map(static function (Node\Param $parameterPassedByReference) use ($functionName): Rules\RuleError { | ||
/** @var Node\Expr\Variable $variable */ | ||
$variable = $parameterPassedByReference->var; | ||
|
||
/** @var string $parameterName */ | ||
$parameterName = $variable->name; | ||
|
||
$message = \sprintf( | ||
'Function %s() has parameter $%s that is passed by reference.', | ||
$functionName, | ||
$parameterName, | ||
); | ||
|
||
return Rules\RuleErrorBuilder::message($message) | ||
->identifier(ErrorIdentifier::noParameterWithNullDefaultValue()->toString()) | ||
->build(); | ||
}, $parametersPassedByReference); | ||
} | ||
} |
Oops, something went wrong.