Fix #20482: Fix deprecation of ReflectionMethod::setAccessible() in PHP 8.5

This commit is contained in:
Wilmer Arambula
2025-08-20 08:31:47 -04:00
committed by GitHub
parent 5aabdd3a21
commit f5a071b1f8
13 changed files with 120 additions and 50 deletions

View File

@ -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)

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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

View File

@ -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);
}
/**

View File

@ -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);

View File

@ -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');

View File

@ -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();});

View File

@ -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);

View File

@ -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'
<select class="form-control" name="Order[customer_id]">
@ -183,7 +172,8 @@ class DataColumnTest extends \yiiunit\TestCase
<option value="1">2</option>
</select>
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'
<select class="form-control" name="Order[customer_id]">
@ -233,7 +220,8 @@ HTML
<option value="0">No</option>
</select>
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('<input type="text" class="form-control" name="RulesModel[user_id]">', $result);
$this->assertEquals(
'<input type="text" class="form-control" name="RulesModel[user_id]">',
$this->invokeMethod($dataColumn, 'renderFilterCellContent'),
);
}
}

View File

@ -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;
}