Skip to content

Commit 7e5543b

Browse files
bug #61304 [PhpUnitBridge] Call Reflection*::setAccessible() only for PHP < 8.1 (W0rma)
This PR was merged into the 6.4 branch. Discussion ---------- [PhpUnitBridge] Call Reflection*::setAccessible() only for PHP < 8.1 | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT Follow-up of #61207 There are plans to deprecate `Reflection*::setAccessible()` in PHP 8.5: https://wiki.php.net/rfc/deprecations_php_8_5#extreflection_deprecations Thus, it might be beneficial to only call it if PHP < 8.1 is used. Commits ------- 9e89dd3 Reflection*::setAccessible() has no effect as of PHP 8.1
2 parents 8366098 + 9e89dd3 commit 7e5543b

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

src/Symfony/Bridge/PhpUnit/CoverageListener.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public function startTest(Test $test): void
8686
private function addCoversForClassToAnnotationCache(Test $test, array $covers): void
8787
{
8888
$r = new \ReflectionProperty(TestUtil::class, 'annotationCache');
89-
$r->setAccessible(true);
89+
if (\PHP_VERSION_ID < 80100) {
90+
$r->setAccessible(true);
91+
}
9092

9193
$cache = $r->getValue();
9294
$cache = array_replace_recursive($cache, [
@@ -103,7 +105,9 @@ private function addCoversForDocBlockInsideRegistry(Test $test, array $covers):
103105
$docBlock = Registry::getInstance()->forClassName(\get_class($test));
104106

105107
$symbolAnnotations = new \ReflectionProperty($docBlock, 'symbolAnnotations');
106-
$symbolAnnotations->setAccessible(true);
108+
if (\PHP_VERSION_ID < 80100) {
109+
$symbolAnnotations->setAccessible(true);
110+
}
107111

108112
// Exclude internal classes; PHPUnit 9.1+ is picky about tests covering, say, a \RuntimeException
109113
$covers = array_filter($covers, function (string $class) {

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,9 @@ public function toString()
437437
{
438438
$exception = new \Exception($this->message);
439439
$reflection = new \ReflectionProperty($exception, 'trace');
440-
$reflection->setAccessible(true);
440+
if (\PHP_VERSION_ID < 80100) {
441+
$reflection->setAccessible(true);
442+
}
441443
$reflection->setValue($exception, $this->trace);
442444

443445
return ($this->originatesFromAnObject() ? 'deprecation triggered by '.$this->originatingClass().'::'.$this->originatingMethod().":\n" : '')

src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,9 @@ private function willBeIsolated(TestCase $test): bool
357357
}
358358

359359
$r = new \ReflectionProperty($test, 'runTestInSeparateProcess');
360-
$r->setAccessible(true);
360+
if (\PHP_VERSION_ID < 80100) {
361+
$r->setAccessible(true);
362+
}
361363

362364
return $r->getValue($test) ?? false;
363365
}

src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/DeprecationTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ public static function setUpBeforeClass(): void
275275
$loader = require $v.'/autoload.php';
276276
$reflection = new \ReflectionClass($loader);
277277
$prop = $reflection->getProperty('prefixDirsPsr4');
278-
$prop->setAccessible(true);
278+
if (\PHP_VERSION_ID < 80100) {
279+
$prop->setAccessible(true);
280+
}
279281
$currentValue = $prop->getValue($loader);
280282
self::$prefixDirsPsr4[] = [$prop, $loader, $currentValue];
281283
$currentValue['Symfony\\Bridge\\PhpUnit\\'] = [realpath(__DIR__.'/../..')];

0 commit comments

Comments
 (0)