Magicento icon indicating copy to clipboard operation
Magicento copied to clipboard

[Magento2] Generate/autocomplete factory classes

Open Koc opened this issue 8 years ago • 13 comments

    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 avatar Dec 18 '16 15:12 Koc

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

Koc avatar Jun 16 '17 12:06 Koc

@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?

enriquepiatti avatar Jun 16 '17 21:06 enriquepiatti

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.

Koc avatar Jun 19 '17 21:06 Koc

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

enriquepiatti avatar Jun 21 '17 15:06 enriquepiatti

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.

Koc avatar Jul 10 '17 17:07 Koc

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

enriquepiatti avatar Jul 11 '17 20:07 enriquepiatti

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.

Koc avatar Jul 12 '17 08:07 Koc

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

enriquepiatti avatar Jul 13 '17 22:07 enriquepiatti

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

default

Koc avatar Jul 18 '17 14:07 Koc

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

enriquepiatti avatar Jul 19 '17 18:07 enriquepiatti

@Koc just mark var directory as excluded.

orlangur avatar Sep 20 '17 14:09 orlangur

@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()->...

Koc avatar Sep 20 '17 14:09 Koc

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

orlangur avatar Sep 20 '17 14:09 orlangur