Skip to content

Commit 7365178

Browse files
committed
Updated Rector to commit ca9da16d1620daf8ca4a12912dee1aa8fbbb5b71
rectorphp/rector-src@ca9da16 Fix deep ArrayItem scope filling on Foreach_ value (#7873)
1 parent 48bf940 commit 7365178

File tree

12 files changed

+228
-14
lines changed

12 files changed

+228
-14
lines changed

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,7 @@
20502050
'Rector\\PHPUnit\\PHPUnit120\\Rector\\ClassMethod\\ExpressionCreateMockToCreateStubRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector.php',
20512051
'Rector\\PHPUnit\\PHPUnit120\\Rector\\Class_\\AllowMockObjectsWithoutExpectationsAttributeRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php',
20522052
'Rector\\PHPUnit\\PHPUnit120\\Rector\\Class_\\AssertIsTypeMethodCallRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector.php',
2053+
'Rector\\PHPUnit\\PHPUnit120\\Rector\\Class_\\PropertyCreateMockToCreateStubRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit120/Rector/Class_/PropertyCreateMockToCreateStubRector.php',
20532054
'Rector\\PHPUnit\\PHPUnit120\\Rector\\Class_\\RemoveOverrideFinalConstructTestCaseRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector.php',
20542055
'Rector\\PHPUnit\\PHPUnit120\\Rector\\MethodCall\\ExplicitMockExpectsCallRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php',
20552056
'Rector\\PHPUnit\\PHPUnit50\\Rector\\StaticCall\\GetMockRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit50/Rector/StaticCall/GetMockRector.php',

vendor/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,6 +2310,7 @@ class ComposerStaticInit43920ef660f86182970f9c36338f7965
23102310
'Rector\\PHPUnit\\PHPUnit120\\Rector\\ClassMethod\\ExpressionCreateMockToCreateStubRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector.php',
23112311
'Rector\\PHPUnit\\PHPUnit120\\Rector\\Class_\\AllowMockObjectsWithoutExpectationsAttributeRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php',
23122312
'Rector\\PHPUnit\\PHPUnit120\\Rector\\Class_\\AssertIsTypeMethodCallRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector.php',
2313+
'Rector\\PHPUnit\\PHPUnit120\\Rector\\Class_\\PropertyCreateMockToCreateStubRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit120/Rector/Class_/PropertyCreateMockToCreateStubRector.php',
23132314
'Rector\\PHPUnit\\PHPUnit120\\Rector\\Class_\\RemoveOverrideFinalConstructTestCaseRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit120/Rector/Class_/RemoveOverrideFinalConstructTestCaseRector.php',
23142315
'Rector\\PHPUnit\\PHPUnit120\\Rector\\MethodCall\\ExplicitMockExpectsCallRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php',
23152316
'Rector\\PHPUnit\\PHPUnit50\\Rector\\StaticCall\\GetMockRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit50/Rector/StaticCall/GetMockRector.php',

vendor/composer/installed.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,12 +1818,12 @@
18181818
"source": {
18191819
"type": "git",
18201820
"url": "https:\/\/github.com\/rectorphp\/rector-phpunit.git",
1821-
"reference": "9f5f19d7af2a45fa701ff1f7b98765719c0629a2"
1821+
"reference": "40a6044f1899b34a727fdb69053f37fd5541554d"
18221822
},
18231823
"dist": {
18241824
"type": "zip",
1825-
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/9f5f19d7af2a45fa701ff1f7b98765719c0629a2",
1826-
"reference": "9f5f19d7af2a45fa701ff1f7b98765719c0629a2",
1825+
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/40a6044f1899b34a727fdb69053f37fd5541554d",
1826+
"reference": "40a6044f1899b34a727fdb69053f37fd5541554d",
18271827
"shasum": ""
18281828
},
18291829
"require": {
@@ -1850,7 +1850,7 @@
18501850
"tomasvotruba\/unused-public": "^2.2",
18511851
"tracy\/tracy": "^2.11"
18521852
},
1853-
"time": "2026-02-03T10:58:22+00:00",
1853+
"time": "2026-02-03T12:41:05+00:00",
18541854
"default-branch": true,
18551855
"type": "rector-extension",
18561856
"extra": {

vendor/composer/installed.php

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

vendor/rector/extension-installer/src/GeneratedConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
final class GeneratedConfig
1111
{
12-
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main 46e4f77'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main a110e2f'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main 9f5f19d'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main 582fc7c'));
12+
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main 46e4f77'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main a110e2f'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main 40a6044'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main 582fc7c'));
1313
private function __construct()
1414
{
1515
}

