From ca12fee3698f536b4bdc229e06abe4a9f6c2ceb5 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 9 Aug 2025 23:00:01 +0200 Subject: [PATCH] PHP 8.5 | Tests: prevent deprecation notice for Reflection*::setAccessible() Since PHP 8.1, calling the `Reflection*::setAccessible()` methods is no longer necessary as reflected properties/methods/etc will always be accessible. However, the method calls are still needed for PHP < 8.1. As of PHP 8.5, calling the `Reflection*::setAccessible()` methods is now formally deprecated and will yield a deprecation notice, which will fail test runs. As of PHP 9.0, the `setAccessible()` method(s) will be removed. With the latter in mind, this commit prevents the deprecation notice by making the calls to `setAccessible()` conditional. Silencing the deprecation would mean, this would need to be "fixed" again come PHP 9.0, while the current solution should be stable, including for PHP 9.0. Ref: https://wiki.php.net/rfc/deprecations_php_8_5#extreflection_deprecations --- tests/ConfigDouble.php | 7 +- .../Config/AbstractRealConfigTestCase.php | 4 +- tests/Core/Filters/GitModifiedTest.php | 2 +- tests/Core/Filters/GitStagedTest.php | 2 +- .../Ruleset/DisplayCachedMessagesTest.php | 8 +- .../Ruleset/PopulateTokenListenersTest.php | 4 +- .../Tokenizers/AbstractTokenizerTestCase.php | 4 +- tests/Core/Util/Help/HelpTest.php | 139 ++++++++++-------- 8 files changed, 97 insertions(+), 73 deletions(-) diff --git a/tests/ConfigDouble.php b/tests/ConfigDouble.php index 8ebce05858..ed02389af8 100644 --- a/tests/ConfigDouble.php +++ b/tests/ConfigDouble.php @@ -185,7 +185,8 @@ private function preventAutoDiscoveryScreenWidth() private function getStaticConfigProperty($name) { $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); - $property->setAccessible(true); + (PHP_VERSION_ID < 80100) && $property->setAccessible(true); + return $property->getValue(); }//end getStaticConfigProperty() @@ -202,9 +203,9 @@ private function getStaticConfigProperty($name) private function setStaticConfigProperty($name, $value) { $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); - $property->setAccessible(true); + (PHP_VERSION_ID < 80100) && $property->setAccessible(true); $property->setValue(null, $value); - $property->setAccessible(false); + (PHP_VERSION_ID < 80100) && $property->setAccessible(false); }//end setStaticConfigProperty() diff --git a/tests/Core/Config/AbstractRealConfigTestCase.php b/tests/Core/Config/AbstractRealConfigTestCase.php index 6f5cd88443..5d569bb38f 100644 --- a/tests/Core/Config/AbstractRealConfigTestCase.php +++ b/tests/Core/Config/AbstractRealConfigTestCase.php @@ -82,9 +82,9 @@ public static function resetConfigToDefaults() protected static function setStaticConfigProperty($name, $value) { $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); - $property->setAccessible(true); + (PHP_VERSION_ID < 80100) && $property->setAccessible(true); $property->setValue(null, $value); - $property->setAccessible(false); + (PHP_VERSION_ID < 80100) && $property->setAccessible(false); }//end setStaticConfigProperty() diff --git a/tests/Core/Filters/GitModifiedTest.php b/tests/Core/Filters/GitModifiedTest.php index c0b6521264..0a4afe3a7a 100644 --- a/tests/Core/Filters/GitModifiedTest.php +++ b/tests/Core/Filters/GitModifiedTest.php @@ -228,7 +228,7 @@ public function testExecAlwaysReturnsArray($cmd, $expected) $filter = new GitModified($fakeDI, '/', self::$config, self::$ruleset); $reflMethod = new ReflectionMethod($filter, 'exec'); - $reflMethod->setAccessible(true); + (PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true); $result = $reflMethod->invoke($filter, $cmd); $this->assertSame($expected, $result); diff --git a/tests/Core/Filters/GitStagedTest.php b/tests/Core/Filters/GitStagedTest.php index 0c936dedd9..481d4dfd7f 100644 --- a/tests/Core/Filters/GitStagedTest.php +++ b/tests/Core/Filters/GitStagedTest.php @@ -228,7 +228,7 @@ public function testExecAlwaysReturnsArray($cmd, $expected) $filter = new GitStaged($fakeDI, '/', self::$config, self::$ruleset); $reflMethod = new ReflectionMethod($filter, 'exec'); - $reflMethod->setAccessible(true); + (PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true); $result = $reflMethod->invoke($filter, $cmd); $this->assertSame($expected, $result); diff --git a/tests/Core/Ruleset/DisplayCachedMessagesTest.php b/tests/Core/Ruleset/DisplayCachedMessagesTest.php index 55b0b7081b..f58aefe8ec 100644 --- a/tests/Core/Ruleset/DisplayCachedMessagesTest.php +++ b/tests/Core/Ruleset/DisplayCachedMessagesTest.php @@ -280,14 +280,14 @@ private function getPlainRuleset() private function mockCachedMessages(Ruleset $ruleset, $messages) { $reflProperty = new ReflectionProperty($ruleset, 'msgCache'); - $reflProperty->setAccessible(true); + (PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(true); $msgCache = $reflProperty->getValue($ruleset); foreach ($messages as $msg => $type) { $msgCache->add($msg, $type); } - $reflProperty->setAccessible(false); + (PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(false); }//end mockCachedMessages() @@ -302,9 +302,9 @@ private function mockCachedMessages(Ruleset $ruleset, $messages) private function invokeDisplayCachedMessages(Ruleset $ruleset) { $reflMethod = new ReflectionMethod($ruleset, 'displayCachedMessages'); - $reflMethod->setAccessible(true); + (PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true); $reflMethod->invoke($ruleset); - $reflMethod->setAccessible(false); + (PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(false); }//end invokeDisplayCachedMessages() diff --git a/tests/Core/Ruleset/PopulateTokenListenersTest.php b/tests/Core/Ruleset/PopulateTokenListenersTest.php index 347ffddee7..54729ed394 100644 --- a/tests/Core/Ruleset/PopulateTokenListenersTest.php +++ b/tests/Core/Ruleset/PopulateTokenListenersTest.php @@ -172,9 +172,9 @@ public static function dataSniffListensToTokenss() public function testRegistersWhenADeprecatedSniffIsLoaded() { $property = new ReflectionProperty(self::$ruleset, 'deprecatedSniffs'); - $property->setAccessible(true); + (PHP_VERSION_ID < 80100) && $property->setAccessible(true); $actualValue = $property->getValue(self::$ruleset); - $property->setAccessible(false); + (PHP_VERSION_ID < 80100) && $property->setAccessible(false); // Only verify there is one deprecated sniff registered. // There are other tests which test the deprecated sniff handling in more detail. diff --git a/tests/Core/Tokenizers/AbstractTokenizerTestCase.php b/tests/Core/Tokenizers/AbstractTokenizerTestCase.php index f3daf2bf21..aee3aa6021 100644 --- a/tests/Core/Tokenizers/AbstractTokenizerTestCase.php +++ b/tests/Core/Tokenizers/AbstractTokenizerTestCase.php @@ -137,9 +137,9 @@ protected function getTargetToken($commentString, $tokenType, $tokenContent=null public static function clearResolvedTokensCache() { $property = new ReflectionProperty('PHP_CodeSniffer\Tokenizers\PHP', 'resolveTokenCache'); - $property->setAccessible(true); + (PHP_VERSION_ID < 80100) && $property->setAccessible(true); $property->setValue(null, []); - $property->setAccessible(false); + (PHP_VERSION_ID < 80100) && $property->setAccessible(false); }//end clearResolvedTokensCache() diff --git a/tests/Core/Util/Help/HelpTest.php b/tests/Core/Util/Help/HelpTest.php index b3d51a5c78..d6165e22d6 100644 --- a/tests/Core/Util/Help/HelpTest.php +++ b/tests/Core/Util/Help/HelpTest.php @@ -37,12 +37,8 @@ final class HelpTest extends TestCase */ public function testQaArgumentNamesAreWithinAcceptableBounds() { - $help = new Help(new ConfigDouble(), []); - - $reflMethod = new ReflectionMethod($help, 'getAllOptions'); - $reflMethod->setAccessible(true); - $allOptions = $reflMethod->invoke($help); - $reflMethod->setAccessible(false); + $help = new Help(new ConfigDouble(), []); + $allOptions = $this->invokeReflectionMethod($help, 'getAllOptions'); $this->assertGreaterThan(0, count($allOptions), 'No categories found'); @@ -81,12 +77,8 @@ public function testQaArgumentNamesAreWithinAcceptableBounds() */ public function testQaValidCategoryOptionDefinitions() { - $help = new Help(new ConfigDouble(), []); - - $reflMethod = new ReflectionMethod($help, 'getAllOptions'); - $reflMethod->setAccessible(true); - $allOptions = $reflMethod->invoke($help); - $reflMethod->setAccessible(false); + $help = new Help(new ConfigDouble(), []); + $allOptions = $this->invokeReflectionMethod($help, 'getAllOptions'); $this->assertGreaterThan(0, count($allOptions), 'No categories found'); @@ -185,10 +177,7 @@ public function testOptionFiltering($longOptions, $shortOptions, $expected) { $help = new Help(new ConfigDouble(), $longOptions, $shortOptions); - $reflProperty = new ReflectionProperty($help, 'activeOptions'); - $reflProperty->setAccessible(true); - $activeOptions = $reflProperty->getValue($help); - $reflProperty->setAccessible(false); + $activeOptions = $this->getReflectionProperty($help, 'activeOptions'); // Simplify the value to make it comparible. foreach ($activeOptions as $category => $options) { @@ -324,10 +313,7 @@ public function testOptionFilteringSpacerHandling($longOptions, $shortOptions) { $help = new Help(new ConfigDouble(), $longOptions, $shortOptions); - $reflProperty = new ReflectionProperty($help, 'activeOptions'); - $reflProperty->setAccessible(true); - $activeOptions = $reflProperty->getValue($help); - $reflProperty->setAccessible(false); + $activeOptions = $this->getReflectionProperty($help, 'activeOptions'); $this->assertNotEmpty($activeOptions, 'Active options is empty, test is invalid'); @@ -493,10 +479,7 @@ public function testReportWidthCalculations($reportWidth, $longOptions, $expecte $config = new ConfigDouble(["--report-width=$reportWidth", '--no-colors']); $help = new Help($config, $longOptions); - $reflMethod = new ReflectionMethod($help, 'printCategories'); - $reflMethod->setAccessible(true); - $reflMethod->invoke($help); - $reflMethod->setAccessible(false); + $this->invokeReflectionMethod($help, 'printCategories'); $this->expectOutputString($expectedOutput); @@ -572,12 +555,8 @@ public static function dataReportWidthCalculations() */ public function testColorizeVariableInput($input, $expected) { - $help = new Help(new ConfigDouble(), []); - - $reflMethod = new ReflectionMethod($help, 'colorizeVariableInput'); - $reflMethod->setAccessible(true); - $result = $reflMethod->invoke($help, $input); - $reflMethod->setAccessible(false); + $help = new Help(new ConfigDouble(), []); + $result = $this->invokeReflectionMethod($help, 'colorizeVariableInput', $input); $this->assertSame($expected, $result); @@ -640,20 +619,9 @@ public function testPrintCategoryOptionsNoColor($input, $expectedRegex) $config = new ConfigDouble(['--no-colors']); $help = new Help($config, []); - $reflProperty = new ReflectionProperty($help, 'activeOptions'); - $reflProperty->setAccessible(true); - $reflProperty->setValue($help, ['cat' => $input]); - $reflProperty->setAccessible(false); - - $reflMethod = new ReflectionMethod($help, 'setMaxOptionNameLength'); - $reflMethod->setAccessible(true); - $reflMethod->invoke($help); - $reflMethod->setAccessible(false); - - $reflMethod = new ReflectionMethod($help, 'printCategoryOptions'); - $reflMethod->setAccessible(true); - $reflMethod->invoke($help, $input); - $reflMethod->setAccessible(false); + $this->setReflectionProperty($help, 'activeOptions', ['cat' => $input]); + $this->invokeReflectionMethod($help, 'setMaxOptionNameLength'); + $this->invokeReflectionMethod($help, 'printCategoryOptions', $input); $this->expectOutputRegex($expectedRegex['no-color']); @@ -675,20 +643,9 @@ public function testPrintCategoryOptionsColor($input, $expectedRegex) $config = new ConfigDouble(['--colors']); $help = new Help($config, []); - $reflProperty = new ReflectionProperty($help, 'activeOptions'); - $reflProperty->setAccessible(true); - $reflProperty->setValue($help, ['cat' => $input]); - $reflProperty->setAccessible(false); - - $reflMethod = new ReflectionMethod($help, 'setMaxOptionNameLength'); - $reflMethod->setAccessible(true); - $reflMethod->invoke($help); - $reflMethod->setAccessible(false); - - $reflMethod = new ReflectionMethod($help, 'printCategoryOptions'); - $reflMethod->setAccessible(true); - $reflMethod->invoke($help, $input); - $reflMethod->setAccessible(false); + $this->setReflectionProperty($help, 'activeOptions', ['cat' => $input]); + $this->invokeReflectionMethod($help, 'setMaxOptionNameLength'); + $this->invokeReflectionMethod($help, 'printCategoryOptions', $input); $this->expectOutputRegex($expectedRegex['color']); @@ -766,4 +723,70 @@ public static function dataPrintCategoryOptions() }//end dataPrintCategoryOptions() + /** + * Test Helper: invoke a reflected method which is not publicly accessible. + * + * @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object. + * @param string $methodName The name of the method to invoke. + * @param mixed $params Optional. Parameters to pass to the method invocation. + * + * @return mixed + */ + private function invokeReflectionMethod(Help $help, $methodName, $params=null) + { + $reflMethod = new ReflectionMethod($help, $methodName); + (PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true); + + if ($params === null) { + $returnValue = $reflMethod->invoke($help); + } else { + $returnValue = $reflMethod->invoke($help, $params); + } + + (PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(false); + + return $returnValue; + + }//end invokeReflectionMethod() + + + /** + * Test Helper: retrieve the value of property which is not publicly accessible. + * + * @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object. + * @param string $properyName The name of the property to retrieve. + * + * @return mixed + */ + private function getReflectionProperty(Help $help, $properyName) + { + $reflProperty = new ReflectionProperty($help, $properyName); + (PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(true); + $returnValue = $reflProperty->getValue($help); + (PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(false); + + return $returnValue; + + }//end getReflectionProperty() + + + /** + * Test Helper: set the value of property which is not publicly accessible. + * + * @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object. + * @param string $properyName The name of the property to set. + * @param mixed $value The value to set. + * + * @return void + */ + private function setReflectionProperty(Help $help, $properyName, $value) + { + $reflProperty = new ReflectionProperty($help, $properyName); + (PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(true); + $reflProperty->setValue($help, $value); + (PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(false); + + }//end setReflectionProperty() + + }//end class