Skip to content

Commit bd861af

Browse files
Do not use ReflectionProperty::setAccessible() with PHP >= 8.1
1 parent e581053 commit bd861af

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

ChangeLog-8.5.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes of the PHPUnit 8.5 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
44

5+
## [8.5.43] - 2025-MM-DD
6+
7+
### Changed
8+
9+
* Do not use `ReflectionProperty::setAccessible()` with PHP >= 8.1
10+
511
## [8.5.42] - 2025-05-02
612

713
### Changed
@@ -327,6 +333,7 @@ All notable changes of the PHPUnit 8.5 release series are documented in this fil
327333
* [#3967](https://github.com/sebastianbergmann/phpunit/issues/3967): Cannot double interface that extends interface that extends `\Throwable`
328334
* [#3968](https://github.com/sebastianbergmann/phpunit/pull/3968): Test class run in a separate PHP process are passing when `exit` called inside
329335

336+
[8.5.43]: https://github.com/sebastianbergmann/phpunit/compare/8.5.42...8.5
330337
[8.5.42]: https://github.com/sebastianbergmann/phpunit/compare/8.5.41...8.5.42
331338
[8.5.41]: https://github.com/sebastianbergmann/phpunit/compare/8.5.40...8.5.41
332339
[8.5.40]: https://github.com/sebastianbergmann/phpunit/compare/8.5.39...8.5.40

src/Framework/Assert.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use const DEBUG_BACKTRACE_IGNORE_ARGS;
1313
use const PHP_EOL;
14+
use const PHP_VERSION;
1415
use function array_key_exists;
1516
use function array_shift;
1617
use function array_unshift;
@@ -33,6 +34,7 @@
3334
use function preg_split;
3435
use function sprintf;
3536
use function strpos;
37+
use function version_compare;
3638
use ArrayAccess;
3739
use Countable;
3840
use DOMAttr;
@@ -3514,9 +3516,15 @@ public static function getObjectAttribute($object, string $attributeName)
35143516
return $object->{$attributeName};
35153517
}
35163518

3517-
$attribute->setAccessible(true);
3519+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
3520+
$attribute->setAccessible(true);
3521+
}
3522+
35183523
$value = $attribute->getValue($object);
3519-
$attribute->setAccessible(false);
3524+
3525+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
3526+
$attribute->setAccessible(false);
3527+
}
35203528

35213529
return $value;
35223530
} catch (ReflectionException $e) {

tests/unit/Framework/MockObject/MockObjectTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
*/
1010
namespace PHPUnit\Framework\MockObject;
1111

12+
use const PHP_VERSION;
1213
use function class_uses;
1314
use function func_get_args;
1415
use function get_class;
1516
use function get_parent_class;
1617
use function sprintf;
18+
use function version_compare;
1719
use Exception;
1820
use PHPUnit\Framework\ExpectationFailedException;
1921
use PHPUnit\Framework\TestCase;
@@ -1195,7 +1197,11 @@ private function resetMockObjects(): void
11951197
$refl = new ReflectionObject($this);
11961198
$refl = $refl->getParentClass();
11971199
$prop = $refl->getProperty('mockObjects');
1198-
$prop->setAccessible(true);
1200+
1201+
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
1202+
$prop->setAccessible(true);
1203+
}
1204+
11991205
$prop->setValue($this, []);
12001206
}
12011207
}

0 commit comments

Comments
 (0)