-
-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not prefix "parent", "self", "static" and native constants (#6159)
Co-authored-by: kaizen-ci <info@kaizen-ci.org>
- Loading branch information
1 parent
95b31d0
commit e8a76e8
Showing
8 changed files
with
309 additions
and
80 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
45 changes: 45 additions & 0 deletions
45
...Namespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/no_prefix_consts.php.inc
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
final class NoPrefixConsts | ||
{ | ||
public function someMethod() | ||
{ | ||
$parentClass = parent::someMethod(); | ||
|
||
$value = true; | ||
$value = FALSE; | ||
$value = __DIR__; | ||
|
||
$native = PREG_GREP_INVERT; | ||
|
||
$localConstant = LOCAL_CONSTANT; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector\Fixture; | ||
|
||
final class NoPrefixConsts | ||
{ | ||
public function someMethod() | ||
{ | ||
$parentClass = parent::someMethod(); | ||
|
||
$value = true; | ||
$value = FALSE; | ||
$value = __DIR__; | ||
|
||
$native = PREG_GREP_INVERT; | ||
|
||
$localConstant = \LOCAL_CONSTANT; | ||
} | ||
} | ||
|
||
?> |
29 changes: 29 additions & 0 deletions
29
...amespace/NormalizeNamespaceByPSR4ComposerAutoloadRector/Fixture/skip_class_consts.php.inc
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,29 @@ | ||
<?php | ||
|
||
final class SkipClassConsts | ||
{ | ||
const KEY = 'value'; | ||
|
||
public function someMethod() | ||
{ | ||
return self::KEY; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector\Fixture; | ||
|
||
final class SkipClassConsts | ||
{ | ||
const KEY = 'value'; | ||
|
||
public function someMethod() | ||
{ | ||
return self::KEY; | ||
} | ||
} | ||
|
||
?> |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\NodeAnalyzer; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
|
||
final class UsedVariableNameAnalyzer | ||
{ | ||
/** | ||
* @var NodeNameResolver | ||
*/ | ||
private $nodeNameResolver; | ||
|
||
public function __construct(NodeNameResolver $nodeNameResolver) | ||
{ | ||
$this->nodeNameResolver = $nodeNameResolver; | ||
} | ||
|
||
public function isVariableNamed(Node $node, Variable $variable): bool | ||
{ | ||
if (($node instanceof MethodCall || $node instanceof PropertyFetch) && ($node->name instanceof Variable && is_string( | ||
$node->name->name | ||
))) { | ||
return $this->nodeNameResolver->isName($variable, $node->name->name); | ||
} | ||
|
||
if (! $node instanceof Variable) { | ||
return false; | ||
} | ||
|
||
return $this->nodeNameResolver->areNamesEqual($variable, $node); | ||
} | ||
} |
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,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PSR4\NodeManipulator; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt; | ||
use PHPStan\Reflection\Constant\RuntimeConstantReflection; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use Rector\Core\Configuration\Option; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser; | ||
use Symplify\PackageBuilder\Parameter\ParameterProvider; | ||
|
||
final class FullyQualifyStmtsAnalyzer | ||
{ | ||
/** | ||
* @var ParameterProvider | ||
*/ | ||
private $parameterProvider; | ||
|
||
/** | ||
* @var SimpleCallableNodeTraverser | ||
*/ | ||
private $simpleCallableNodeTraverser; | ||
|
||
/** | ||
* @var NodeNameResolver | ||
*/ | ||
private $nodeNameResolver; | ||
|
||
/** | ||
* @var ReflectionProvider | ||
*/ | ||
private $reflectionProvider; | ||
|
||
public function __construct( | ||
ParameterProvider $parameterProvider, | ||
SimpleCallableNodeTraverser $simpleCallableNodeTraverser, | ||
NodeNameResolver $nodeNameResolver, | ||
ReflectionProvider $reflectionProvider | ||
) { | ||
$this->parameterProvider = $parameterProvider; | ||
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser; | ||
$this->nodeNameResolver = $nodeNameResolver; | ||
$this->reflectionProvider = $reflectionProvider; | ||
} | ||
|
||
/** | ||
* @param Stmt[] $nodes | ||
*/ | ||
public function process(array $nodes): void | ||
{ | ||
// no need to | ||
if ($this->parameterProvider->provideBoolParameter(Option::AUTO_IMPORT_NAMES)) { | ||
return; | ||
} | ||
|
||
// FQNize all class names | ||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node): ?FullyQualified { | ||
if (! $node instanceof Name) { | ||
return null; | ||
} | ||
|
||
$fullyQualifiedName = $this->nodeNameResolver->getName($node); | ||
if (in_array($fullyQualifiedName, ['self', 'parent', 'static'], true)) { | ||
return null; | ||
} | ||
|
||
if ($this->isNativeConstant($node)) { | ||
return null; | ||
} | ||
|
||
return new FullyQualified($fullyQualifiedName); | ||
}); | ||
} | ||
|
||
private function isNativeConstant(Name $name): bool | ||
{ | ||
$parent = $name->getAttribute(AttributeKey::PARENT_NODE); | ||
if (! $parent instanceof ConstFetch) { | ||
return false; | ||
} | ||
|
||
$scope = $name->getAttribute(AttributeKey::SCOPE); | ||
if (! $this->reflectionProvider->hasConstant($name, $scope)) { | ||
return false; | ||
} | ||
|
||
$constantReflection = $this->reflectionProvider->getConstant($name, $scope); | ||
return $constantReflection instanceof RuntimeConstantReflection; | ||
} | ||
} |
Oops, something went wrong.