From 5f69f0faa22712ac5b5499661f19a10339f80ff1 Mon Sep 17 00:00:00 2001 From: NickSdot Date: Wed, 23 Jul 2025 01:48:30 +0700 Subject: [PATCH] [Form][PhpUnitBridge] Remove usage of noop `ReflectionProperty::setAccessible()` --- src/Symfony/Bridge/PhpUnit/CoverageListener.php | 2 -- .../Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php | 1 - .../Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php | 1 - .../PhpUnit/Tests/DeprecationErrorHandler/DeprecationTest.php | 1 - .../Component/DependencyInjection/Tests/ContainerTest.php | 2 -- src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php | 1 - .../NumberToLocalizedStringTransformerTest.php | 1 - .../Mailer/Tests/Transport/SendmailTransportTest.php | 4 ---- 8 files changed, 13 deletions(-) diff --git a/src/Symfony/Bridge/PhpUnit/CoverageListener.php b/src/Symfony/Bridge/PhpUnit/CoverageListener.php index 65d6aa9dc9dcc..e9fea920dd215 100644 --- a/src/Symfony/Bridge/PhpUnit/CoverageListener.php +++ b/src/Symfony/Bridge/PhpUnit/CoverageListener.php @@ -86,7 +86,6 @@ public function startTest(Test $test): void private function addCoversForClassToAnnotationCache(Test $test, array $covers): void { $r = new \ReflectionProperty(TestUtil::class, 'annotationCache'); - $r->setAccessible(true); $cache = $r->getValue(); $cache = array_replace_recursive($cache, [ @@ -103,7 +102,6 @@ private function addCoversForDocBlockInsideRegistry(Test $test, array $covers): $docBlock = Registry::getInstance()->forClassName(\get_class($test)); $symbolAnnotations = new \ReflectionProperty($docBlock, 'symbolAnnotations'); - $symbolAnnotations->setAccessible(true); // Exclude internal classes; PHPUnit 9.1+ is picky about tests covering, say, a \RuntimeException $covers = array_filter($covers, function (string $class) { diff --git a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php index f7a57f5704dae..710ef6088ddbb 100644 --- a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php +++ b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php @@ -437,7 +437,6 @@ public function toString() { $exception = new \Exception($this->message); $reflection = new \ReflectionProperty($exception, 'trace'); - $reflection->setAccessible(true); $reflection->setValue($exception, $this->trace); return ($this->originatesFromAnObject() ? 'deprecation triggered by '.$this->originatingClass().'::'.$this->originatingMethod().":\n" : '') diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php b/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php index 2b45051e83d74..d058d9b9cc7c3 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php @@ -357,7 +357,6 @@ private function willBeIsolated(TestCase $test): bool } $r = new \ReflectionProperty($test, 'runTestInSeparateProcess'); - $r->setAccessible(true); return $r->getValue($test) ?? false; } diff --git a/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/DeprecationTest.php b/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/DeprecationTest.php index 4c17a806b4281..85e0104ce66a3 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/DeprecationTest.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/DeprecationTest.php @@ -275,7 +275,6 @@ public static function setUpBeforeClass(): void $loader = require $v.'/autoload.php'; $reflection = new \ReflectionClass($loader); $prop = $reflection->getProperty('prefixDirsPsr4'); - $prop->setAccessible(true); $currentValue = $prop->getValue($loader); self::$prefixDirsPsr4[] = [$prop, $loader, $currentValue]; $currentValue['Symfony\\Bridge\\PhpUnit\\'] = [realpath(__DIR__.'/../..')]; diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index 4cde971948693..65716cef30006 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -420,7 +420,6 @@ public function testGetEnvDoesNotAutoCastNullWithDefaultEnvVarProcessor() $container->compile(); $r = new \ReflectionMethod($container, 'getEnv'); - $r->setAccessible(true); $this->assertNull($r->invoke($container, 'FOO')); } @@ -436,7 +435,6 @@ public function testGetEnvDoesNotAutoCastNullWithEnvVarProcessorsLocatorReturnin $container->compile(); $r = new \ReflectionMethod($container, 'getEnv'); - $r->setAccessible(true); $this->assertNull($r->invoke($container, 'FOO')); } } diff --git a/src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php b/src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php index c4cc5b738cb40..48ddefa5c206b 100644 --- a/src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php +++ b/src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php @@ -34,7 +34,6 @@ class ErrorHandlerTest extends TestCase protected function tearDown(): void { $r = new \ReflectionProperty(ErrorHandler::class, 'exitCode'); - $r->setAccessible(true); $r->setValue(null, 0); } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php index 261fcf136c040..fbb1030457ce3 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php @@ -754,7 +754,6 @@ public function testRoundMethodKeepsIntegersAsIntegers() // Use reflection to test the private round() method directly $reflection = new \ReflectionClass($transformer); $roundMethod = $reflection->getMethod('round'); - $roundMethod->setAccessible(true); $int = \PHP_INT_MAX - 1; $result = $roundMethod->invoke($transformer, $int); diff --git a/src/Symfony/Component/Mailer/Tests/Transport/SendmailTransportTest.php b/src/Symfony/Component/Mailer/Tests/Transport/SendmailTransportTest.php index cb748e2ee4ef3..057f9c5e69c5e 100644 --- a/src/Symfony/Component/Mailer/Tests/Transport/SendmailTransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/Transport/SendmailTransportTest.php @@ -94,7 +94,6 @@ public function testThrowsTransportExceptionOnFailure() $sendmailTransport->send($mail, $envelope); $streamProperty = new \ReflectionProperty(SendmailTransport::class, 'stream'); - $streamProperty->setAccessible(true); $stream = $streamProperty->getValue($sendmailTransport); $this->assertNull($stream->stream); } @@ -112,10 +111,8 @@ public function testStreamIsClearedOnFailure() } $streamProperty = new \ReflectionProperty(SendmailTransport::class, 'stream'); - $streamProperty->setAccessible(true); $stream = $streamProperty->getValue($sendmailTransport); $innerStreamProperty = new \ReflectionProperty(ProcessStream::class, 'stream'); - $innerStreamProperty->setAccessible(true); $this->assertNull($innerStreamProperty->getValue($stream)); } @@ -127,7 +124,6 @@ public function testDoesNotThrowWhenInteractive() $sendmailTransport = new SendmailTransport(self::FAKE_INTERACTIVE_SENDMAIL); $transportProperty = new \ReflectionProperty(SendmailTransport::class, 'transport'); - $transportProperty->setAccessible(true); // Replace the transport with an anonymous consumer that trigger the stream methods $transportProperty->setValue($sendmailTransport, new class($transportProperty->getValue($sendmailTransport)->getStream()) extends SmtpTransport {