Skip to content

Commit f5a071b

Browse files
Fix #20482: Fix deprecation of ReflectionMethod::setAccessible() in PHP 8.5
1 parent 5aabdd3 commit f5a071b

File tree

13 files changed

+120
-50
lines changed

13 files changed

+120
-50
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Yii Framework 2 Change Log
1313
- Enh #20461: Add PHPStan/Psalm annotations for `yii\filters\auth\AuthInterface` (max-s-lab)
1414
- Bug #20459: Fix return type in `RequestParserInterface::parse` (max-s-lab)
1515
- Bug #20475: Fix `Formatter` class `asScientific()` method for PHP `8.5` `sprintf` precision change (`6` to `0`) (terabytesoftw)
16+
- Bug #20482: Fix deprecation of `ReflectionMethod::setAccessible()` in PHP `8.5` (terabytesoftw)
1617
- Enh #20480: Add PHPStan/Psalm annotations for `ServiceLocator::get` (max-s-lab)
1718
- Bug #20447: Fix behavior for `yii\web\Controller::bindActionParams` around `mixed` type (chriscpty)
1819

framework/base/ErrorException.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ public function __construct($message = '', $code = 0, $severity = 1, $filename =
6767
}
6868

6969
$ref = new \ReflectionProperty('Exception', 'trace');
70-
$ref->setAccessible(true);
70+
71+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
72+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
73+
if (PHP_VERSION_ID < 80100) {
74+
$ref->setAccessible(true);
75+
}
76+
7177
$ref->setValue($this, $trace);
7278
}
7379
}

