Skip to content

Commit b5f2dfa

Browse files
committed
Merge branch 'master' into 4.x
2 parents b9b8925 + 9033cfc commit b5f2dfa

File tree

9 files changed

+100
-77
lines changed

9 files changed

+100
-77
lines changed

tests/ConfigDouble.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private function preventAutoDiscoveryScreenWidth()
183183
private function getStaticConfigProperty($name)
184184
{
185185
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
186-
$property->setAccessible(true);
186+
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
187187

188188
if ($name === 'overriddenDefaults') {
189189
return $property->getValue($this);
@@ -205,15 +205,15 @@ private function getStaticConfigProperty($name)
205205
private function setStaticConfigProperty($name, $value)
206206
{
207207
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
208-
$property->setAccessible(true);
208+
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
209209

210210
if ($name === 'overriddenDefaults') {
211211
$property->setValue($this, $value);
212212
} else {
213213
$property->setValue(null, $value);
214214
}
215215

216-
$property->setAccessible(false);
216+
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);
217217

218218
}//end setStaticConfigProperty()
219219

tests/Core/Config/AbstractRealConfigTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ public static function tearDownAfterClass(): void
7474
protected static function setStaticConfigProperty($name, $value)
7575
{
7676
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
77-
$property->setAccessible(true);
77+
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
7878
$property->setValue(null, $value);
79-
$property->setAccessible(false);
79+
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);
8080

8181
}//end setStaticConfigProperty()
8282

tests/Core/Filters/GitModifiedTest.php

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

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

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

tests/Core/Filters/GitStagedTest.php

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

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

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

tests/Core/Ruleset/DisplayCachedMessagesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,14 @@ private function getPlainRuleset()
288288
private function mockCachedMessages(Ruleset $ruleset, $messages)
289289
{
290290
$reflProperty = new ReflectionProperty($ruleset, 'msgCache');
291-
$reflProperty->setAccessible(true);
291+
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(true);
292292

293293
$msgCache = $reflProperty->getValue($ruleset);
294294
foreach ($messages as $msg => $type) {
295295
$msgCache->add($msg, $type);
296296
}
297297

298-
$reflProperty->setAccessible(false);
298+
(PHP_VERSION_ID < 80100) && $reflProperty->setAccessible(false);
299299

300300
}//end mockCachedMessages()
301301

@@ -310,9 +310,9 @@ private function mockCachedMessages(Ruleset $ruleset, $messages)
310310
private function invokeDisplayCachedMessages(Ruleset $ruleset)
311311
{
312312
$reflMethod = new ReflectionMethod($ruleset, 'displayCachedMessages');
313-
$reflMethod->setAccessible(true);
313+
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(true);
314314
$reflMethod->invoke($ruleset);
315-
$reflMethod->setAccessible(false);
315+
(PHP_VERSION_ID < 80100) && $reflMethod->setAccessible(false);
316316

317317
}//end invokeDisplayCachedMessages()
318318

tests/Core/Ruleset/PopulateTokenListenersTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ public static function dataSniffListensToTokenss()
166166
public function testRegistersWhenADeprecatedSniffIsLoaded()
167167
{
168168
$property = new ReflectionProperty(self::$ruleset, 'deprecatedSniffs');
169-
$property->setAccessible(true);
169+
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
170170
$actualValue = $property->getValue(self::$ruleset);
171-
$property->setAccessible(false);
171+
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);
172172

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

tests/Core/StatusWriterTestHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ protected function redirectStatusWriterOutputToStream(): void
6868
$this->stream = $stream;
6969

7070
$streamProperty = new ReflectionProperty(StatusWriter::class, 'stream');
71-
$streamProperty->setAccessible(true);
71+
(PHP_VERSION_ID < 80100) && $streamProperty->setAccessible(true);
7272
$streamProperty->setValue(null, $this->stream);
73-
$streamProperty->setAccessible(false);
73+
(PHP_VERSION_ID < 80100) && $streamProperty->setAccessible(false);
7474

7575
}//end redirectStatusWriterOutputToStream()
7676

@@ -86,9 +86,9 @@ protected function resetStatusWriterStream(): void
8686
{
8787
// Reset the static property to its default.
8888
$streamProperty = new ReflectionProperty(StatusWriter::class, 'stream');
89-
$streamProperty->setAccessible(true);
89+
(PHP_VERSION_ID < 80100) && $streamProperty->setAccessible(true);
9090
$streamProperty->setValue(null, STDERR);
91-
$streamProperty->setAccessible(false);
91+
(PHP_VERSION_ID < 80100) && $streamProperty->setAccessible(false);
9292

9393
}//end resetStatusWriterStream()
9494

tests/Core/Tokenizers/AbstractTokenizerTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ protected function getTargetToken($commentString, $tokenType, $tokenContent=null
125125
public static function clearResolvedTokensCache()
126126
{
127127
$property = new ReflectionProperty('PHP_CodeSniffer\Tokenizers\PHP', 'resolveTokenCache');
128-
$property->setAccessible(true);
128+
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
129129
$property->setValue(null, []);
130-
$property->setAccessible(false);
130+
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);
131131

132132
}//end clearResolvedTokensCache()
133133

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