vendor/rector/rector-phpunit/config/sets/phpunit-code-quality.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector;
5353
use Rector\PHPUnit\CodeQuality\Rector\StmtsAwareInterface\DeclareStrictTypesTestsRector;
5454
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
55+
use Rector\PHPUnit\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector;
5556
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;
5657
use Rector\PHPUnit\PHPUnit60\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
5758
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;
@@ -118,6 +119,7 @@
118119
// stub over mock
119120
CreateStubOverCreateMockArgRector::class,
120121
ExpressionCreateMockToCreateStubRector::class,
122+
PropertyCreateMockToCreateStubRector::class,
121123
// @test first, enable later
122124
// \Rector\PHPUnit\CodeQuality\Rector\Expression\ConfiguredMockEntityToSetterObjectRector::class,
123125
FinalizeTestCaseClassRector::class,

vendor/rector/rector-phpunit/config/sets/phpunit120.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Rector\Config\RectorConfig;
77
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
88
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector;
9+
use Rector\PHPUnit\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector;
910
use Rector\PHPUnit\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector;
1011
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;
1112
return static function (RectorConfig $rectorConfig): void {
@@ -15,5 +16,6 @@
1516
// stubs over mocks
1617
CreateStubOverCreateMockArgRector::class,
1718
ExpressionCreateMockToCreateStubRector::class,
19+
PropertyCreateMockToCreateStubRector::class,
1820
]);
1921
};

vendor/rector/rector-phpunit/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
use PhpParser\Node\Expr;
77
use PhpParser\Node\Expr\MethodCall;
8+
use PhpParser\Node\Expr\PropertyFetch;
89
use PhpParser\Node\Expr\Variable;
10+
use PhpParser\Node\Stmt\Class_;
911
use PhpParser\Node\Stmt\ClassMethod;
1012
use Rector\NodeNameResolver\NodeNameResolver;
1113
use Rector\PhpParser\Node\BetterNodeFinder;
@@ -59,4 +61,21 @@ public function isUsedForMocking(Expr $expr, ClassMethod $classMethod): bool
5961
}
6062
return \false;
6163
}
64+
public function isPropertyUsedForMocking(Class_ $class, string $propertyName): bool
65+
{
66+
// find out, how many are used in call likes as args
67+
/** @var array<Expr\MethodCall> $methodCalls */
68+
$methodCalls = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), [MethodCall::class]);
69+
foreach ($methodCalls as $methodCall) {
70+
if (!$methodCall->var instanceof PropertyFetch) {
71+
continue;
72+
}
73+
$propertyFetch = $methodCall->var;
74+
if ($this->nodeNameResolver->isName($propertyFetch->name, $propertyName)) {
75+
// variable is being called on, most like mocking, lets skip
76+
return \true;
77+
}
78+
}
79+
return \false;
80+
}
6281
}

vendor/rector/rector-phpunit/rules/CodeQuality/NodeAnalyser/MockObjectPropertyDetector.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,63 @@
33
declare (strict_types=1);
44
namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;
55

