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) - 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 #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 #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) - 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) - 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 = new \ReflectionProperty('Exception', 'trace');
// @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->setAccessible(true);
}
$ref->setValue($this, $trace); $ref->setValue($this, $trace);
} }
} }

View File

@ -222,7 +222,13 @@ abstract class ErrorHandler extends Component
if (E_ERROR & $code) { if (E_ERROR & $code) {
$exception = new ErrorException($message, $code, $code, $file, $line); $exception = new ErrorException($message, $code, $code, $file, $line);
$ref = new \ReflectionProperty('\Exception', 'trace'); $ref = new \ReflectionProperty('\Exception', 'trace');
// @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->setAccessible(true);
}
$ref->setValue($exception, $backtrace); $ref->setValue($exception, $backtrace);
$this->_hhvmException = $exception; $this->_hhvmException = $exception;
} }

View File

@ -195,9 +195,18 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
{ {
$reflection = new \ReflectionObject($object); $reflection = new \ReflectionObject($object);
$method = $reflection->getMethod($method); $method = $reflection->getMethod($method);
// @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); $method->setAccessible(true);
}
$result = $method->invokeArgs($object, $args); $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); $method->setAccessible(false);
} }
@ -219,9 +228,18 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
$class = $class->getParentClass(); $class = $class->getParentClass();
} }
$property = $class->getProperty($propertyName); $property = $class->getProperty($propertyName);
// @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->setAccessible(true);
}
$property->setValue($object, $value); $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); $property->setAccessible(false);
} }
} }
@ -240,9 +258,18 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
$class = $class->getParentClass(); $class = $class->getParentClass();
} }
$property = $class->getProperty($propertyName); $property = $class->getProperty($propertyName);
// @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->setAccessible(true);
}
$result = $property->getValue($object); $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); $property->setAccessible(false);
} }

View File

@ -111,7 +111,12 @@ class ActionFilterTest extends TestCase
$filter = Yii::createObject($filterClass); $filter = Yii::createObject($filterClass);
$reflection = new \ReflectionClass($filter); $reflection = new \ReflectionClass($filter);
$method = $reflection->getMethod('isActive'); $method = $reflection->getMethod('isActive');
// @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); $method->setAccessible(true);
}
$controller = new \yii\web\Controller('test', Yii::$app); $controller = new \yii\web\Controller('test', Yii::$app);
@ -145,7 +150,12 @@ class ActionFilterTest extends TestCase
$filter = new ActionFilter(); $filter = new ActionFilter();
$reflection = new \ReflectionClass($filter); $reflection = new \ReflectionClass($filter);
$method = $reflection->getMethod('isActive'); $method = $reflection->getMethod('isActive');
// @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); $method->setAccessible(true);
}
$controller = new \yii\web\Controller('test', Yii::$app); $controller = new \yii\web\Controller('test', Yii::$app);

View File

@ -70,7 +70,13 @@ class FileCacheTest extends CacheTestCase
$refClass = new \ReflectionClass($cache); $refClass = new \ReflectionClass($cache);
$refMethodGetCacheFile = $refClass->getMethod('getCacheFile'); $refMethodGetCacheFile = $refClass->getMethod('getCacheFile');
// @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); $refMethodGetCacheFile->setAccessible(true);
}
$refMethodGet = $refClass->getMethod('get'); $refMethodGet = $refClass->getMethod('get');
$refMethodSet = $refClass->getMethod('set'); $refMethodSet = $refClass->getMethod('set');
@ -91,7 +97,13 @@ class FileCacheTest extends CacheTestCase
$normalizeKey = $cache->buildKey(__FUNCTION__); $normalizeKey = $cache->buildKey(__FUNCTION__);
$refClass = new \ReflectionClass($cache); $refClass = new \ReflectionClass($cache);
$refMethodGetCacheFile = $refClass->getMethod('getCacheFile'); $refMethodGetCacheFile = $refClass->getMethod('getCacheFile');
// @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); $refMethodGetCacheFile->setAccessible(true);
}
$cacheFile = $refMethodGetCacheFile->invoke($cache, $normalizeKey); $cacheFile = $refMethodGetCacheFile->invoke($cache, $normalizeKey);
// simulate cache expire 10 seconds ago // simulate cache expire 10 seconds ago

View File

@ -183,13 +183,8 @@ class AssetControllerTest extends TestCase
protected function invokeAssetControllerMethod($methodName, array $args = []) protected function invokeAssetControllerMethod($methodName, array $args = [])
{ {
$controller = $this->createAssetController(); $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()); $rc = new \ReflectionClass(BaseDataProvider::className());
$rp = $rc->getProperty('counter'); $rp = $rc->getProperty('counter');
// @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->setAccessible(true);
}
$rp->setValue(new ConcreteDataProvider(), null); $rp->setValue(new ConcreteDataProvider(), null);
$this->assertNull((new ConcreteDataProvider())->id); $this->assertNull((new ConcreteDataProvider())->id);

View File

