Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

14 changes: 7 additions & 7 deletions rules/CodeQuality/Rector/Class_/ConvertStaticToSelfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,17 @@ public function refactor(Node $node): ?Class_
{
$hasChanged = false;
$isFinal = $node->isFinal() || FeatureFlags::treatClassesAsFinal($node);

$scope = null;
$classReflection = null;

$classReflection = $this->getClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}

$this->traverseNodesWithCallable($node->stmts, function (Node $subNode) use (
&$hasChanged,
&$classReflection,
$classReflection,
$isFinal,
&$scope,
$node,
Expand All @@ -108,11 +113,6 @@ public function refactor(Node $node): ?Class_
}

$scope ??= ScopeFetcher::fetch($node);
$classReflection ??= $scope->getClassReflection();

if (! $classReflection instanceof ClassReflection) {
return null;
}

if ($this->shouldSkip($subNode, $classReflection, $isFinal, $scope)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node\Stmt\Enum_;
use PHPStan\Reflection\ClassReflection;
use Rector\NodeManipulator\ClassConstManipulator;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\RuleDoc\CodeSample\CodeSample;
Expand Down Expand Up @@ -78,7 +77,7 @@ public function refactor(Node $node): ?Node
continue;
}

if ($this->shouldSkipClassConst($stmt)) {
if ($this->shouldSkipClassConst($classReflection, $stmt)) {
continue;
}

Expand All @@ -101,7 +100,7 @@ public function refactor(Node $node): ?Node
return $node;
}

private function shouldSkipClassConst(ClassConst $classConst): bool
private function shouldSkipClassConst(ClassReflection $classReflection, ClassConst $classConst): bool
{
if (! $classConst->isPrivate()) {
return true;
Expand All @@ -111,13 +110,6 @@ private function shouldSkipClassConst(ClassConst $classConst): bool
return true;
}

$scope = ScopeFetcher::fetch($classConst);

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

return $this->hasParentClassOfEnumSuffix($classReflection);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
Expand Down Expand Up @@ -91,9 +90,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$scope = ScopeFetcher::fetch($node);
$classReflection = $scope->getClassReflection();

$classReflection = $this->getClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function refactor(Node $node): Class_|null
return null;
}

$classReflection = $scope->getClassReflection();
$classReflection = $this->getClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPStan\Reflection\Php\PhpMethodReflection;
use Rector\Enum\ObjectReference;
use Rector\NodeCollector\StaticAnalyzer;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\RuleDoc\CodeSample\CodeSample;
Expand Down Expand Up @@ -93,13 +92,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$scope = ScopeFetcher::fetch($node);
if (! $scope->isInClass()) {
$classReflection = $this->getClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}

$classReflection = $scope->getClassReflection();

// skip PHPUnit calls, as they accept both self:: and $this-> formats
if ($classReflection->is('PHPUnit\Framework\TestCase')) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Printer\BetterStandardPrinter;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
Expand Down Expand Up @@ -113,6 +112,11 @@ public function refactor(Node $node): ?Node

$hasChanged = false;

$classReflection = $this->getClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}

foreach ($node->getMethods() as $classMethod) {
if ($this->isName($classMethod, MethodName::CONSTRUCT)) {
continue;
Expand All @@ -127,14 +131,11 @@ public function refactor(Node $node): ?Node
continue;
}

$scope = ScopeFetcher::fetch($node);
$currentClassReflection = $scope->getClassReflection();

// It relies on phpstorm stubs that define 2 kind of query method for both php 7.4 and php 8.0
// @see https://github.com/JetBrains/phpstorm-stubs/blob/e2e898a29929d2f520fe95bdb2109d8fa895ba4a/PDO/PDO.php#L1096-L1126
if ($parentMethodReflection->getName() === 'query'
&& $currentClassReflection instanceof ClassReflection
&& $currentClassReflection->is('PDO')) {
// && $classReflection instanceof ClassReflection
&& $classReflection->is('PDO')) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PHPStan\Reflection\ClassReflection;
use Rector\PHPStan\ScopeFetcher;
use Rector\Privatization\Guard\OverrideByParentClassGuard;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -68,8 +67,7 @@ public function refactor(Node $node): ?Node
return null;
}

$scope = ScopeFetcher::fetch($node);
$classReflection = $scope->getClassReflection();
$classReflection = $this->getClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ClassReflection;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStan\ScopeFetcher;
use Rector\Privatization\Guard\LaravelModelGuard;
use Rector\Privatization\Guard\OverrideByParentClassGuard;
use Rector\Privatization\Guard\ParentClassMagicCallGuard;
Expand Down Expand Up @@ -80,14 +79,12 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->overrideByParentClassGuard->isLegal($node)) {
$classReflection = $this->getClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}