@@ -179,10 +171,7 @@ public function testOptionFiltering($longOptions, $shortOptions, $expected)
179171
{
180172
$help = new Help(new ConfigDouble(), $longOptions, $shortOptions);
181173

182-
$reflProperty = new ReflectionProperty($help, 'activeOptions');
183-
$reflProperty->setAccessible(true);
184-
$activeOptions = $reflProperty->getValue($help);
185-
$reflProperty->setAccessible(false);
174+
$activeOptions = $this->getReflectionProperty($help, 'activeOptions');
186175

187176
// Simplify the value to make it comparible.
188177
foreach ($activeOptions as $category => $options) {
@@ -318,10 +307,7 @@ public function testOptionFilteringSpacerHandling($longOptions, $shortOptions)
318307
{
319308
$help = new Help(new ConfigDouble(), $longOptions, $shortOptions);
320309

321-
$reflProperty = new ReflectionProperty($help, 'activeOptions');
322-
$reflProperty->setAccessible(true);
323-
$activeOptions = $reflProperty->getValue($help);
324-
$reflProperty->setAccessible(false);
310+
$activeOptions = $this->getReflectionProperty($help, 'activeOptions');
325311

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

@@ -487,10 +473,7 @@ public function testReportWidthCalculations($reportWidth, $longOptions, $expecte
487473
$config = new ConfigDouble(["--report-width=$reportWidth", '--no-colors']);
488474
$help = new Help($config, $longOptions);
489475

490-
$reflMethod = new ReflectionMethod($help, 'printCategories');
491-
$reflMethod->setAccessible(true);
492-
$reflMethod->invoke($help);
493-
$reflMethod->setAccessible(false);
476+
$this->invokeReflectionMethod($help, 'printCategories');
494477

495478
$this->expectOutputString($expectedOutput);
496479

@@ -566,12 +549,8 @@ public static function dataReportWidthCalculations()
566549
*/
567550
public function testColorizeVariableInput($input, $expected)
568551
{
569-
$help = new Help(new ConfigDouble(), []);
570-
571-
$reflMethod = new ReflectionMethod($help, 'colorizeVariableInput');
572-
$reflMethod->setAccessible(true);
573-
$result = $reflMethod->invoke($help, $input);
574-
$reflMethod->setAccessible(false);
552+
$help = new Help(new ConfigDouble(), []);
553+
$result = $this->invokeReflectionMethod($help, 'colorizeVariableInput', $input);
575554

576555
$this->assertSame($expected, $result);
577556

@@ -634,20 +613,9 @@ public function testPrintCategoryOptionsNoColor($input, $expectedRegex)
634613
$config = new ConfigDouble(['--no-colors']);
635614
$help = new Help($config, []);
636615

637-
$reflProperty = new ReflectionProperty($help, 'activeOptions');
638-
$reflProperty->setAccessible(true);
639-
$reflProperty->setValue($help, ['cat' => $input]);
640-
$reflProperty->setAccessible(false);
641-
642-
$reflMethod = new ReflectionMethod($help, 'setMaxOptionNameLength');
643-
$reflMethod->setAccessible(true);
644-
$reflMethod->invoke($help);
645-
$reflMethod->setAccessible(false);
646-
647-
$reflMethod = new ReflectionMethod($help, 'printCategoryOptions');
648-
$reflMethod->setAccessible(true);
649-
$reflMethod->invoke($help, $input);
650-
$reflMethod->setAccessible(false);
616+
$this->setReflectionProperty($help, 'activeOptions', ['cat' => $input]);
617+
$this->invokeReflectionMethod($help, 'setMaxOptionNameLength');
618+
$this->invokeReflectionMethod($help, 'printCategoryOptions', $input);
651619

652620
$this->expectOutputRegex($expectedRegex['no-color']);
653621

@@ -669,20 +637,9 @@ public function testPrintCategoryOptionsColor($input, $expectedRegex)
669637
$config = new ConfigDouble(['--colors']);
670638
$help = new Help($config, []);
671639

672-
$reflProperty = new ReflectionProperty($help, 'activeOptions');
673-
$reflProperty->setAccessible(true);
674-
$reflProperty->setValue($help, ['cat' => $input]);
675-
$reflProperty->setAccessible(false);
676-
677-
$reflMethod = new ReflectionMethod($help, 'setMaxOptionNameLength');
678-
$reflMethod->setAccessible(true);
679-
$reflMethod->invoke($help);
680-
$reflMethod->setAccessible(false);
681-
682-
$reflMethod = new ReflectionMethod($help, 'printCategoryOptions');
683-
$reflMethod->setAccessible(true);
684-
$reflMethod->invoke($help, $input);
685-
$reflMethod->setAccessible(false);
640+
$this->setReflectionProperty($help, 'activeOptions', ['cat' => $input]);
641+
$this->invokeReflectionMethod($help, 'setMaxOptionNameLength');
642+
$this->invokeReflectionMethod($help, 'printCategoryOptions', $input);
686643

687644
$this->expectOutputRegex($expectedRegex['color']);
688645

@@ -760,4 +717,70 @@ public static function dataPrintCategoryOptions()
760717
}//end dataPrintCategoryOptions()
761718

762719

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

0 commit comments

Comments
 (0)