Skip to content

Conversation

@NickSdot
Copy link
Contributor

@NickSdot NickSdot commented Jul 23, 2025

As of PHP 8.1.0, calling Reflection*::setAccessible() has no effect; all properties are accessible by default. Currently, there is a RFC which proposes to issue a deprecation warning starting with Php 8.5.

Note:
I added it to the 8.5 rules, because it will likely be decprecated by then. Alternatively, we could add it to the 8.1 rules. Let me know if you think this would be more sensible.

@NickSdot NickSdot force-pushed the feat/remove-setAccessible-rector branch 3 times, most recently from 4087338 to 2d84438 Compare July 23, 2025 03:34
@NickSdot

This comment was marked as resolved.

@NickSdot NickSdot changed the title [Php85] added RemoveDeprecatedReflectionSetAccessibleCallsRector [Php85] added RemoveReflectionSetAccessibleCallsRector Jul 23, 2025
@samsonasik
Copy link
Member

You may need to install gpatch and register to path, see troubleshooting note

https://github.com/symplify/vendor-patches/?tab=readme-ov-file#troubleshooting

@NickSdot

This comment has been minimized.

@TomasVotruba
Copy link
Member

Thanks for the rule. Targeting 8.1 would be better, as we add rules as soon the change appears in PHP.

@samsonasik
Copy link
Member

samsonasik commented Jul 23, 2025

@TomasVotruba there is no "deprecation" warning on php 8.1, it just have no effect, see https://3v4l.org/ukI3N#v8.1.33

For note: downgrade to php 8.0 rule is needed for it, to add:

if (PHP_VERSION_ID <=80100) {
    $r->setAccessible(true);
}

to avoid error on downgrade upgraded code, see https://3v4l.org/ukI3N#v8.0.30

@NickSdot NickSdot force-pushed the feat/remove-setAccessible-rector branch from 2d84438 to 84deedb Compare July 23, 2025 07:17
@TomasVotruba
Copy link
Member

@samsonasik Exactly, it's no longer required. That's why it should be in 8.1 as a dead code

@NickSdot
Copy link
Contributor Author

Thanks for the rule. Targeting 8.1 would be better, as we add rules as soon the change appears in PHP.

@TomasVotruba That's done.

TomasVotruba there is no "deprecation" warning on php 8.1, it just have no effect, see https://3v4l.org/ukI3N#v8.1.33

@samsonasik That's correct. However, to have the calls in >=8.1 is useless either way

@NickSdot NickSdot force-pushed the feat/remove-setAccessible-rector branch from 84deedb to 5b59464 Compare July 23, 2025 07:30
@samsonasik
Copy link
Member

We have setAccessible call in rector-src code:

$reflectionProperty->setAccessible(true);

If bin/rector executed, that will be removed, then downgrade will error on php 7.4 scoped, the downgrade rule needs to cover this first at

https://github.com/rectorphp/rector-downgrade-php/tree/main/rules/DowngradePhp80

Then register at

https://github.com/rectorphp/rector-downgrade-php/blob/main/config/set/downgrade-php80.php

@NickSdot
Copy link
Contributor Author

We have setAccessible call in rector-src code:

$reflectionProperty->setAccessible(true);

If bin/rector executed, that will be removed, then downgrade will error on php 7.4 scoped, the downgrade rule needs to cover this first at

https://github.com/rectorphp/rector-downgrade-php/tree/main/rules/DowngradePhp80

Then register at

https://github.com/rectorphp/rector-downgrade-php/blob/main/config/set/downgrade-php80.php

@samsonasik, I never looked into downgrade rules. I assume it must also cover situations where no upgrade rule was run, and where the $reflectionProperty->setAccessible(true); is already present. I have no idea how to avoid duplication in such situations. Can you think of an existing rule that does something similar? I could try to come up with a PR in the downgrade rules repo if I have an example to orient myself.

@TomasVotruba
Copy link
Member

I think we have this covered in PHP 8.1 downgrade set:
https://github.com/rectorphp/rector-downgrade-php/blob/main/config/set/downgrade-php81.php#L19

I made sure it works 👍 rectorphp/rector-downgrade-php#155

@samsonasik samsonasik changed the title [Php85] added RemoveReflectionSetAccessibleCallsRector [Php81] added RemoveReflectionSetAccessibleCallsRector Jul 23, 2025
@samsonasik
Copy link
Member

@TomasVotruba thank you for verify

@NickSdot could you try run:

bin/rector

then ensure this line removed

$reflectionProperty->setAccessible(true);

then, we can merge to see if it can be downgraded correctly, thank you.

@samsonasik
Copy link
Member

@TomasVotruba the downgrade rule only handle ReflectionProperty, but not ReflectionMethod, I will add that first ...

@NickSdot
Copy link
Contributor Author

@TomasVotruba thank you for verify

@NickSdot could you try run:

bin/rector

then ensure this line removed

$reflectionProperty->setAccessible(true);

then, we can merge to see if it can be downgraded correctly, thank you.

@samsonasik, yep, that works. There are more than one call of it.

Subject: [PATCH] chore: ran rector
---
Index: src/Util/Reflection/PrivatesAccessor.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Util/Reflection/PrivatesAccessor.php b/src/Util/Reflection/PrivatesAccessor.php
--- a/src/Util/Reflection/PrivatesAccessor.php	(revision 5b59464b0a17c34b1f492898660e25cedb02841b)
+++ b/src/Util/Reflection/PrivatesAccessor.php	(date 1753259388951)
@@ -47,7 +47,6 @@
     public function getPrivateProperty(object $object, string $propertyName): mixed
     {
         $reflectionProperty = $this->resolvePropertyReflection($object, $propertyName);
-        $reflectionProperty->setAccessible(true);
 
         return $reflectionProperty->getValue($object);
     }
