Skip to content

Commit b6e90ba

Browse files
committed
Updated Rector to commit 697f4c918d12d19895c863170f9f2264ec2bafd2
rectorphp/rector-src@697f4c9 [depre] Deprecate StaticCallOnNonStaticToInstanceCallRector, as risky change (#8093)
1 parent 6e231c6 commit b6e90ba

5 files changed

Lines changed: 13 additions & 160 deletions

File tree

config/set/php70.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,11 @@
1717
use Rector\Php70\Rector\If_\IfToSpaceshipRector;
1818
use Rector\Php70\Rector\List_\EmptyListRector;
1919
use Rector\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector;
20-
use Rector\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector;
2120
use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector;
2221
use Rector\Php70\Rector\Switch_\ReduceMultipleDefaultSwitchRector;
2322
use Rector\Php70\Rector\Ternary\TernaryToNullCoalescingRector;
2423
use Rector\Php70\Rector\Ternary\TernaryToSpaceshipRector;
2524
use Rector\Php70\Rector\Variable\WrapVariableVariableNameInCurlyBracesRector;
2625
return static function (RectorConfig $rectorConfig): void {
27-
$rectorConfig->rules([
28-
Php4ConstructorRector::class,
29-
TernaryToNullCoalescingRector::class,
30-
RandomFunctionRector::class,
31-
ExceptionHandlerTypehintRector::class,
32-
MultiDirnameRector::class,
33-
ListSplitStringRector::class,
34-
EmptyListRector::class,
35-
// be careful, run this just once, since it can keep swapping order back and forth
36-
ListSwapArrayOrderRector::class,
37-
CallUserMethodRector::class,
38-
EregToPregMatchRector::class,
39-
ReduceMultipleDefaultSwitchRector::class,
40-
TernaryToSpaceshipRector::class,
41-
WrapVariableVariableNameInCurlyBracesRector::class,
42-
IfToSpaceshipRector::class,
43-
StaticCallOnNonStaticToInstanceCallRector::class,
44-
ThisCallOnStaticMethodToStaticCallRector::class,
45-
BreakNotInLoopOrSwitchToReturnRector::class,
46-
RenameMktimeWithoutArgsToTimeRector::class,
47-
IfIssetToCoalescingRector::class,
48-
]);
26+
$rectorConfig->rules([Php4ConstructorRector::class, TernaryToNullCoalescingRector::class, RandomFunctionRector::class, ExceptionHandlerTypehintRector::class, MultiDirnameRector::class, ListSplitStringRector::class, EmptyListRector::class, ListSwapArrayOrderRector::class, CallUserMethodRector::class, EregToPregMatchRector::class, ReduceMultipleDefaultSwitchRector::class, TernaryToSpaceshipRector::class, WrapVariableVariableNameInCurlyBracesRector::class, IfToSpaceshipRector::class, ThisCallOnStaticMethodToStaticCallRector::class, BreakNotInLoopOrSwitchToReturnRector::class, RenameMktimeWithoutArgsToTimeRector::class, IfIssetToCoalescingRector::class]);
4927
};

rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1919
/**
2020
* @see \Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\CoalesceToTernaryRectorTest
21+
*
22+
* @see https://github.com/rectorphp/rector/issues/9730
2123
*/
2224
final class CoalesceToTernaryRector extends AbstractRector
2325
{

rules/CodingStyle/ValueObject/ObjectMagicMethods.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
namespace Rector\CodingStyle\ValueObject;
55

66
use Rector\ValueObject\MethodName;
7+
/**
8+
* @api designed for public use
9+
*/
710
final class ObjectMagicMethods
811
{
912
/**

rules/Php70/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php

Lines changed: 5 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,19 @@
44
namespace Rector\Php70\Rector\StaticCall;
55

66
use PhpParser\Node;
7-
use PhpParser\Node\Expr;
8-
use PhpParser\Node\Expr\MethodCall;
9-
use PhpParser\Node\Expr\New_;
10-
use PhpParser\Node\Expr\PropertyFetch;
117
use PhpParser\Node\Expr\StaticCall;
12-
use PhpParser\Node\Expr\Variable;
13-
use PHPStan\Analyser\Scope;
14-
use PHPStan\Reflection\ClassReflection;
15-
use PHPStan\Reflection\MethodReflection;
16-
use PHPStan\Reflection\ReflectionProvider;
17-
use PHPStan\Type\ObjectType;
18-
use Rector\CodingStyle\ValueObject\ObjectMagicMethods;
19-
use Rector\Enum\ObjectReference;
20-
use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver;
21-
use Rector\NodeCollector\StaticAnalyzer;
22-
use Rector\PHPStan\ScopeFetcher;
8+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
9+
use Rector\Exception\ShouldNotHappenException;
2310
use Rector\Rector\AbstractRector;
24-
use Rector\Reflection\ReflectionResolver;
2511
use Rector\ValueObject\PhpVersionFeature;
2612
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
27-
use ReflectionMethod;
2813
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2914
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
3015
/**
31-
* @see \Rector\Tests\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector\StaticCallOnNonStaticToInstanceCallRectorTest
16+
* @deprecated as risky change with little value. Use manually or custom rule where needed instead.
3217
*/
33-
final class StaticCallOnNonStaticToInstanceCallRector extends AbstractRector implements MinPhpVersionInterface
18+
final class StaticCallOnNonStaticToInstanceCallRector extends AbstractRector implements MinPhpVersionInterface, DeprecatedInterface
3419
{
35-
/**
36-
* @readonly
37-
*/
38-
private StaticAnalyzer $staticAnalyzer;
39-
/**
40-
* @readonly
41-
*/
42-
private ReflectionProvider $reflectionProvider;
43-
/**
44-
* @readonly
45-
*/
46-
private ReflectionResolver $reflectionResolver;
47-
/**
48-
* @readonly
49-
*/
50-
private ParentClassScopeResolver $parentClassScopeResolver;
51-
public function __construct(StaticAnalyzer $staticAnalyzer, ReflectionProvider $reflectionProvider, ReflectionResolver $reflectionResolver, ParentClassScopeResolver $parentClassScopeResolver)
52-
{
53-
$this->staticAnalyzer = $staticAnalyzer;
54-
$this->reflectionProvider = $reflectionProvider;
55-
$this->reflectionResolver = $reflectionResolver;
56-
$this->parentClassScopeResolver = $parentClassScopeResolver;
57-
}
5820
public function provideMinPhpVersion(): int
5921
{
6022
return PhpVersionFeature::INSTANCE_CALL;
@@ -107,98 +69,6 @@ public function getNodeTypes(): array
10769
*/
10870
public function refactor(Node $node): ?Node
10971
{
110-
if ($node->name instanceof Expr) {
111-
return null;
112-
}
113-
$methodName = $this->getName($node->name);
114-
$className = $this->resolveStaticCallClassName($node);
115-
if ($methodName === null) {
116-
return null;
117-
}
118-
if ($className === null) {
119-
return null;
120-
}
121-
$scope = ScopeFetcher::fetch($node);
122-
if ($this->shouldSkip($methodName, $className, $node, $scope)) {
123-
return null;
124-
}
125-
$classReflection = $scope->getClassReflection();
126-
if ($classReflection instanceof ClassReflection && $classReflection->getName() === $className) {
127-
// detect any scope where $this is unavailable or possibly unavailable
128-
if (!$scope->hasVariableType('this')->yes()) {
129-
return null;
130-
}
131-
return new MethodCall(new Variable('this'), $node->name, $node->args);
132-
}
133-
if ($this->isInstantiable($className, $scope)) {
134-
$new = new New_($node->class);
135-
return new MethodCall($new, $node->name, $node->args);
136-
}
137-
return null;
138-
}
139-
private function resolveStaticCallClassName(StaticCall $staticCall): ?string
140-
{
141-
if ($staticCall->class instanceof PropertyFetch) {
142-
$objectType = $this->getType($staticCall->class);
143-
if ($objectType instanceof ObjectType) {
144-
return $objectType->getClassName();
145-
}
146-
}
147-
return $this->getName($staticCall->class);
148-
}
149-
private function shouldSkip(string $methodName, string $className, StaticCall $staticCall, Scope $scope): bool
150-
{
151-
if (in_array($methodName, ObjectMagicMethods::METHOD_NAMES, \true)) {
152-
return \true;
153-
}
154-
if (!$this->reflectionProvider->hasClass($className)) {
155-
return \true;
156-
}
157-
$classReflection = $this->reflectionProvider->getClass($className);
158-
if ($classReflection->isAbstract()) {
159-
return \true;
160-
}
161-
// does the method even exist?
162-
if (!$classReflection->hasMethod($methodName)) {
163-
return \true;
164-
}
165-
$isStaticMethod = $this->staticAnalyzer->isStaticMethod($classReflection, $methodName);
166-
if ($isStaticMethod) {
167-
return \true;
168-
}
169-
$currentClassReflection = $scope->getClassReflection();
170-
if ($currentClassReflection instanceof ClassReflection && $this->reflectionProvider->hasClass($className) && $currentClassReflection->isSubclassOfClass($this->reflectionProvider->getClass($className))) {
171-
return \true;
172-
}
173-
$className = $this->getName($staticCall->class);
174-
if (in_array($className, [ObjectReference::PARENT, ObjectReference::SELF, ObjectReference::STATIC], \true)) {
175-
return \true;
176-
}
177-
if ($className === 'class') {
178-
return \true;
179-
}
180-
$parentClassName = $this->parentClassScopeResolver->resolveParentClassName($scope);
181-
return $className === $parentClassName;
182-
}
183-
private function isInstantiable(string $className, Scope $scope): bool
184-
{
185-
if (!$this->reflectionProvider->hasClass($className)) {
186-
return \false;
187-
}
188-
$methodReflection = $this->reflectionResolver->resolveMethodReflection($className, '__callStatic', $scope);
189-
if ($methodReflection instanceof MethodReflection) {
190-
return \false;
191-
}
192-
$classReflection = $this->reflectionProvider->getClass($className);
193-
$nativeReflection = $classReflection->getNativeReflection();
194-
$reflectionMethod = $nativeReflection->getConstructor();
195-
if (!$reflectionMethod instanceof ReflectionMethod) {
196-
return \true;
197-
}
198-
if (!$reflectionMethod->isPublic()) {
199-
return \false;
200-
}
201-
// required parameters in constructor, nothing we can do
202-
return !(bool) $reflectionMethod->getNumberOfRequiredParameters();
72+
throw new ShouldNotHappenException(sprintf('"%s" is deprecated as risky change with little value. Use manually or custom rule where needed instead', self::class));
20373
}
20474
}

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '9ba9187fb327746383199a5924497539bced744d';
22+
public const PACKAGE_VERSION = '697f4c918d12d19895c863170f9f2264ec2bafd2';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-06-27 21:43:46';
27+
public const RELEASE_DATE = '2026-06-27 20:10:01';
2828
/**
2929
* @var int
3030
*/

0 commit comments

Comments
 (0)