Skip to content

Commit c243796

Browse files
committed
Updated Rector to commit 1233b635eb3decbeb5692bbd7a04ae3a74cf0162
rectorphp/rector-src@1233b63 [Kaizen] Save kaizen applied rules into cache to make works on parallel (#7046)
1 parent 5b88892 commit c243796

File tree

13 files changed

+116
-42
lines changed

13 files changed

+116
-42
lines changed

src/Application/ApplicationFileProcessor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ public function run(Configuration $configuration, InputInterface $input) : Proce
128128
} else {
129129
$preFileCallback = null;
130130
}
131+
// on repetitive runs, the counter of kaizen must start from 0 again
132+
if ($configuration->isKaizenEnabled()) {
133+
$this->kaizenStepper->start();
134+
}
131135
if ($configuration->isParallel()) {
132136
$processResult = $this->runParallel($filePaths, $input, $postFileCallback);
133137
} else {

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 = '162fb378528f2f799f66bed5f375614f92176251';
22+
public const PACKAGE_VERSION = '1233b635eb3decbeb5692bbd7a04ae3a74cf0162';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2025-07-07 10:26:37';
27+
public const RELEASE_DATE = '2025-07-08 00:11:29';
2828
/**
2929
* @var int
3030
*/
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\Caching\Detector;
5+
6+
use Rector\Contract\Rector\RectorInterface;
7+
use Rector\Caching\Cache;
8+
use Rector\Caching\Enum\CacheKey;
9+
use Rector\Exception\ShouldNotHappenException;
10+
use Rector\Util\FileHasher;
11+
final class KaizenRulesDetector
12+
{
13+
/**
14+
* @readonly
15+
*/
16+
private Cache $cache;
17+
/**
18+
* @readonly
19+
*/
20+
private FileHasher $fileHasher;
21+
public function __construct(Cache $cache, FileHasher $fileHasher)
22+
{
23+
$this->cache = $cache;
24+
$this->fileHasher = $fileHasher;
25+
}
26+
private function getCacheKey() : string
27+
{
28+
return CacheKey::KAIZEN_RULES . '_' . $this->fileHasher->hash(\getcwd());
29+
}
30+
public function clean() : void
31+
{
32+
$this->cache->clean(CacheKey::KAIZEN_RULES);
33+
}
34+
public function addRule(string $rectorClass) : void
35+
{
36+
$cachedValue = $this->loadRules();
37+
$appliedRectorClasses = \array_unique(\array_merge($cachedValue, [$rectorClass]));
38+
$this->cache->save($this->getCacheKey(), CacheKey::KAIZEN_RULES, $appliedRectorClasses);
39+
}
40+
/**
41+
* @return array<class-string<RectorInterface>>
42+
*/
43+
public function loadRules() : array
44+
{
45+
$key = $this->getCacheKey();
46+
$rules = $this->cache->load($key, CacheKey::KAIZEN_RULES) ?? [];
47+
if (!\is_array($rules)) {
48+
throw new ShouldNotHappenException();
49+
}
50+
return \array_unique($rules);
51+
}
52+
}

src/Caching/Enum/CacheKey.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ final class CacheKey
1616
* @var string
1717
*/
1818
public const FILE_HASH_KEY = 'file_hash';
19+
/**
20+
* @var string
21+
*/
22+
public const KAIZEN_RULES = 'kaizen_rules';
1923
}

src/Configuration/KaizenStepper.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33
declare (strict_types=1);
44
namespace Rector\Configuration;
55

