Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: ContainerGetToConstructorInjectionRector replaces constant with object #439

Closed
vlados opened this issue Jun 21, 2023 · 8 comments
Closed

Comments

@vlados
Copy link

vlados commented Jun 21, 2023

Code:

        $alertNotifications = $this->getAlertNotificationService()->updateAlertsNotificationsVisibility(
            $request->get((string) Constants::ALERT_NOTIFICATIONS)
        );

Result:

    ---------- begin diff ----------
@@ @@
     use ControllerExceptionHandling;

     public function __construct(
-        private readonly ManagerRegistry $managerRegistry
+        private readonly ManagerRegistry $managerRegistry, private readonly Constants $constants
     )
     {
     }
@@ @@
         return $em->wrapInTransaction(
             function () use ($request) {
                 $alertNotifications = $this->getAlertNotificationService()->updateAlertsNotificationsVisibility(
-                    $request->get(Constants::ALERT_NOTIFICATIONS),
+                    $this->constants,
                     false
                 );
    ----------- end diff -----------

Applied rules:
 * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
 * ContainerGetToConstructorInjectionRector

Expected result

    ---------- begin diff ----------
@@ @@
     use ControllerExceptionHandling;

     public function __construct(
-        private readonly ManagerRegistry $managerRegistry
+        private readonly ManagerRegistry $managerRegistry, private readonly Constants $constants
     )
     {
     }
@@ @@
         return $em->wrapInTransaction(
             function () use ($request) {
                 $alertNotifications = $this->getAlertNotificationService()->updateAlertsNotificationsVisibility(
-                    $request->get(Constants::ALERT_NOTIFICATIONS),
+                    $this->constants::ALERT_NOTIFICATIONS,
                     false
                 );
    ----------- end diff -----------

Applied rules:
 * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
 * ContainerGetToConstructorInjectionRector

Or how I can make this rule to be skipped for Constants?

@TomasVotruba
Copy link
Member

Thank you for your report!

We'll need an isolated failing demo link from: http://getrector.org/demo,
that way we can reproduce the bug.

@vlados
Copy link
Author

vlados commented Jun 23, 2023

I've tried but it returns no code change - https://getrector.com/demo/fcd3131b-2333-45d4-934b-e3d38f9b9acc
I am locally using latest version - 0.17.1

@stefantalen
Copy link
Contributor

The demo cannot guess your service definition so it doesn't know about AlertNotificationService and its method updateAlertsNotificationsVisibility 😉

You can add the class in the example

@vlados
Copy link
Author

vlados commented Jun 23, 2023

https://getrector.com/demo/d086fa78-f0b8-4c32-a7ac-6bc50c0d00d0
still nothing ... I think this demo have some problem

@stefantalen
Copy link
Contributor

Can you share the full file content of your first message? The diff shows an anonymous function structure that is not in any of your demo examples 🙂

My first thought is the ContainerGetToConstructorInjectionRector somehow gets triggered on the get() method for $request

@TomasVotruba
Copy link
Member

TomasVotruba commented Jun 24, 2023

Demo is not suitable for such a complex rule reproducer, as it need the class to exist and be part of dumped xml.
We'll probably need a reproducible Github repository, that will speed up the process :)

What are the parent types of your $request variable?

@samsonasik
Copy link
Member

I am closing it as no reproducer repo, feel free to reopen new issue with reproducer repo.

@bbrala
Copy link
Contributor

bbrala commented Apr 18, 2024

Reproduced the bug, yay, see related issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants