-
-
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 NoReturnByReferenceRule
- Loading branch information
1 parent
4e42c4f
commit e1f463e
Showing
17 changed files
with
406 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,50 @@ | ||
<?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 NoReturnByReferenceRule implements Rules\Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Function_::class; | ||
} | ||
|
||
public function processNode( | ||
Node $node, | ||
Analyser\Scope $scope | ||
): array { | ||
if (false === $node->byRef) { | ||
return []; | ||
} | ||
|
||
$message = \sprintf( | ||
'Function %s() returns by reference.', | ||
$node->namespacedName, | ||
); | ||
|
||
return [ | ||
Rules\RuleErrorBuilder::message($message) | ||
->identifier(ErrorIdentifier::noReturnByReference()->toString()) | ||
->build(), | ||
]; | ||
} | ||
} |
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,72 @@ | ||
<?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\Methods; | ||
|
||
use Ergebnis\PHPStan\Rules\ErrorIdentifier; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser; | ||
use PHPStan\Reflection; | ||
use PHPStan\Rules; | ||
|
||
/** | ||
* @implements Rules\Rule<Node\Stmt\ClassMethod> | ||
*/ | ||
final class NoReturnByReferenceRule implements Rules\Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\ClassMethod::class; | ||
} | ||
|
||
public function processNode( | ||
Node $node, | ||
Analyser\Scope $scope | ||
): array { | ||
if (false === $node->byRef) { | ||
return []; | ||
} | ||
|
||
$methodName = $node->name->toString(); | ||
|
||
/** @var Reflection\ClassReflection $classReflection */ | ||
$classReflection = $scope->getClassReflection(); | ||
|
||
if ($classReflection->isAnonymous()) { | ||
$message = \sprintf( | ||
'Method %s() in anonymous class returns by reference.', | ||
$methodName, | ||
); | ||
|
||
return [ | ||
Rules\RuleErrorBuilder::message($message) | ||
->identifier(ErrorIdentifier::noReturnByReference()->toString()) | ||
->build(), | ||
]; | ||
} | ||
|
||
$className = $classReflection->getName(); | ||
|
||
$message = \sprintf( | ||
'Method %s::%s() returns by reference.', | ||
$className, | ||
$methodName, | ||
); | ||
|
||
return [ | ||
Rules\RuleErrorBuilder::message($message) | ||
->identifier(ErrorIdentifier::noReturnByReference()->toString()) | ||
->build(), | ||
]; | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergebnis\PHPStan\Rules\Test\Fixture\Functions\NoReturnByReferenceRule; | ||
|
||
function foo(): void | ||
{ | ||
} | ||
|
||
function &bar($bar) | ||
{ | ||
return $bar; | ||
} |
13 changes: 13 additions & 0 deletions
13
test/Fixture/Methods/NoReturnByReferenceRule/MethodInClass.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 Ergebnis\PHPStan\Rules\Test\Fixture\Methods\NoReturnByReferenceRule; | ||
|
||
final class MethodInClass | ||
{ | ||
public function foo($bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
test/Fixture/Methods/NoReturnByReferenceRule/MethodInClassReturningByReference.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 Ergebnis\PHPStan\Rules\Test\Fixture\Methods\NoReturnByReferenceRule; | ||
|
||
final class MethodInClassReturningByReference | ||
{ | ||
public function &foo($bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
test/Fixture/Methods/NoReturnByReferenceRule/MethodInInterface.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 Ergebnis\PHPStan\Rules\Test\Fixture\Methods\NoReturnByReferenceRule; | ||
|
||
interface MethodInInterface | ||
{ | ||
public function foo($bar); | ||
} |
10 changes: 10 additions & 0 deletions
10
test/Fixture/Methods/NoReturnByReferenceRule/MethodInInterfaceReturningByReference.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 Ergebnis\PHPStan\Rules\Test\Fixture\Methods\NoReturnByReferenceRule; | ||
|
||
interface MethodInInterfaceReturningByReference | ||
{ | ||
public function &foo($bar); | ||
} |
13 changes: 13 additions & 0 deletions
13
test/Fixture/Methods/NoReturnByReferenceRule/MethodInTrait.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 Ergebnis\PHPStan\Rules\Test\Fixture\Methods\NoReturnByReferenceRule; | ||
|
||
trait MethodInTrait | ||
{ | ||
public function foo($bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
test/Fixture/Methods/NoReturnByReferenceRule/MethodInTraitReturningByReference.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 Ergebnis\PHPStan\Rules\Test\Fixture\Methods\NoReturnByReferenceRule; | ||
|
||
trait MethodInTraitReturningByReference | ||
{ | ||
public function &foo($bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\NoReturnByReferenceRule; | ||
|
||
$foo = new class() { | ||
public function foo(): void | ||
{ | ||
} | ||
}; | ||
|
||
$bar = new class() { | ||
public function foo($bar) | ||
{ | ||
return $bar; | ||
} | ||
}; | ||
|
||
$baz = new class() { | ||
public function &foo($bar) | ||
{ | ||
return $bar; | ||
} | ||
}; |
Oops, something went wrong.