Git Product home page Git Product logo

Comments (14)

Bilge avatar Bilge commented on June 25, 2024 1

OK, I made https://github.com/Bilge/rector-fail.

from rector-symfony.

TomasVotruba avatar TomasVotruba commented on June 25, 2024 1

I've tried the repository and after linking the XML via SYMFONY_CONTAINER_XML_PATH_PARAMETER:

// rector.php
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Symfony\Set\SymfonySetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    // get parameters
    $parameters = $containerConfigurator->parameters();
    $parameters->set(Option::PATHS, [
        __DIR__ . '/src'
    ]);

    // path to dumped service container - run the project at least once to build container services map
    $parameters->set(Option::SYMFONY_CONTAINER_XML_PATH_PARAMETER,
    __DIR__ . '/var/cache/dev/OrgOrg_KernelDevDebugContainer.xml');

    $containerConfigurator->import(SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION);
};

...it works as expected 👍


Screenshot from 2021-10-23 19-11-05


See https://github.com/rectorphp/rector-symfony#use-sets

Could you complete your example? What effect has it on the result?
I do not have an access, otherwise I'd send PR to your repository.

from rector-symfony.

Bilge avatar Bilge commented on June 25, 2024 1

Yes, that does seem to work.

 class DefaultController extends Controller
 {
+    public function __construct(private \Org\Service\MyRegularService $myRegularService, private \Org\FirstBundle\Service\MyBundleService $myBundleService)
+    {
+    }
     public function indexAction()
     {
-        $regularService = $this->get('my.regular.service');
-        $bundleService = $this->get('my.bundle.service');
-        $regularServiceAsClassName = $this->get(MyRegularService::class);
+        $regularService = $this->myRegularService;
+        $bundleService = $this->myBundleService;
+        $regularServiceAsClassName = $this->myRegularService;

         return $this->render('FirstBundle:Default:index.html.twig');
     }

It's weird that that has always been in the readme. It does not really draw attention to itself there.

from rector-symfony.

TomasVotruba avatar TomasVotruba commented on June 25, 2024

Hi, thanks for this question.

Could you provide minimalistic diff of what exactly you have in mind? Just to be sure we understand your request correctly.

from rector-symfony.

Bilge avatar Bilge commented on June 25, 2024
<!-- services.xml -->
<service id="my.service" class="My\MyService">
    <argument type="service" id="My\OtherService" />
</service>
+use My\MyService;

class Foo extends Controller
{
    function bar()
    {
-        $this->get('my.service');
+        $this->get(MyService::class);
    }
}

Thereafter, the existing SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION Rector set could take over and do its job of refactoring the DI service grab into constructor injection, now that the service class name is used instead of the "machine name".

Note that this would not be possible in the case where two different service definitions are declared for the same class name. In this rare case, probably the fixer should do nothing and defer to manual intervention. If it was super smart, it could perhaps create a ServiceLocator or something, but that's out of scope here.

from rector-symfony.

TomasVotruba avatar TomasVotruba commented on June 25, 2024

Thanks for reporting. Looking at rules in linked set, this should be supported. I'm doing a bit cleanup of the rules just to be sure.

I'll need reproducible repository to verify where bug is. Could you share opened repository in Github? Ideally with single class and minimal composer.json. It will speed up th efix.

from rector-symfony.

Bilge avatar Bilge commented on June 25, 2024

What do you mean by, where bug is? There is no bug here. Either the feature exists or it doesn't. If it doesn't then this can be treated as a feature request. That is, unless you mean SYMFONY_CONSTRUCTOR_INJECTION is supposed to work for more than just services whose definition name matches the class name?

from rector-symfony.

TomasVotruba avatar TomasVotruba commented on June 25, 2024

The feature exists and should work.

See https://github.com/rectorphp/rector-symfony/blob/main/docs/rector_rules_overview.md#gettoconstructorinjectionrector

from rector-symfony.

Bilge avatar Bilge commented on June 25, 2024

I see. Is it likely to not work when the service definitions are in XML format and/or in many separate bundles? In old versions of Symfony, it was recommended that even all first-party code be placed inside bundles, and indeed is this case in this project, though those bundles are in the same repository and in the same directory structure as everything else, so it shouldn't matter too much...

from rector-symfony.

TomasVotruba avatar TomasVotruba commented on June 25, 2024

It works for specifically located xml config at the moment. That should be easy to implement 😉
Could you send the minimal repository with such example?

from rector-symfony.

TomasVotruba avatar TomasVotruba commented on June 25, 2024

Thank you, I'll look into it 👍

from rector-symfony.

TomasVotruba avatar TomasVotruba commented on June 25, 2024

That's issue with readmes. It's full of details that only one person needs :)

I'd be happy if you could send update of README.md that would help you next person in the situation 👍

from rector-symfony.

Bilge avatar Bilge commented on June 25, 2024

I was thinking of a few different options.

  1. Maybe a note next to the relevant fixer(s) or set(s) reminding to set the configuration option for those fixers that need it.
  2. Maybe a notice/warning generated by Rector itself if a fixer is used that requires the option but it is missing.

from rector-symfony.

TomasVotruba avatar TomasVotruba commented on June 25, 2024

Maybe a notice/warning generated by Rector itself if a fixer is used that requires the option but it is missing.

This would be awesome 👍

from rector-symfony.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.