From 8fa419fdadbf8b6347ec036b5e5feeb82a47bdd7 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Thu, 19 Dec 2024 16:29:59 +0800 Subject: [PATCH] refactor: `Reflection*::setAccessible()` is now no-op in PHP 8.1 (#9331) --- system/BaseModel.php | 2 -- system/Test/ReflectionHelper.php | 8 ++------ system/Traits/PropertiesTrait.php | 1 - tests/system/CLI/CLITest.php | 2 -- 4 files changed, 2 insertions(+), 11 deletions(-) diff --git a/system/BaseModel.php b/system/BaseModel.php index 3ced5599a1b5..0d4a7b3979b3 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -1799,8 +1799,6 @@ protected function objectToRawArray($object, bool $onlyChanged = true, bool $rec // Loop over each property, // saving the name/value in a new array we can return. foreach ($props as $prop) { - // Must make protected values accessible. - $prop->setAccessible(true); $properties[$prop->getName()] = $prop->getValue($object); } } diff --git a/system/Test/ReflectionHelper.php b/system/Test/ReflectionHelper.php index adf22ac5a9ac..a1b7ee99c67f 100644 --- a/system/Test/ReflectionHelper.php +++ b/system/Test/ReflectionHelper.php @@ -39,8 +39,7 @@ trait ReflectionHelper public static function getPrivateMethodInvoker($obj, $method) { $refMethod = new ReflectionMethod($obj, $method); - $refMethod->setAccessible(true); - $obj = (gettype($obj) === 'object') ? $obj : null; + $obj = (gettype($obj) === 'object') ? $obj : null; return static fn (...$args) => $refMethod->invokeArgs($obj, $args); } @@ -59,10 +58,7 @@ private static function getAccessibleRefProperty($obj, $property) { $refClass = is_object($obj) ? new ReflectionObject($obj) : new ReflectionClass($obj); - $refProperty = $refClass->getProperty($property); - $refProperty->setAccessible(true); - - return $refProperty; + return $refClass->getProperty($property); } /** diff --git a/system/Traits/PropertiesTrait.php b/system/Traits/PropertiesTrait.php index ed2da42efc26..30b3f86690c0 100644 --- a/system/Traits/PropertiesTrait.php +++ b/system/Traits/PropertiesTrait.php @@ -71,7 +71,6 @@ final public function getNonPublicProperties(): array continue; } - $property->setAccessible(true); $properties[] = $property; } diff --git a/tests/system/CLI/CLITest.php b/tests/system/CLI/CLITest.php index 0a4e36316dbe..413fd71c85f4 100644 --- a/tests/system/CLI/CLITest.php +++ b/tests/system/CLI/CLITest.php @@ -549,13 +549,11 @@ public function testParseCommandMultipleOptions(): void public function testWindow(): void { $height = new ReflectionProperty(CLI::class, 'height'); - $height->setAccessible(true); $height->setValue(null, null); $this->assertIsInt(CLI::getHeight()); $width = new ReflectionProperty(CLI::class, 'width'); - $width->setAccessible(true); $width->setValue(null, null); $this->assertIsInt(CLI::getWidth());