6+
use Rector\Caching\Detector\KaizenRulesDetector;
67
use Rector\Contract\Rector\RectorInterface;
78
final class KaizenStepper
89
{
910
/**
10-
* @var positive-int|null
11+
* @readonly
1112
*/
12-
private ?int $stepCount = null;
13+
private KaizenRulesDetector $kaizenRulesDetector;
1314
/**
14-
* @var array<class-string<RectorInterface>>
15+
* @var positive-int|null
1516
*/
16-
private array $appliedRectorClasses = [];
17+
private ?int $stepCount = null;
18+
public function __construct(KaizenRulesDetector $kaizenRulesDetector)
19+
{
20+
$this->kaizenRulesDetector = $kaizenRulesDetector;
21+
}
22+
public function start() : void
23+
{
24+
$this->kaizenRulesDetector->clean();
25+
}
1726
/**
1827
* @param positive-int $stepCount
1928
*/
@@ -30,16 +39,16 @@ public function enabled() : bool
3039
*/
3140
public function recordAppliedRule(string $rectorClass) : void
3241
{
33-
$this->appliedRectorClasses[] = $rectorClass;
42+
$this->kaizenRulesDetector->addRule($rectorClass);
3443
}
3544
public function shouldKeepImproving(string $rectorClass) : bool
3645
{
46+
$appliedRectorClasses = $this->kaizenRulesDetector->loadRules();
3747
// is rule already in applied rules? keep going
38-
$uniqueAppliedRectorClasses = \array_unique($this->appliedRectorClasses);
39-
if (\in_array($rectorClass, $uniqueAppliedRectorClasses)) {
48+
if (\in_array($rectorClass, $appliedRectorClasses)) {
4049
return \true;
4150
}
4251
// make sure we made enough changes
43-
return \count($uniqueAppliedRectorClasses) < $this->stepCount;
52+
return \count($appliedRectorClasses) < $this->stepCount;
4453
}
4554
}

src/Rector/AbstractRector.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -117,30 +117,6 @@ public final function enterNode(Node $node)
117117
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node;
118118
NodeAttributeReIndexer::reIndexNodeAttributes($node);
119119
$refactoredNode = $this->refactor($node);
120-
// take it step by step
121-
if ($refactoredNode !== null && $this->kaizenStepper->enabled()) {
122-
$this->kaizenStepper->recordAppliedRule(static::class);
123-
}
124-
// @see NodeTraverser::* codes, e.g. removal of node of stopping the traversing
125-
if ($refactoredNode === NodeVisitor::REMOVE_NODE) {
126-
// log here, so we can remove the node in leaveNode() method
127-
$this->toBeRemovedNodeId = \spl_object_id($originalNode);
128-
// notify this rule changing code
129-
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
130-
$this->file->addRectorClassWithLine($rectorWithLineChange);
131-
return $originalNode;
132-
}
133-
if (\is_int($refactoredNode)) {
134-
$this->createdByRuleDecorator->decorate($node, $originalNode, static::class);
135-
if (!\in_array($refactoredNode, [NodeVisitor::DONT_TRAVERSE_CHILDREN, NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN], \true)) {
136-
// notify this rule changing code
137-
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
138-
$this->file->addRectorClassWithLine($rectorWithLineChange);
139-
return $refactoredNode;
140-
}
141-
$this->decorateCurrentAndChildren($node);
142-
return null;
143-
}
144120
// nothing to change → continue
145121
if ($refactoredNode === null) {
146122
return null;
@@ -149,6 +125,33 @@ public final function enterNode(Node $node)
149125
$errorMessage = \sprintf(self::EMPTY_NODE_ARRAY_MESSAGE, static::class);
150126
throw new ShouldNotHappenException($errorMessage);
151127
}
128+
$isIntRefactoredNode = \is_int($refactoredNode);
129+
/**
130+
* If below node and/or its children not traversed on current rule
131+
* early return null with decorate current and children node with skipped by "only" current rule
132+
*/
133+
if ($isIntRefactoredNode) {
134+
$this->createdByRuleDecorator->decorate($node, $originalNode, static::class);
135+
if (\in_array($refactoredNode, [NodeVisitor::DONT_TRAVERSE_CHILDREN, NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN], \true)) {
136+
$this->decorateCurrentAndChildren($node);
137+
return null;
138+
}
139+
}
140+
// take it step by step
141+
if ($this->kaizenStepper->enabled()) {
142+
$this->kaizenStepper->recordAppliedRule(static::class);
143+
}
144+
if ($isIntRefactoredNode) {
145+
// @see NodeTraverser::* codes, e.g. removal of node of stopping the traversing
146+
if ($refactoredNode === NodeVisitor::REMOVE_NODE) {
147+
// log here, so we can remove the node in leaveNode() method
148+
$this->toBeRemovedNodeId = \spl_object_id($originalNode);
149+
}
150+
// notify this rule changing code
151+
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
152+
$this->file->addRectorClassWithLine($rectorWithLineChange);
153+
return $refactoredNode === NodeVisitor::REMOVE_NODE ? $originalNode : $refactoredNode;
154+
}
152155
return $this->postRefactorProcess($originalNode, $node, $refactoredNode, $filePath);
153156
}
154157
/**

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,7 @@
10861086
'Rector\\Caching\\Config\\FileHashComputer' => $baseDir . '/src/Caching/Config/FileHashComputer.php',
10871087
'Rector\\Caching\\Contract\\ValueObject\\Storage\\CacheStorageInterface' => $baseDir . '/src/Caching/Contract/ValueObject/Storage/CacheStorageInterface.php',
10881088
'Rector\\Caching\\Detector\\ChangedFilesDetector' => $baseDir . '/src/Caching/Detector/ChangedFilesDetector.php',
1089+
'Rector\\Caching\\Detector\\KaizenRulesDetector' => $baseDir . '/src/Caching/Detector/KaizenRulesDetector.php',
10891090
'Rector\\Caching\\Enum\\CacheKey' => $baseDir . '/src/Caching/Enum/CacheKey.php',
10901091
'Rector\\Caching\\UnchangedFilesFilter' => $baseDir . '/src/Caching/UnchangedFilesFilter.php',
10911092
'Rector\\Caching\\ValueObject\\CacheFilePaths' => $baseDir . '/src/Caching/ValueObject/CacheFilePaths.php',

vendor/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,7 @@ class ComposerStaticInitdb65f1617bfddbb41dc0dee4425e5c9b
13051305
'Rector\\Caching\\Config\\FileHashComputer' => __DIR__ . '/../..' . '/src/Caching/Config/FileHashComputer.php',
13061306
'Rector\\Caching\\Contract\\ValueObject\\Storage\\CacheStorageInterface' => __DIR__ . '/../..' . '/src/Caching/Contract/ValueObject/Storage/CacheStorageInterface.php',
13071307
'Rector\\Caching\\Detector\\ChangedFilesDetector' => __DIR__ . '/../..' . '/src/Caching/Detector/ChangedFilesDetector.php',
1308+
'Rector\\Caching\\Detector\\KaizenRulesDetector' => __DIR__ . '/../..' . '/src/Caching/Detector/KaizenRulesDetector.php',
13081309
'Rector\\Caching\\Enum\\CacheKey' => __DIR__ . '/../..' . '/src/Caching/Enum/CacheKey.php',
13091310
'Rector\\Caching\\UnchangedFilesFilter' => __DIR__ . '/../..' . '/src/Caching/UnchangedFilesFilter.php',
13101311
'Rector\\Caching\\ValueObject\\CacheFilePaths' => __DIR__ . '/../..' . '/src/Caching/ValueObject/CacheFilePaths.php',

vendor/composer/installed.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,12 +1866,12 @@
18661866
"source": {
18671867
"type": "git",
18681868
"url": "https:\/\/github.com\/rectorphp\/rector-symfony.git",
1869-
"reference": "b2de1d56f528ff2c70921357a641fc0cd5c2fc9e"
1869+
"reference": "564999eb1cadcb99eac82192f1166c3de3d7fddf"
18701870
},
18711871
"dist": {
18721872
"type": "zip",
1873-
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/b2de1d56f528ff2c70921357a641fc0cd5c2fc9e",
1874-
"reference": "b2de1d56f528ff2c70921357a641fc0cd5c2fc9e",
1873+
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/564999eb1cadcb99eac82192f1166c3de3d7fddf",
1874+
"reference": "564999eb1cadcb99eac82192f1166c3de3d7fddf",
18751875
"shasum": ""
18761876
},
18771877
"require": {
@@ -1900,7 +1900,7 @@
19001900
"tomasvotruba\/unused-public": "^2.0",
19011901
"tracy\/tracy": "^2.10"
19021902
},
1903-
"time": "2025-07-04T14:11:34+00:00",
1903+
"time": "2025-07-07T17:10:08+00:00",
19041904
"default-branch": true,
19051905
"type": "rector-extension",
19061906
"extra": {

0 commit comments

Comments
 (0)