-
-
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 e08a5cf
Showing
6 changed files
with
139 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,54 @@ | ||
<?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 (0 === \count($node->params)) { | ||
return []; | ||
} | ||
|
||
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergebnis\PHPStan\Rules\Test\Fixture\Functions\NoReturnByReferenceRule; | ||
|
||
function foo(): void | ||
{ | ||
} | ||
|
||
function &bar($bar) | ||
{ | ||
return $bar; | ||
} |
49 changes: 49 additions & 0 deletions
49
test/Integration/Functions/NoReturnByReferenceRuleTest.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,49 @@ | ||
<?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\Test\Integration\Functions; | ||
|
||
use Ergebnis\PHPStan\Rules\Functions; | ||
use Ergebnis\PHPStan\Rules\Test; | ||
use PHPStan\Rules; | ||
use PHPStan\Testing; | ||
|
||
/** | ||
* @covers \Ergebnis\PHPStan\Rules\Functions\NoReturnByReferenceRule | ||
* | ||
* @uses \Ergebnis\PHPStan\Rules\ErrorIdentifier | ||
* | ||
* @extends Testing\RuleTestCase<Functions\NoReturnByReferenceRule> | ||
*/ | ||
final class NoReturnByReferenceRuleTest extends Testing\RuleTestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
public function testNoReturnByReferenceRule(): void | ||
{ | ||
$this->analyse( | ||
self::phpFilesIn(__DIR__ . '/../../Fixture/Functions/NoReturnByReferenceRule'), | ||
[ | ||
[ | ||
'Function Ergebnis\PHPStan\Rules\Test\Fixture\Functions\NoReturnByReferenceRule\baz() returns by reference.', | ||
11, | ||
], | ||
], | ||
); | ||
} | ||
|
||
protected function getRule(): Rules\Rule | ||
{ | ||
return new Functions\NoReturnByReferenceRule(); | ||
} | ||
} |
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