@ -52,9 +52,13 @@ class HttpCacheTest extends \yiiunit\TestCase
{ {
$httpCache = new HttpCache(); $httpCache = new HttpCache();
$request = Yii::$app->getRequest(); $request = Yii::$app->getRequest();
$method = new \ReflectionMethod($httpCache, 'validateCache'); $method = new \ReflectionMethod($httpCache, 'validateCache');
// @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); $method->setAccessible(true);
}
$request->headers->remove('If-Modified-Since'); $request->headers->remove('If-Modified-Since');
$request->headers->remove('If-None-Match'); $request->headers->remove('If-None-Match');

View File

@ -73,7 +73,12 @@ class AuthMethodTest extends TestCase
{ {
$reflection = new \ReflectionClass(AuthMethod::className()); $reflection = new \ReflectionClass(AuthMethod::className());
$method = $reflection->getMethod('isOptional'); $method = $reflection->getMethod('isOptional');
// @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); $method->setAccessible(true);
}
$filter = $this->createFilter(function () {return new \stdClass();}); $filter = $this->createFilter(function () {return new \stdClass();});

View File

@ -153,7 +153,12 @@ class AuthTest extends \yiiunit\TestCase
$filter = new $authClass(); $filter = new $authClass();
$reflection = new \ReflectionClass($filter); $reflection = new \ReflectionClass($filter);
$method = $reflection->getMethod('isActive'); $method = $reflection->getMethod('isActive');
// @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); $method->setAccessible(true);
}
$controller = new \yii\web\Controller('test', Yii::$app); $controller = new \yii\web\Controller('test', Yii::$app);

View File

@ -39,9 +39,7 @@ class DataColumnTest extends \yiiunit\TestCase
]); ]);
$labels = []; $labels = [];
foreach ($grid->columns as $column) { foreach ($grid->columns as $column) {
$method = new \ReflectionMethod($column, 'getHeaderCellLabel'); $labels[] = $this->invokeMethod($column, 'getHeaderCellLabel');
$method->setAccessible(true);
$labels[] = $method->invoke($column);
} }
$this->assertEquals(['Customer', 'Invoice Total'], $labels); $this->assertEquals(['Customer', 'Invoice Total'], $labels);
} }
@ -62,9 +60,7 @@ class DataColumnTest extends \yiiunit\TestCase
]); ]);
$labels = []; $labels = [];
foreach ($grid->columns as $column) { foreach ($grid->columns as $column) {
$method = new \ReflectionMethod($column, 'getHeaderCellLabel'); $labels[] = $this->invokeMethod($column, 'getHeaderCellLabel');
$method->setAccessible(true);
$labels[] = $method->invoke($column);
} }
$this->assertEquals(['Customer', 'Invoice Total'], $labels); $this->assertEquals(['Customer', 'Invoice Total'], $labels);
} }
@ -91,10 +87,8 @@ class DataColumnTest extends \yiiunit\TestCase
]); ]);
//print_r($grid->columns);exit(); //print_r($grid->columns);exit();
$dataColumn = $grid->columns[0]; $dataColumn = $grid->columns[0];
$method = new \ReflectionMethod($dataColumn, 'renderFilterCellContent');
$method->setAccessible(true); $this->assertEquals($this->invokeMethod($dataColumn, 'renderFilterCellContent'), $filterInput);
$result = $method->invoke($dataColumn);
$this->assertEquals($result, $filterInput);
} }
/** /**
@ -128,10 +122,8 @@ class DataColumnTest extends \yiiunit\TestCase
]); ]);
$dataColumn = $grid->columns[0]; $dataColumn = $grid->columns[0];
$method = new \ReflectionMethod($dataColumn, 'renderFilterCellContent');
$method->setAccessible(true); $this->assertEquals($this->invokeMethod($dataColumn, 'renderFilterCellContent'), $filterInput);
$result = $method->invoke($dataColumn);
$this->assertEquals($result, $filterInput);
} }
@ -172,9 +164,6 @@ class DataColumnTest extends \yiiunit\TestCase
]); ]);
$dataColumn = $grid->columns[0]; $dataColumn = $grid->columns[0];
$method = new \ReflectionMethod($dataColumn, 'renderFilterCellContent');
$method->setAccessible(true);
$result = $method->invoke($dataColumn);
$this->assertEqualsWithoutLE(<<<'HTML' $this->assertEqualsWithoutLE(<<<'HTML'
<select class="form-control" name="Order[customer_id]"> <select class="form-control" name="Order[customer_id]">
@ -183,7 +172,8 @@ class DataColumnTest extends \yiiunit\TestCase
<option value="1">2</option> <option value="1">2</option>
</select> </select>
HTML HTML
, $result); , $this->invokeMethod($dataColumn, 'renderFilterCellContent'),
);
} }
/** /**
@ -222,9 +212,6 @@ HTML
]); ]);
$dataColumn = $grid->columns[0]; $dataColumn = $grid->columns[0];
$method = new \ReflectionMethod($dataColumn, 'renderFilterCellContent');
$method->setAccessible(true);
$result = $method->invoke($dataColumn);
$this->assertEqualsWithoutLE(<<<'HTML' $this->assertEqualsWithoutLE(<<<'HTML'
<select class="form-control" name="Order[customer_id]"> <select class="form-control" name="Order[customer_id]">
@ -233,7 +220,8 @@ HTML
<option value="0">No</option> <option value="0">No</option>
</select> </select>
HTML HTML
, $result); , $this->invokeMethod($dataColumn, 'renderFilterCellContent'),
);
} }
/** /**
@ -258,10 +246,10 @@ HTML
]); ]);
$dataColumn = $grid->columns[0]; $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') protected function reflectMethod($class = '\yii\widgets\Breadcrumbs', $method = 'renderItem')
{ {
$value = new \ReflectionMethod($class, $method); $value = new \ReflectionMethod($class, $method);
// @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); $value->setAccessible(true);
}
return $value; return $value;
} }