enriquepiatti / Magicento

PHPStorm plugin for Magento developers
115 stars 35 forks source link

[Magento2] Generate/autocomplete factory classes #219

Open Koc opened 7 years ago

Koc commented 7 years ago
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Catalog\Helper\Category $catalogCategory,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Model\Indexer\Category\Flat\State $flatState,
        array $data = []
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_catalogLayer = $layerResolver->get();
        $this->httpContext = $httpContext;
        $this->_catalogCategory = $catalogCategory;
        $this->_registry = $registry;
        $this->flatState = $flatState;
        $this->_categoryInstance = $categoryFactory->create();
        parent::__construct($context, $data);
    }

CategoryFactory and CollectionFactory marks as undefined because they are generated at runtime. Magicento can detect this kind of classes and create they on the fly and autocompletes it.

Koc commented 7 years ago

@enriquepiatti please, please, please, fix this :)

enriquepiatti commented 7 years ago

@Koc what is your proposal? if the classes are created when Magento is executed, is not easier just to run Magento and let PhpStorm find the class after that?

Koc commented 7 years ago

Some project created as remote project by sftp. And we need each time make sync for downloading factory classes. And newly just created classes haven't factories unntil they will requested - so no any autocomplete here. Magento has long namespaces and easy make mistake and type wrong class or ns.

enriquepiatti commented 7 years ago

OK, but what is the proposal for that then? PhpStorm need the class, the only solution I can think is to create a fake class somewhere, but then it could confuse the code when you do the sync with the real class, I will think about it a bit more... but you don't have a local Magento2 in your environment? to test your code? it should be enough to create the classes just executing your local Magento

Koc commented 7 years ago

but you don't have a local Magento2 in your environment? to test your code?

I am testing my code on virtual machine and it configured as sftp https://www.jetbrains.com/help/phpstorm/creating-a-remote-server-configuration.html

The other problem that Magento generates too many Interceptors classes. And this classes should be ignored. But they are located not in separated folders, between factories. So better way just ignore full var dir and generate fake factory classes by plugin.

enriquepiatti commented 7 years ago

I don't see very clear this solution, or even if it's a good idea, normally you have a local environment to test, because is faster too, and you can break it without messing other things, and also because you can debug it, and when you debug the code you will probably need those interceptors and factory, the real ones. I guess you could sync in both ways your SFTP, so your local project will download the files from var/generation and PhpStorm will understand the code better

Koc commented 7 years ago

Magento 2 not supports Windows oficially http://devdocs.magento.com/guides/v2.1/install-gde/system-requirements-tech.html . I am working on Windows with Ubuntu launched on Virtualbox.

Also on Linux it runs faster.

enriquepiatti commented 7 years ago

Yes but you can sync your code in both ways anyway with SFTP, just download the var/generation folder from your Magento2 and PhpStorm will recognize and use the real classes

Koc commented 7 years ago

How can we stop this madness with intercepror classes and exclude they from any search by default?

default

enriquepiatti commented 7 years ago

Not sure if that's possible. But you could just search inside app/code or inside vendor folders. Or I guess you could just ignore those results, they are easy to identify. But let me know if this is becoming a problem for some other reason, or is slowing you somehow

EDIT: actually, I think you could also create a custom "scope" and search on that always

orlangur commented 7 years ago

@Koc just mark var directory as excluded.

Koc commented 7 years ago

@orlangur in this case there is no autocomplete of factory classes and I've got a tons of undefined factory classes. And no autocomplete of $factory->create()->...

orlangur commented 7 years ago

True, just noticed all but var/generation is excluded in my setup, looks like just always ignored them during search.