Skip to content

Commit 9033cfc

Browse files
authored
Merge pull request #1178 from PHPCSStandards/feature/fix-test-runtime-deprecations-php-8.5
PHP 8.5 | Tests: prevent deprecation notice for Reflection*::setAccessible()
2 parents 2f08e74 + ca12fee commit 9033cfc

File tree

8 files changed

+97
-73
lines changed

8 files changed

+97
-73
lines changed

tests/ConfigDouble.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ private function preventAutoDiscoveryScreenWidth()
185185
private function getStaticConfigProperty($name)
186186
{
187187
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
188-
$property->setAccessible(true);
188+
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
189+
189190
return $property->getValue();
190191

191192
}//end getStaticConfigProperty()
@@ -202,9 +203,9 @@ private function getStaticConfigProperty($name)
202203
private function setStaticConfigProperty($name, $value)
203204
{
204205
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
205-
$property->setAccessible(true);
206+
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
206207
$property->setValue(null, $value);
207-
$property->setAccessible(false);
208+
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);
208209

209210
}//end setStaticConfigProperty()
210211

tests/Core/Config/AbstractRealConfigTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public static function resetConfigToDefaults()
8282
protected static function setStaticConfigProperty($name, $value)
8383
{
8484
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
85-
$property->setAccessible(true);
85+
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
8686
$property->setValue(null, $value);
87-
$property->setAccessible(false);
87+
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);
8888

8989
}//end setStaticConfigProperty()
9090

tests/Core/Filters/GitModifiedTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public function testExecAlwaysReturnsArray($cmd, $expected)
228228
$filter = new GitModified($fakeDI, '/', self::$config, self::$ruleset);
229229

230230
$reflMethod = new ReflectionMethod($filter, 'exec');
231-
$reflMethod->setAccessible(true);
231+
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true);
232232
$result = $reflMethod->invoke($filter, $cmd);
233233

234234
$this->assertSame($expected, $result);

tests/Core/Filters/GitStagedTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public function testExecAlwaysReturnsArray($cmd, $expected)
228228
$filter = new GitStaged($fakeDI, '/', self::$config, self::$ruleset);
229229

230230
$reflMethod = new ReflectionMethod($filter, 'exec');
231-
$reflMethod->setAccessible(true);
231+
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true);
232232
$result = $reflMethod->invoke($filter, $cmd);
233233

234234
$this->assertSame($expected, $result);

tests/Core/Ruleset/DisplayCachedMessagesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,14 @@ private function getPlainRuleset()
280280
private function mockCachedMessages(Ruleset $ruleset, $messages)
281281
{
282282
$reflProperty = new ReflectionProperty($ruleset, 'msgCache');
283-
$reflProperty->setAccessible(true);
283+
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(true);
284284

285285
$msgCache = $reflProperty->getValue($ruleset);
286286
foreach ($messages as $msg => $type) {
287287
$msgCache->add($msg, $type);
288288
}
289289

290-
$reflProperty->setAccessible(false);
290+
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(false);
291291

292292
}//end mockCachedMessages()
293293

@@ -302,9 +302,9 @@ private function mockCachedMessages(Ruleset $ruleset, $messages)
302302
private function invokeDisplayCachedMessages(Ruleset $ruleset)
303303
{
304304
$reflMethod = new ReflectionMethod($ruleset, 'displayCachedMessages');
305-
$reflMethod->setAccessible(true);
305+
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true);
306306
$reflMethod->invoke($ruleset);
307-
$reflMethod->setAccessible(false);
307+
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(false);
308308

309309
}//end invokeDisplayCachedMessages()
310310

tests/Core/Ruleset/PopulateTokenListenersTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ public static function dataSniffListensToTokenss()
172172
public function testRegistersWhenADeprecatedSniffIsLoaded()
173173
{
174174
$property = new ReflectionProperty(self::$ruleset, 'deprecatedSniffs');
175-
$property->setAccessible(true);
175+
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
176176
$actualValue = $property->getValue(self::$ruleset);
177-
$property->setAccessible(false);
177+
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);
178178

179179
// Only verify there is one deprecated sniff registered.
180180
// There are other tests which test the deprecated sniff handling in more detail.

tests/Core/Tokenizers/AbstractTokenizerTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ protected function getTargetToken($commentString, $tokenType, $tokenContent=null
137137
public static function clearResolvedTokensCache()
138138
{
139139
$property = new ReflectionProperty('PHP_CodeSniffer\Tokenizers\PHP', 'resolveTokenCache');
140-
$property->setAccessible(true);
140+
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
141141
$property->setValue(null, []);
142-
$property->setAccessible(false);
142+
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);
143143

144144
}//end clearResolvedTokensCache()
145145