framework/base/ErrorHandler.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,13 @@ public function handleHhvmError($code, $message, $file, $line, $context, $backtr
222222
if (E_ERROR & $code) {
223223
$exception = new ErrorException($message, $code, $code, $file, $line);
224224
$ref = new \ReflectionProperty('\Exception', 'trace');
225-
$ref->setAccessible(true);
225+
226+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
227+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
228+
if (PHP_VERSION_ID < 80100) {
229+
$ref->setAccessible(true);
230+
}
231+
226232
$ref->setValue($exception, $backtrace);
227233
$this->_hhvmException = $exception;
228234
}

tests/TestCase.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,18 @@ protected function invokeMethod($object, $method, $args = [], $revoke = true)
195195
{
196196
$reflection = new \ReflectionObject($object);
197197
$method = $reflection->getMethod($method);
198-
$method->setAccessible(true);
198+
199+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
200+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
201+
if (PHP_VERSION_ID < 80100) {
202+
$method->setAccessible(true);
203+
}
204+
199205
$result = $method->invokeArgs($object, $args);
200-
if ($revoke) {
206+
207+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
208+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
209+
if ($revoke && PHP_VERSION_ID < 80100) {
201210
$method->setAccessible(false);
202211
}
203212

@@ -219,9 +228,18 @@ protected function setInaccessibleProperty($object, $propertyName, $value, $revo
219228
$class = $class->getParentClass();
220229
}
221230
$property = $class->getProperty($propertyName);
222-
$property->setAccessible(true);
231+
232+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
233+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
234+
if (PHP_VERSION_ID < 80100) {
235+
$property->setAccessible(true);
236+
}
237+
223238
$property->setValue($object, $value);
224-
if ($revoke) {
239+
240+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
241+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
242+
if ($revoke && PHP_VERSION_ID < 80100) {
225243
$property->setAccessible(false);
226244
}
227245
}
@@ -240,9 +258,18 @@ protected function getInaccessibleProperty($object, $propertyName, $revoke = tru
240258
$class = $class->getParentClass();
241259
}
242260
$property = $class->getProperty($propertyName);
243-
$property->setAccessible(true);
261+
262+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
263+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
264+
if (PHP_VERSION_ID < 80100) {
265+
$property->setAccessible(true);
266+
}
267+
244268
$result = $property->getValue($object);
245-
if ($revoke) {
269+
270+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
271+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
272+
if ($revoke && PHP_VERSION_ID < 80100) {
246273
$property->setAccessible(false);
247274
}
248275

tests/framework/base/ActionFilterTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ public function testActive($filterClass)
111111
$filter = Yii::createObject($filterClass);
112112
$reflection = new \ReflectionClass($filter);
113113
$method = $reflection->getMethod('isActive');
114-
$method->setAccessible(true);
114+
115+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
116+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
117+
if (PHP_VERSION_ID < 80100) {
118+
$method->setAccessible(true);
119+
}
115120

116121
$controller = new \yii\web\Controller('test', Yii::$app);
117122

@@ -145,7 +150,12 @@ public function testActiveWildcard()
145150
$filter = new ActionFilter();
146151
$reflection = new \ReflectionClass($filter);
147152
$method = $reflection->getMethod('isActive');
148-
$method->setAccessible(true);
153+
154+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
155+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
156+
if (PHP_VERSION_ID < 80100) {
157+
$method->setAccessible(true);
158+
}
149159

150160
$controller = new \yii\web\Controller('test', Yii::$app);
151161

tests/framework/caching/FileCacheTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ public function testKeyPrefix()
7070
$refClass = new \ReflectionClass($cache);
7171

7272
$refMethodGetCacheFile = $refClass->getMethod('getCacheFile');
73-
$refMethodGetCacheFile->setAccessible(true);
73+
74+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
75+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
76+
if (PHP_VERSION_ID < 80100) {
77+
$refMethodGetCacheFile->setAccessible(true);
78+
}
79+
7480
$refMethodGet = $refClass->getMethod('get');
7581
$refMethodSet = $refClass->getMethod('set');
7682

@@ -91,7 +97,13 @@ public function testStatCache()
9197
$normalizeKey = $cache->buildKey(__FUNCTION__);
9298
$refClass = new \ReflectionClass($cache);
9399
$refMethodGetCacheFile = $refClass->getMethod('getCacheFile');
94-
$refMethodGetCacheFile->setAccessible(true);
100+
101+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
102+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
103+
if (PHP_VERSION_ID < 80100) {
104+
$refMethodGetCacheFile->setAccessible(true);
105+
}
106+
95107
$cacheFile = $refMethodGetCacheFile->invoke($cache, $normalizeKey);
96108

97109
// simulate cache expire 10 seconds ago

tests/framework/console/controllers/AssetControllerTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,8 @@ protected function createAssetSourceFiles(array $files, $fileBasePath = null)
183183
protected function invokeAssetControllerMethod($methodName, array $args = [])
184184
{
185185
$controller = $this->createAssetController();
186-
$controllerClassReflection = new \ReflectionClass(get_class($controller));
187-
$methodReflection = $controllerClassReflection->getMethod($methodName);
188-
$methodReflection->setAccessible(true);
189-
$result = $methodReflection->invokeArgs($controller, $args);
190-
$methodReflection->setAccessible(false);
191186

192-
return $result;
187+
return $this->invokeMethod($controller, $methodName, $args);
193188
}
194189

195190
/**

tests/framework/data/BaseDataProviderTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ public function testGenerateId()
1919
{
2020
$rc = new \ReflectionClass(BaseDataProvider::className());
2121
$rp = $rc->getProperty('counter');
22-
$rp->setAccessible(true);
22+
23+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
24+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
25+
if (PHP_VERSION_ID < 80100) {
26+
$rp->setAccessible(true);
27+
}
28+
2329
$rp->setValue(new ConcreteDataProvider(), null);
2430

2531
$this->assertNull((new ConcreteDataProvider())->id);

tests/framework/filters/HttpCacheTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ public function testValidateCache()
5252
{
5353
$httpCache = new HttpCache();
5454
$request = Yii::$app->getRequest();
55-
5655
$method = new \ReflectionMethod($httpCache, 'validateCache');
57-
$method->setAccessible(true);
56+
57+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
58+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
59+
if (PHP_VERSION_ID < 80100) {
60+
$method->setAccessible(true);
61+
}
5862

5963
$request->headers->remove('If-Modified-Since');
6064
$request->headers->remove('If-None-Match');

tests/framework/filters/auth/AuthMethodTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ public function testIsOptional()
7373
{
7474
$reflection = new \ReflectionClass(AuthMethod::className());
7575
$method = $reflection->getMethod('isOptional');
76-
$method->setAccessible(true);
76+
77+
// @link https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_reflectionsetaccessible
78+
// @link https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
79+
if (PHP_VERSION_ID < 80100) {
80+
$method->setAccessible(true);
81+
}
7782

7883
$filter = $this->createFilter(function () {return new \stdClass();});
7984

0 commit comments

Comments
 (0)