Skip to content

Commit 16d43af

Browse files
authored
[Turbo] Fix crash cache due to PHPStan turbo (#8850)
1 parent b256695 commit 16d43af

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎src/Configuration/Parameter/SimpleParameterProvider.php‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public static function hashForCacheInvalidation(): string
175175

176176
ksort($strictParameters);
177177

178-
return sha1(serialize($strictParameters));
178+
return sha1(serialize(self::canonicalizeObjects($strictParameters, [])));
179179
}
180180

181181
/**
@@ -231,6 +231,42 @@ private static function relativizeProjectPaths(array $parameters, string $projec
231231
return $parameters;
232232
}
233233

234+
/**
235+
* serialize() writes object properties in declaration order, which differs for the same class
236+
* between runtimes: PHPStan Turbo replaces e.g. PHPStan\Type\MixedType with a native class
237+
* declaring its properties in another order. The main process and the Turbo workers would then
238+
* hash the same configuration differently and keep clearing each other's cache.
239+
*
240+
* @param array<int, true> $visitedObjectIds
241+
*/
242+
private static function canonicalizeObjects(mixed $value, array $visitedObjectIds): mixed
243+
{
244+
if (is_array($value)) {
245+
return array_map(
246+
static fn (mixed $item): mixed => self::canonicalizeObjects($item, $visitedObjectIds),
247+
$value
248+
);
249+
}
250+
251+
if (! is_object($value)) {
252+
return $value;
253+
}
254+
255+
$objectId = spl_object_id($value);
256+
if (isset($visitedObjectIds[$objectId])) {
257+
return $value::class;
258+
}
259+
260+
$visitedObjectIds[$objectId] = true;
261+
262+
$properties = (array) $value;
263+
ksort($properties);
264+
265+
return [
266+
$value::class => self::canonicalizeObjects($properties, $visitedObjectIds),
267+
];
268+
}
269+
234270
/**
235271
* Normalized to "/" separators, so the prefix compares alike with config paths on every platform.
236272
* Empty when the working directory cannot be resolved, which makes the relativizing above a

‎tests/Configuration/ConfigurationFactoryTest.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Rector\Console\ProcessConfigureDecorator;
1111
use Rector\FileSystem\FilesFinder;
1212
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
13+
use stdClass;
1314
use Symfony\Component\Console\Command\Command;
1415
use Symfony\Component\Console\Input\ArrayInput;
1516

@@ -20,6 +21,7 @@ protected function tearDown(): void
2021
SimpleParameterProvider::setParameter(Option::IS_RUN_NARROWED, false);
2122
SimpleParameterProvider::setParameter(Option::SOURCE, []);
2223
SimpleParameterProvider::setParameter(Option::PATHS, []);
24+
SimpleParameterProvider::setParameter(Option::RULE_CONFIGURATIONS, []);
2325
}
2426

2527
public function test(): void
@@ -93,6 +95,33 @@ public function testSourceParameterIsOutsideTheCacheInvalidationHash(): void
9395
$this->assertSame($hashForOnePath, SimpleParameterProvider::hashForCacheInvalidation());
9496
}
9597

98+
public function testObjectPropertyOrderIsOutsideTheCacheInvalidationHash(): void
99+
{
100+
// PHPStan Turbo workers declare e.g. MixedType properties in another order than the main process,
101+
// the same configured value must hash alike in both
102+
$firstOrder = new stdClass();
103+
$firstOrder->isExplicitMixed = false;
104+
$firstOrder->subtractedType = null;
105+
106+
SimpleParameterProvider::setParameter(Option::RULE_CONFIGURATIONS, [
107+
'SomeRector' => [$firstOrder],
108+
]);
109+
$firstOrderHash = SimpleParameterProvider::hashForCacheInvalidation();
110+
111+
$secondOrder = new stdClass();
112+
$secondOrder->subtractedType = null;
113+
$secondOrder->isExplicitMixed = false;
114+
115+
SimpleParameterProvider::setParameter(Option::RULE_CONFIGURATIONS, [
116+
'SomeRector' => [$secondOrder],
117+
]);
118+
119+
$this->assertSame($firstOrderHash, SimpleParameterProvider::hashForCacheInvalidation());
120+
121+
$secondOrder->isExplicitMixed = true;
122+
$this->assertNotSame($firstOrderHash, SimpleParameterProvider::hashForCacheInvalidation());
123+
}
124+
96125
/**
97126
* @param string[] $paths
98127
*/

0 commit comments

Comments
 (0)