tests/Core/Util/Help/HelpTest.php

Lines changed: 81 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,8 @@ final class HelpTest extends TestCase
3737
*/
3838
public function testQaArgumentNamesAreWithinAcceptableBounds()
3939
{
40-
$help = new Help(new ConfigDouble(), []);
41-
42-
$reflMethod = new ReflectionMethod($help, 'getAllOptions');
43-
$reflMethod->setAccessible(true);
44-
$allOptions = $reflMethod->invoke($help);
45-
$reflMethod->setAccessible(false);
40+
$help = new Help(new ConfigDouble(), []);
41+
$allOptions = $this->invokeReflectionMethod($help, 'getAllOptions');
4642

4743
$this->assertGreaterThan(0, count($allOptions), 'No categories found');
4844

@@ -81,12 +77,8 @@ public function testQaArgumentNamesAreWithinAcceptableBounds()
8177
*/
8278
public function testQaValidCategoryOptionDefinitions()
8379
{
84-
$help = new Help(new ConfigDouble(), []);
85-
86-
$reflMethod = new ReflectionMethod($help, 'getAllOptions');
87-
$reflMethod->setAccessible(true);
88-
$allOptions = $reflMethod->invoke($help);
89-
$reflMethod->setAccessible(false);
80+
$help = new Help(new ConfigDouble(), []);
81+
$allOptions = $this->invokeReflectionMethod($help, 'getAllOptions');
9082

9183
$this->assertGreaterThan(0, count($allOptions), 'No categories found');
9284

@@ -185,10 +177,7 @@ public function testOptionFiltering($longOptions, $shortOptions, $expected)
185177
{
186178
$help = new Help(new ConfigDouble(), $longOptions, $shortOptions);
187179

188-
$reflProperty = new ReflectionProperty($help, 'activeOptions');
189-
$reflProperty->setAccessible(true);
190-
$activeOptions = $reflProperty->getValue($help);
191-
$reflProperty->setAccessible(false);
180+
$activeOptions = $this->getReflectionProperty($help, 'activeOptions');
192181

193182
// Simplify the value to make it comparible.
194183
foreach ($activeOptions as $category => $options) {
@@ -324,10 +313,7 @@ public function testOptionFilteringSpacerHandling($longOptions, $shortOptions)
324313
{
325314
$help = new Help(new ConfigDouble(), $longOptions, $shortOptions);
326315

327-
$reflProperty = new ReflectionProperty($help, 'activeOptions');
328-
$reflProperty->setAccessible(true);
329-
$activeOptions = $reflProperty->getValue($help);
330-
$reflProperty->setAccessible(false);
316+
$activeOptions = $this->getReflectionProperty($help, 'activeOptions');
331317

332318
$this->assertNotEmpty($activeOptions, 'Active options is empty, test is invalid');
333319

@@ -493,10 +479,7 @@ public function testReportWidthCalculations($reportWidth, $longOptions, $expecte
493479
$config = new ConfigDouble(["--report-width=$reportWidth", '--no-colors']);
494480
$help = new Help($config, $longOptions);
495481

496-
$reflMethod = new ReflectionMethod($help, 'printCategories');
497-
$reflMethod->setAccessible(true);
498-
$reflMethod->invoke($help);
499-
$reflMethod->setAccessible(false);
482+
$this->invokeReflectionMethod($help, 'printCategories');
500483

501484
$this->expectOutputString($expectedOutput);
502485

@@ -572,12 +555,8 @@ public static function dataReportWidthCalculations()
572555
*/
573556
public function testColorizeVariableInput($input, $expected)
574557
{
575-
$help = new Help(new ConfigDouble(), []);
576-
577-
$reflMethod = new ReflectionMethod($help, 'colorizeVariableInput');
578-
$reflMethod->setAccessible(true);
579-
$result = $reflMethod->invoke($help, $input);
580-
$reflMethod->setAccessible(false);
558+
$help = new Help(new ConfigDouble(), []);
559+
$result = $this->invokeReflectionMethod($help, 'colorizeVariableInput', $input);
581560

582561
$this->assertSame($expected, $result);
583562

@@ -640,20 +619,9 @@ public function testPrintCategoryOptionsNoColor($input, $expectedRegex)
640619
$config = new ConfigDouble(['--no-colors']);
641620
$help = new Help($config, []);
642621

643-
$reflProperty = new ReflectionProperty($help, 'activeOptions');
644-
$reflProperty->setAccessible(true);
645-
$reflProperty->setValue($help, ['cat' => $input]);
646-
$reflProperty->setAccessible(false);
647-
648-
$reflMethod = new ReflectionMethod($help, 'setMaxOptionNameLength');
649-
$reflMethod->setAccessible(true);
650-
$reflMethod->invoke($help);
651-
$reflMethod->setAccessible(false);
652-
653-
$reflMethod = new ReflectionMethod($help, 'printCategoryOptions');
654-
$reflMethod->setAccessible(true);
655-
$reflMethod->invoke($help, $input);
656-
$reflMethod->setAccessible(false);
622+
$this->setReflectionProperty($help, 'activeOptions', ['cat' => $input]);
623+
$this->invokeReflectionMethod($help, 'setMaxOptionNameLength');
624+
$this->invokeReflectionMethod($help, 'printCategoryOptions', $input);
657625

658626
$this->expectOutputRegex($expectedRegex['no-color']);
659627

@@ -675,20 +643,9 @@ public function testPrintCategoryOptionsColor($input, $expectedRegex)
675643
$config = new ConfigDouble(['--colors']);
676644
$help = new Help($config, []);
677645

678-
$reflProperty = new ReflectionProperty($help, 'activeOptions');
679-
$reflProperty->setAccessible(true);
680-
$reflProperty->setValue($help, ['cat' => $input]);
681-
$reflProperty->setAccessible(false);
682-
683-
$reflMethod = new ReflectionMethod($help, 'setMaxOptionNameLength');
684-
$reflMethod->setAccessible(true);
685-
$reflMethod->invoke($help);
686-
$reflMethod->setAccessible(false);
687-
688-
$reflMethod = new ReflectionMethod($help, 'printCategoryOptions');
689-
$reflMethod->setAccessible(true);
690-
$reflMethod->invoke($help, $input);
691-
$reflMethod->setAccessible(false);
646+
$this->setReflectionProperty($help, 'activeOptions', ['cat' => $input]);
647+
$this->invokeReflectionMethod($help, 'setMaxOptionNameLength');
648+
$this->invokeReflectionMethod($help, 'printCategoryOptions', $input);
692649

693650
$this->expectOutputRegex($expectedRegex['color']);
694651

@@ -766,4 +723,70 @@ public static function dataPrintCategoryOptions()
766723
}//end dataPrintCategoryOptions()
767724

768725

726+
/**
727+
* Test Helper: invoke a reflected method which is not publicly accessible.
728+
*
729+
* @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
730+
* @param string $methodName The name of the method to invoke.
731+
* @param mixed $params Optional. Parameters to pass to the method invocation.
732+
*
733+
* @return mixed
734+
*/
735+
private function invokeReflectionMethod(Help $help, $methodName, $params=null)
736+
{
737+
$reflMethod = new ReflectionMethod($help, $methodName);
738+
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true);
739+
740+
if ($params === null) {
741+
$returnValue = $reflMethod->invoke($help);
742+
} else {
743+
$returnValue = $reflMethod->invoke($help, $params);
744+
}
745+
746+
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(false);
747+
748+
return $returnValue;
749+
750+
}//end invokeReflectionMethod()
751+
752+
753+
/**
754+
* Test Helper: retrieve the value of property which is not publicly accessible.
755+
*
756+
* @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
757+
* @param string $properyName The name of the property to retrieve.
758+
*
759+
* @return mixed
760+
*/
761+
private function getReflectionProperty(Help $help, $properyName)
762+
{
763+
$reflProperty = new ReflectionProperty($help, $properyName);
764+
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(true);
765+
$returnValue = $reflProperty->getValue($help);
766+
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(false);
767+
768+
return $returnValue;
769+
770+
}//end getReflectionProperty()
771+
772+
773+
/**
774+
* Test Helper: set the value of property which is not publicly accessible.
775+
*
776+
* @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
777+
* @param string $properyName The name of the property to set.
778+
* @param mixed $value The value to set.
779+
*
780+
* @return void
781+
*/
782+
private function setReflectionProperty(Help $help, $properyName, $value)
783+
{
784+
$reflProperty = new ReflectionProperty($help, $properyName);
785+
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(true);
786+
$reflProperty->setValue($help, $value);
787+
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(false);
788+
789+
}//end setReflectionProperty()
790+
791+
769792
}//end class

0 commit comments

Comments
 (0)