6+
use PhpParser\Node\Expr\Assign;
7+
use PhpParser\Node\Expr\MethodCall;
8+
use PhpParser\Node\Expr\PropertyFetch;
69
use PhpParser\Node\Name\FullyQualified;
10+
use PhpParser\Node\Stmt\ClassMethod;
11+
use PhpParser\Node\Stmt\Expression;
712
use PhpParser\Node\Stmt\Property;
13+
use Rector\NodeNameResolver\NodeNameResolver;
814
use Rector\PHPUnit\Enum\PHPUnitClassName;
915
final class MockObjectPropertyDetector
1016
{
17+
/**
18+
* @readonly
19+
*/
20+
private NodeNameResolver $nodeNameResolver;
21+
public function __construct(NodeNameResolver $nodeNameResolver)
22+
{
23+
$this->nodeNameResolver = $nodeNameResolver;
24+
}
1125
public function detect(Property $property): bool
1226
{
1327
if (!$property->type instanceof FullyQualified) {
1428
return \false;
1529
}
1630
return $property->type->toString() === PHPUnitClassName::MOCK_OBJECT;
1731
}
32+
/**
33+
* @return array<string, MethodCall>
34+
*/
35+
public function collectFromClassMethod(ClassMethod $classMethod): array
36+
{
37+
$propertyNamesToCreateMockMethodCalls = [];
38+
foreach ((array) $classMethod->stmts as $stmt) {
39+
if (!$stmt instanceof Expression) {
40+
continue;
41+
}
42+
if (!$stmt->expr instanceof Assign) {
43+
continue;
44+
}
45+
$assign = $stmt->expr;
46+
if (!$assign->var instanceof PropertyFetch) {
47+
continue;
48+
}
49+
if (!$assign->expr instanceof MethodCall) {
50+
continue;
51+
}
52+
$methodCall = $assign->expr;
53+
if (!$this->nodeNameResolver->isName($methodCall->name, 'createMock')) {
54+
continue;
55+
}
56+
$propertyFetch = $assign->var;
57+
$propertyName = $this->nodeNameResolver->getName($propertyFetch->name);
58+
if (!is_string($propertyName)) {
59+
continue;
60+
}
61+
$propertyNamesToCreateMockMethodCalls[$propertyName] = $methodCall;
62+
}
63+
return $propertyNamesToCreateMockMethodCalls;
64+
}
1865
}

vendor/rector/rector-phpunit/rules/PHPUnit120/Rector/CallLike/CreateStubOverCreateMockArgRector.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare (strict_types=1);
44
namespace Rector\PHPUnit\PHPUnit120\Rector\CallLike;
55

6+
use PhpParser\Node\Expr\PropertyFetch;
67
use PhpParser\Node;
78
use PhpParser\Node\ArrayItem;
89
use PhpParser\Node\Expr;
@@ -13,9 +14,7 @@
1314
use PhpParser\Node\Identifier;
1415
use PhpParser\Node\Stmt\ClassMethod;
1516
use PhpParser\Node\Stmt\Expression;
16-
use Rector\PHPStan\ScopeFetcher;
1717
use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectExprDetector;
18-
use Rector\PHPUnit\Enum\PHPUnitClassName;
1918
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
2019
use Rector\Rector\AbstractRector;
2120
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -86,12 +85,7 @@ public function getNodeTypes(): array
8685
*/
8786
public function refactor(Node $node)
8887
{
89-
$scope = ScopeFetcher::fetch($node);
90-
if (!$scope->isInClass()) {
91-
return null;
92-
}
93-
$classReflection = $scope->getClassReflection();
94-
if (!$classReflection->is(PHPUnitClassName::TEST_CASE)) {
88+
if (!$this->testsNodeAnalyzer->isInTestClass($node)) {
9589
return null;
9690
}
9791
if ($node instanceof ClassMethod) {
@@ -144,6 +138,10 @@ private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
144138
continue;
145139
}
146140
$assign = $stmt->expr;
141+
// handled in another rule
142+
if ($assign->var instanceof PropertyFetch) {
143+
continue;
144+
}
147145
$createMockMethodCall = $this->matchCreateMockMethodCall($assign->expr);
148146
if (!$createMockMethodCall instanceof MethodCall) {
149147
continue;

0 commit comments

Comments
 (0)