$scope = ScopeFetcher::fetch($node);

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
if (! $this->overrideByParentClassGuard->isLegal($node)) {
return null;
}

Expand Down Expand Up @@ -133,13 +130,6 @@ public function refactor(Node $node): ?Node

private function shouldSkipClassMethod(ClassMethod $classMethod): bool
{
// edge case in nette framework
/** @var string $methodName */
$methodName = $this->getName($classMethod->name);
if (str_starts_with($methodName, 'createComponent')) {
return true;
}

if (! $classMethod->isProtected()) {
return true;
}
Expand All @@ -149,7 +139,7 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
}

// if has parent call, its probably overriding parent one → skip it
$hasParentCall = (bool) $this->betterNodeFinder->findFirst(
return (bool) $this->betterNodeFinder->findFirst(
(array) $classMethod->stmts,
function (Node $node): bool {
if (! $node instanceof StaticCall) {
Expand All @@ -159,7 +149,5 @@ function (Node $node): bool {
return $this->isName($node->class, 'parent');
}
);

return $hasParentCall;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\Php74\Guard\MakePropertyTypedGuard;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
Expand Down Expand Up @@ -83,9 +82,7 @@ public function refactor(Node $node): ?Node
return null;
}

$scope = ScopeFetcher::fetch($node);

$classReflection = $scope->getClassReflection();
$classReflection = $this->getClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
Expand Down Expand Up @@ -71,6 +70,11 @@ public function refactor(Node $node): ?Node
{
$hasChanged = false;

$classReflection = $this->getClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}

foreach ($node->getProperties() as $property) {
if (! $property->type instanceof Identifier) {
continue;
Expand Down Expand Up @@ -103,7 +107,7 @@ public function refactor(Node $node): ?Node
continue;
}

if ($this->hasUsefulParentPropertyVarTag($node, $property, $propertyDefaultType)) {
if ($this->hasUsefulParentPropertyVarTag($classReflection, $property, $propertyDefaultType)) {
continue;
}

Expand All @@ -123,7 +127,7 @@ public function refactor(Node $node): ?Node
return $node;
}

private function hasUsefulParentPropertyVarTag(Class_ $class, Property $property, Type $propertyDefaultType): bool
private function hasUsefulParentPropertyVarTag(ClassReflection $classReflection, Property $property, Type $propertyDefaultType): bool
{
$propertyName = $this->getName($property);
if ($propertyName === null) {
Expand All @@ -135,13 +139,6 @@ private function hasUsefulParentPropertyVarTag(Class_ $class, Property $property
return false;
}

$scope = ScopeFetcher::fetch($class);

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

foreach ($classReflection->getParents() as $parentClassReflection) {
if (! $parentClassReflection->hasNativeProperty($propertyName)) {
continue;
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ static function (AbstractRector $rector) use ($rectorConfig): void {
$rectorConfig->get(NodeComparator::class),
$rectorConfig->get(CurrentFileProvider::class),
$rectorConfig->get(CommentsMerger::class),
$rectorConfig->get(ReflectionProvider::class)
);
}
);
Expand Down
23 changes: 22 additions & 1 deletion src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\PropertyItem;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Const_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Application\Provider\CurrentFileProvider;
Expand Down Expand Up @@ -42,14 +45,17 @@ abstract class AbstractRector implements RectorInterface

private CommentsMerger $commentsMerger;

private ReflectionProvider $reflectionProvider;

public function autowire(
NodeNameResolver $nodeNameResolver,
NodeTypeResolver $nodeTypeResolver,
SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
NodeFactory $nodeFactory,
NodeComparator $nodeComparator,
CurrentFileProvider $currentFileProvider,
CommentsMerger $commentsMerger
CommentsMerger $commentsMerger,
ReflectionProvider $reflectionProvider
): void {
$this->nodeNameResolver = $nodeNameResolver;
$this->nodeTypeResolver = $nodeTypeResolver;
Expand All @@ -58,6 +64,21 @@ public function autowire(
$this->nodeComparator = $nodeComparator;
$this->currentFileProvider = $currentFileProvider;
$this->commentsMerger = $commentsMerger;
$this->reflectionProvider = $reflectionProvider;
}

protected function getClassReflection(ClassLike $classLike): ?ClassReflection
{
$className = $this->getName($classLike);
if ($className === null) {
return null;
}

if (! $this->reflectionProvider->hasClass($className)) {
throw new ShouldNotHappenException(sprintf('Class "%s" should be known at this point', $className));
}

return $this->reflectionProvider->getClass($className);
}

protected function getFile(): File
Expand Down
Loading