Skip to content

Commit 23f25f1

Browse files
authored
Do not call Reflection*::setAccessible() in PHP >= 8.1 (#1872)
1 parent 33a7838 commit 23f25f1

4 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/ErrorHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ final class ErrorHandler
153153
private function __construct()
154154
{
155155
$this->exceptionReflection = new \ReflectionProperty(\Exception::class, 'trace');
156-
$this->exceptionReflection->setAccessible(true);
156+
if (\PHP_VERSION_ID < 80100) {
157+
$this->exceptionReflection->setAccessible(true);
158+
}
157159
}
158160

159161
/**

src/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* capture_silenced_errors?: bool,
3030
* context_lines?: int|null,
3131
* default_integrations?: bool,
32-
* dsn?: string|bool|null|Dsn,
32+
* dsn?: string|bool|Dsn|null,
3333
* enable_logs?: bool,
3434
* environment?: string|null,
3535
* error_types?: int|null,

tests/FrameBuilderTest.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,16 @@ public function testGetFunctionArgumentsWithVariadicParameters(): void
300300

301301
$reflectionClass = new \ReflectionClass($frameBuilder);
302302
$getFunctionArgumentsMethod = $reflectionClass->getMethod('getFunctionArguments');
303-
$getFunctionArgumentsMethod->setAccessible(true);
303+
if (\PHP_VERSION_ID < 80100) {
304+
$getFunctionArgumentsMethod->setAccessible(true);
305+
}
304306

305307
$reflectionFunction = new \ReflectionFunction($testFunction);
306308

307309
$getFunctionArgumentValuesMethod = $reflectionClass->getMethod('getFunctionArgumentValues');
308-
$getFunctionArgumentValuesMethod->setAccessible(true);
310+
if (\PHP_VERSION_ID < 80100) {
311+
$getFunctionArgumentValuesMethod->setAccessible(true);
312+
}
309313

310314
$result = $getFunctionArgumentValuesMethod->invoke($frameBuilder, $reflectionFunction, $backtraceFrame['args']);
311315

@@ -331,7 +335,9 @@ public function testGetFunctionArgumentsWithOnlyVariadicParameters(): void
331335

332336
$reflectionClass = new \ReflectionClass($frameBuilder);
333337
$getFunctionArgumentValuesMethod = $reflectionClass->getMethod('getFunctionArgumentValues');
334-
$getFunctionArgumentValuesMethod->setAccessible(true);
338+
if (\PHP_VERSION_ID < 80100) {
339+
$getFunctionArgumentValuesMethod->setAccessible(true);
340+
}
335341

336342
$reflectionFunction = new \ReflectionFunction($testFunction);
337343

@@ -356,7 +362,9 @@ public function testGetFunctionArgumentsWithEmptyVariadicParameters(): void
356362

357363
$reflectionClass = new \ReflectionClass($frameBuilder);
358364
$getFunctionArgumentValuesMethod = $reflectionClass->getMethod('getFunctionArgumentValues');
359-
$getFunctionArgumentValuesMethod->setAccessible(true);
365+
if (\PHP_VERSION_ID < 80100) {
366+
$getFunctionArgumentValuesMethod->setAccessible(true);
367+
}
360368

361369
$reflectionFunction = new \ReflectionFunction($testFunction);
362370

@@ -383,7 +391,9 @@ public function testGetFunctionArgumentsWithNullValues(): void
383391

384392
$reflectionClass = new \ReflectionClass($frameBuilder);
385393
$getFunctionArgumentValuesMethod = $reflectionClass->getMethod('getFunctionArgumentValues');
386-
$getFunctionArgumentValuesMethod->setAccessible(true);
394+
if (\PHP_VERSION_ID < 80100) {
395+
$getFunctionArgumentValuesMethod->setAccessible(true);
396+
}
387397

388398
$reflectionFunction = new \ReflectionFunction($testFunction);
389399

@@ -418,7 +428,9 @@ public function testGetFunctionArgumentsWithGapsInBacktraceArrayIndices(): void
418428

419429
$reflectionClass = new \ReflectionClass($frameBuilder);
420430
$getFunctionArgumentValuesMethod = $reflectionClass->getMethod('getFunctionArgumentValues');
421-
$getFunctionArgumentValuesMethod->setAccessible(true);
431+
if (\PHP_VERSION_ID < 80100) {
432+
$getFunctionArgumentValuesMethod->setAccessible(true);
433+
}
422434

423435
$reflectionFunction = new \ReflectionFunction($testFunction);
424436

tests/SentrySdkExtension.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,30 @@ final class SentrySdkExtension implements BeforeTestHookInterface
1414
public function executeBeforeTest(string $test): void
1515
{
1616
$reflectionProperty = new \ReflectionProperty(SentrySdk::class, 'currentHub');
17-
$reflectionProperty->setAccessible(true);
17+
if (\PHP_VERSION_ID < 80100) {
18+
$reflectionProperty->setAccessible(true);
19+
}
1820
$reflectionProperty->setValue(null, null);
19-
$reflectionProperty->setAccessible(false);
21+
if (\PHP_VERSION_ID < 80100) {
22+
$reflectionProperty->setAccessible(false);
23+
}
2024

2125
$reflectionProperty = new \ReflectionProperty(Scope::class, 'globalEventProcessors');
22-
$reflectionProperty->setAccessible(true);
26+
if (\PHP_VERSION_ID < 80100) {
27+
$reflectionProperty->setAccessible(true);
28+
}
2329
$reflectionProperty->setValue(null, []);
24-
$reflectionProperty->setAccessible(false);
30+
if (\PHP_VERSION_ID < 80100) {
31+
$reflectionProperty->setAccessible(false);
32+
}
2533

2634
$reflectionProperty = new \ReflectionProperty(IntegrationRegistry::class, 'integrations');
27-
$reflectionProperty->setAccessible(true);
35+
if (\PHP_VERSION_ID < 80100) {
36+
$reflectionProperty->setAccessible(true);
37+
}
2838
$reflectionProperty->setValue(IntegrationRegistry::getInstance(), []);
29-
$reflectionProperty->setAccessible(false);
39+
if (\PHP_VERSION_ID < 80100) {
40+
$reflectionProperty->setAccessible(false);
41+
}
3042
}
3143
}

0 commit comments

Comments
 (0)