@@ -55,17 +54,13 @@
     public function setPrivateProperty(object $object, string $propertyName, mixed $value): void
     {
         $reflectionProperty = $this->resolvePropertyReflection($object, $propertyName);
-        $reflectionProperty->setAccessible(true);
 
         $reflectionProperty->setValue($object, $value);
     }
 
     private function createAccessibleMethodReflection(object $object, string $methodName): ReflectionMethod
     {
-        $reflectionMethod = new ReflectionMethod($object, $methodName);
-        $reflectionMethod->setAccessible(true);
-
-        return $reflectionMethod;
+        return new ReflectionMethod($object, $methodName);
     }
 
     private function resolvePropertyReflection(object $object, string $propertyName): ReflectionProperty

There are also some formatting changes that composer complete-check didn't report. Is this something you want me to commit or not?

Subject: [PATCH] chore: ran rector
---
Index: rules/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/rules/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector.php b/rules/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector.php
--- a/rules/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector.php	(revision 5b59464b0a17c34b1f492898660e25cedb02841b)
+++ b/rules/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector.php	(date 1753259379572)
@@ -47,8 +47,8 @@
             return null;
         }
 
-        if ($this->isObjectType($methodCall->var, new ObjectType('ReflectionProperty')) === true
-            || $this->isObjectType($methodCall->var, new ObjectType('ReflectionMethod')) === true
+        if ($this->isObjectType($methodCall->var, new ObjectType('ReflectionProperty'))
+            || $this->isObjectType($methodCall->var, new ObjectType('ReflectionMethod'))
         ) {
             return NodeVisitor::REMOVE_NODE;
         }
Index: rules-tests/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector/RemoveReflectionSetAccessibleCallsRectorTest.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/rules-tests/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector/RemoveReflectionSetAccessibleCallsRectorTest.php b/rules-tests/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector/RemoveReflectionSetAccessibleCallsRectorTest.php
--- a/rules-tests/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector/RemoveReflectionSetAccessibleCallsRectorTest.php	(revision 5b59464b0a17c34b1f492898660e25cedb02841b)
+++ b/rules-tests/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector/RemoveReflectionSetAccessibleCallsRectorTest.php	(date 1753259374024)
@@ -4,6 +4,7 @@
 
 namespace Rector\Tests\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector;
 
+use Iterator;
 use PHPUnit\Framework\Attributes\DataProvider;
 use Rector\Testing\PHPUnit\AbstractRectorTestCase;
 
@@ -16,9 +17,9 @@
     }
 
     /**
-     * @return \Iterator<array<string>>
+     * @return Iterator<array<string>>
      */
-    public static function provideData(): \Iterator
+    public static function provideData(): Iterator
     {
         return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
     }

@samsonasik
Copy link
Member

@NickSdot
Copy link
Contributor Author

Two questions:

  • Is the location rules/Php81 ok now or should it be rules/DeadCode
  • Do we need to add this rule to config/set/dead-code.php?

@samsonasik
Copy link
Member

@NickSdot I've just ability to downgrade ReflectionMethod

On php81 set is ok for this :)

@NickSdot could you run and commit the change for it so we can test downgrade is working? Thank you.

bin/rector
git commit -a

@NickSdot
Copy link
Contributor Author

@samsonasik committed and pushed.

FYI: composer check-cs and bin/rector clash in ClosureFromCallableToFirstClassCallableRector.php

@samsonasik
Copy link
Member

It seems you need to rebase latest main

$reflectionMethod->setAccessible(true);

return $reflectionMethod;
return new ReflectionMethod($object, $methodName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the downgrade rule seems not handling on return yet, only Expression, I will look into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: DowngradeSetAccessibleReflectionPropertyRector has "Property" in the name. Your last PR rectorphp/rector-downgrade-php#296 added handling for methods. Just pointing it out in case that's relevant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that isNames() for :)

if (! $this->isNames($new->class, ['ReflectionProperty', 'ReflectionMethod'])) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I just meant that the rule name now no longer exactly fits to what the rule does.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that can be refactored next (if needed :) ), but we need to ensure downgrade working first :)

@samsonasik
Copy link
Member

Okay, preparation is done for our use cases :), @TomasVotruba let's merge.

Thank you @NickSdot

@samsonasik samsonasik merged commit 6d046c1 into rectorphp:main Jul 23, 2025
47 checks passed
@NickSdot
Copy link
Contributor Author

That was fun! 🤝

@samsonasik
Copy link
Member

It seems downgraded correctly

rectorphp/rector@415cc1a#diff-aa4acc8abf2aa549804bc88f2b13eb203d8a18dd8f8a6e9db9289038a4ab1a2d

On resolvePropertyReflection next statement is removed setAccessible(true), but on method resolvePropertyReflection() itself, the setAccessible(true) already added, so it seems safe :)

@NickSdot NickSdot deleted the feat/remove-setAccessible-rector branch July 23, 2025 09:31
@github-actions
Copy link
Contributor

This pull request has been automatically locked because it has been closed for 150 days. Please open a new PR if you want to continue the work.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants