-
-
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 NoAssignByReferenceRule
- Loading branch information
1 parent
45d1852
commit 028c8fd
Showing
8 changed files
with
141 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,41 @@ | ||
<?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\Expressions; | ||
|
||
use Ergebnis\PHPStan\Rules\ErrorIdentifier; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser; | ||
use PHPStan\Rules; | ||
|
||
/** | ||
* @implements Rules\Rule<Node\Expr\AssignRef> | ||
*/ | ||
final class NoAssignByReferenceRule implements Rules\Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\AssignRef::class; | ||
} | ||
|
||
public function processNode( | ||
Node $node, | ||
Analyser\Scope $scope | ||
): array { | ||
return [ | ||
Rules\RuleErrorBuilder::message('Assign by reference should not be used.') | ||
->identifier(ErrorIdentifier::noAssignByReference()->toString()) | ||
->build(), | ||
]; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
test/Fixture/Expressions/NoAssignByReferenceRule/script.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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergebnis\PHPStan\Rules\Test\Fixture\Expressions\NoAssignByReferenceRule; | ||
|
||
$foo = 'bar'; | ||
|
||
$bar = $foo; | ||
|
||
$baz = &$foo; |
49 changes: 49 additions & 0 deletions
49
test/Integration/Expressions/NoAssignByReferenceRuleTest.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\Expressions; | ||
|
||
use Ergebnis\PHPStan\Rules\Expressions; | ||
use Ergebnis\PHPStan\Rules\Test; | ||
use PHPStan\Rules; | ||
use PHPStan\Testing; | ||
|
||
/** | ||
* @covers \Ergebnis\PHPStan\Rules\Expressions\NoAssignByReferenceRule | ||
* | ||
* @uses \Ergebnis\PHPStan\Rules\ErrorIdentifier | ||
* | ||
* @extends Testing\RuleTestCase<Expressions\NoAssignByReferenceRule> | ||
*/ | ||
final class NoAssignByReferenceRuleTest extends Testing\RuleTestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
public function testNoAssignByReferenceRule(): void | ||
{ | ||
$this->analyse( | ||
self::phpFilesIn(__DIR__ . '/../../Fixture/Expressions/NoAssignByReferenceRule'), | ||
[ | ||
[ | ||
'Assign by reference should not be used.', | ||
11, | ||
], | ||
], | ||
); | ||
} | ||
|
||
protected function getRule(): Rules\Rule | ||
{ | ||
return new Expressions\NoAssignByReferenceRule(); | ||
} | ||
} |
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