diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index adea0cf678..71afa45fb2 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -13,6 +13,7 @@ Yii Framework 2 Change Log - Enh #20461: Add PHPStan/Psalm annotations for `yii\filters\auth\AuthInterface` (max-s-lab) - Bug #20459: Fix return type in `RequestParserInterface::parse` (max-s-lab) - Bug #20475: Fix `Formatter` class `asScientific()` method for PHP `8.5` `sprintf` precision change (`6` to `0`) (terabytesoftw) +- Bug #20482: Fix deprecation of `ReflectionMethod::setAccessible()` in PHP `8.5` (terabytesoftw) - Enh #20480: Add PHPStan/Psalm annotations for `ServiceLocator::get` (max-s-lab) - Bug #20447: Fix behavior for `yii\web\Controller::bindActionParams` around `mixed` type (chriscpty) diff --git a/framework/base/ErrorException.php b/framework/base/ErrorException.php index a5af2a8027..3041ac44f6 100644 --- a/framework/base/ErrorException.php +++ b/framework/base/ErrorException.php @@ -67,7 +67,13 @@ class ErrorException extends \ErrorException } $ref = new \ReflectionProperty('Exception', 'trace'); - $ref->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } + $ref->setValue($this, $trace); } } diff --git a/framework/base/ErrorHandler.php b/framework/base/ErrorHandler.php index 74eb02c686..fec60a67ca 100644 --- a/framework/base/ErrorHandler.php +++ b/framework/base/ErrorHandler.php @@ -222,7 +222,13 @@ abstract class ErrorHandler extends Component if (E_ERROR & $code) { $exception = new ErrorException($message, $code, $code, $file, $line); $ref = new \ReflectionProperty('\Exception', 'trace'); - $ref->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } + $ref->setValue($exception, $backtrace); $this->_hhvmException = $exception; } diff --git a/tests/TestCase.php b/tests/TestCase.php index 192a3ccb2a..ebf8f58a2e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -195,9 +195,18 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase { $reflection = new \ReflectionObject($object); $method = $reflection->getMethod($method); - $method->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $method->setAccessible(true); + } + $result = $method->invokeArgs($object, $args); - if ($revoke) { + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if ($revoke && PHP_VERSION_ID < 80100) { $method->setAccessible(false); } @@ -219,9 +228,18 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase $class = $class->getParentClass(); } $property = $class->getProperty($propertyName); - $property->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $property->setAccessible(true); + } + $property->setValue($object, $value); - if ($revoke) { + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if ($revoke && PHP_VERSION_ID < 80100) { $property->setAccessible(false); } } @@ -240,9 +258,18 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase $class = $class->getParentClass(); } $property = $class->getProperty($propertyName); - $property->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $property->setAccessible(true); + } + $result = $property->getValue($object); - if ($revoke) { + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if ($revoke && PHP_VERSION_ID < 80100) { $property->setAccessible(false); } diff --git a/tests/framework/base/ActionFilterTest.php b/tests/framework/base/ActionFilterTest.php index cf1d03217d..7c1b328316 100644 --- a/tests/framework/base/ActionFilterTest.php +++ b/tests/framework/base/ActionFilterTest.php @@ -111,7 +111,12 @@ class ActionFilterTest extends TestCase $filter = Yii::createObject($filterClass); $reflection = new \ReflectionClass($filter); $method = $reflection->getMethod('isActive'); - $method->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $method->setAccessible(true); + } $controller = new \yii\web\Controller('test', Yii::$app); @@ -145,7 +150,12 @@ class ActionFilterTest extends TestCase $filter = new ActionFilter(); $reflection = new \ReflectionClass($filter); $method = $reflection->getMethod('isActive'); - $method->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $method->setAccessible(true); + } $controller = new \yii\web\Controller('test', Yii::$app); diff --git a/tests/framework/caching/FileCacheTest.php b/tests/framework/caching/FileCacheTest.php index 868bae40c8..3c7ed214c5 100644 --- a/tests/framework/caching/FileCacheTest.php +++ b/tests/framework/caching/FileCacheTest.php @@ -70,7 +70,13 @@ class FileCacheTest extends CacheTestCase $refClass = new \ReflectionClass($cache); $refMethodGetCacheFile = $refClass->getMethod('getCacheFile'); - $refMethodGetCacheFile->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $refMethodGetCacheFile->setAccessible(true); + } + $refMethodGet = $refClass->getMethod('get'); $refMethodSet = $refClass->getMethod('set'); @@ -91,7 +97,13 @@ class FileCacheTest extends CacheTestCase $normalizeKey = $cache->buildKey(__FUNCTION__); $refClass = new \ReflectionClass($cache); $refMethodGetCacheFile = $refClass->getMethod('getCacheFile'); - $refMethodGetCacheFile->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $refMethodGetCacheFile->setAccessible(true); + } + $cacheFile = $refMethodGetCacheFile->invoke($cache, $normalizeKey); // simulate cache expire 10 seconds ago diff --git a/tests/framework/console/controllers/AssetControllerTest.php b/tests/framework/console/controllers/AssetControllerTest.php index 768829ef83..795aec95ff 100644 --- a/tests/framework/console/controllers/AssetControllerTest.php +++ b/tests/framework/console/controllers/AssetControllerTest.php @@ -183,13 +183,8 @@ class AssetControllerTest extends TestCase protected function invokeAssetControllerMethod($methodName, array $args = []) { $controller = $this->createAssetController(); - $controllerClassReflection = new \ReflectionClass(get_class($controller)); - $methodReflection = $controllerClassReflection->getMethod($methodName); - $methodReflection->setAccessible(true); - $result = $methodReflection->invokeArgs($controller, $args); - $methodReflection->setAccessible(false); - return $result; + return $this->invokeMethod($controller, $methodName, $args); } /** diff --git a/tests/framework/data/BaseDataProviderTest.php b/tests/framework/data/BaseDataProviderTest.php index c06d4a09bc..2587a835e5 100644 --- a/tests/framework/data/BaseDataProviderTest.php +++ b/tests/framework/data/BaseDataProviderTest.php @@ -19,7 +19,13 @@ class BaseDataProviderTest extends TestCase { $rc = new \ReflectionClass(BaseDataProvider::className()); $rp = $rc->getProperty('counter'); - $rp->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $rp->setAccessible(true); + } + $rp->setValue(new ConcreteDataProvider(), null); $this->assertNull((new ConcreteDataProvider())->id); diff --git a/tests/framework/filters/HttpCacheTest.php b/tests/framework/filters/HttpCacheTest.php index a5be733520..da0526c9fb 100644 --- a/tests/framework/filters/HttpCacheTest.php +++ b/tests/framework/filters/HttpCacheTest.php @@ -52,9 +52,13 @@ class HttpCacheTest extends \yiiunit\TestCase { $httpCache = new HttpCache(); $request = Yii::$app->getRequest(); - $method = new \ReflectionMethod($httpCache, 'validateCache'); - $method->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $method->setAccessible(true); + } $request->headers->remove('If-Modified-Since'); $request->headers->remove('If-None-Match'); diff --git a/tests/framework/filters/auth/AuthMethodTest.php b/tests/framework/filters/auth/AuthMethodTest.php index f045b26b6a..8682a336e3 100644 --- a/tests/framework/filters/auth/AuthMethodTest.php +++ b/tests/framework/filters/auth/AuthMethodTest.php @@ -73,7 +73,12 @@ class AuthMethodTest extends TestCase { $reflection = new \ReflectionClass(AuthMethod::className()); $method = $reflection->getMethod('isOptional'); - $method->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $method->setAccessible(true); + } $filter = $this->createFilter(function () {return new \stdClass();}); diff --git a/tests/framework/filters/auth/AuthTest.php b/tests/framework/filters/auth/AuthTest.php index 62db91d22d..50085223aa 100644 --- a/tests/framework/filters/auth/AuthTest.php +++ b/tests/framework/filters/auth/AuthTest.php @@ -153,7 +153,12 @@ class AuthTest extends \yiiunit\TestCase $filter = new $authClass(); $reflection = new \ReflectionClass($filter); $method = $reflection->getMethod('isActive'); - $method->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $method->setAccessible(true); + } $controller = new \yii\web\Controller('test', Yii::$app); diff --git a/tests/framework/grid/DataColumnTest.php b/tests/framework/grid/DataColumnTest.php index 6642566d77..a58819c8d4 100644 --- a/tests/framework/grid/DataColumnTest.php +++ b/tests/framework/grid/DataColumnTest.php @@ -39,9 +39,7 @@ class DataColumnTest extends \yiiunit\TestCase ]); $labels = []; foreach ($grid->columns as $column) { - $method = new \ReflectionMethod($column, 'getHeaderCellLabel'); - $method->setAccessible(true); - $labels[] = $method->invoke($column); + $labels[] = $this->invokeMethod($column, 'getHeaderCellLabel'); } $this->assertEquals(['Customer', 'Invoice Total'], $labels); } @@ -62,9 +60,7 @@ class DataColumnTest extends \yiiunit\TestCase ]); $labels = []; foreach ($grid->columns as $column) { - $method = new \ReflectionMethod($column, 'getHeaderCellLabel'); - $method->setAccessible(true); - $labels[] = $method->invoke($column); + $labels[] = $this->invokeMethod($column, 'getHeaderCellLabel'); } $this->assertEquals(['Customer', 'Invoice Total'], $labels); } @@ -91,10 +87,8 @@ class DataColumnTest extends \yiiunit\TestCase ]); //print_r($grid->columns);exit(); $dataColumn = $grid->columns[0]; - $method = new \ReflectionMethod($dataColumn, 'renderFilterCellContent'); - $method->setAccessible(true); - $result = $method->invoke($dataColumn); - $this->assertEquals($result, $filterInput); + + $this->assertEquals($this->invokeMethod($dataColumn, 'renderFilterCellContent'), $filterInput); } /** @@ -128,10 +122,8 @@ class DataColumnTest extends \yiiunit\TestCase ]); $dataColumn = $grid->columns[0]; - $method = new \ReflectionMethod($dataColumn, 'renderFilterCellContent'); - $method->setAccessible(true); - $result = $method->invoke($dataColumn); - $this->assertEquals($result, $filterInput); + + $this->assertEquals($this->invokeMethod($dataColumn, 'renderFilterCellContent'), $filterInput); } @@ -172,9 +164,6 @@ class DataColumnTest extends \yiiunit\TestCase ]); $dataColumn = $grid->columns[0]; - $method = new \ReflectionMethod($dataColumn, 'renderFilterCellContent'); - $method->setAccessible(true); - $result = $method->invoke($dataColumn); $this->assertEqualsWithoutLE(<<<'HTML' HTML - , $result); + , $this->invokeMethod($dataColumn, 'renderFilterCellContent'), + ); } /** @@ -222,9 +212,6 @@ HTML ]); $dataColumn = $grid->columns[0]; - $method = new \ReflectionMethod($dataColumn, 'renderFilterCellContent'); - $method->setAccessible(true); - $result = $method->invoke($dataColumn); $this->assertEqualsWithoutLE(<<<'HTML' HTML - , $result); + , $this->invokeMethod($dataColumn, 'renderFilterCellContent'), + ); } /** @@ -258,10 +246,10 @@ HTML ]); $dataColumn = $grid->columns[0]; - $method = new \ReflectionMethod($dataColumn, 'renderFilterCellContent'); - $method->setAccessible(true); - $result = $method->invoke($dataColumn); - $this->assertEquals('', $result); + $this->assertEquals( + '', + $this->invokeMethod($dataColumn, 'renderFilterCellContent'), + ); } } diff --git a/tests/framework/widgets/BreadcrumbsTest.php b/tests/framework/widgets/BreadcrumbsTest.php index fd2106c7bf..2fe732e6e9 100644 --- a/tests/framework/widgets/BreadcrumbsTest.php +++ b/tests/framework/widgets/BreadcrumbsTest.php @@ -195,7 +195,12 @@ class BreadcrumbsTest extends \yiiunit\TestCase protected function reflectMethod($class = '\yii\widgets\Breadcrumbs', $method = 'renderItem') { $value = new \ReflectionMethod($class, $method); - $value->setAccessible(true); + + // @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible + // @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op + if (PHP_VERSION_ID < 80100) { + $value->setAccessible(true); + } return $value; }