Giter VIP home page Giter VIP logo

Comments (7)

shochdoerfer avatar shochdoerfer commented on August 26, 2024 1

Damn it, thanks for catching this. I only tested with primitives. Fix looks good, would have done the same. I'll borrow the code and then merge.

Thanks for your contribution!

from phpstan-magento.

shochdoerfer avatar shochdoerfer commented on August 26, 2024

Thank you for this bug report. Technically it should not be possible to proxy an interface, that is what classes implementing an interface are for. You cannot use an interface and extend from it, thus the error above is correct.

Does Magento really allow to create proxies for interfaces?

from phpstan-magento.

nicojust avatar nicojust commented on August 26, 2024

Yes it does. Below are the contents of the autogenerated file from magento.

generated/code/Magento/Catalog/Api/ProductRepositoryInterface/Proxy.php

<?php
namespace Magento\Catalog\Api\ProductRepositoryInterface;

/**
 * Proxy class for @see \Magento\Catalog\Api\ProductRepositoryInterface
 */
class Proxy implements \Magento\Catalog\Api\ProductRepositoryInterface, \Magento\Framework\ObjectManager\NoninterceptableInterface
{
    /**
     * Object Manager instance
     *
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $_objectManager = null;

    /**
     * Proxied instance name
     *
     * @var string
     */
    protected $_instanceName = null;

    /**
     * Proxied instance
     *
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $_subject = null;

    /**
     * Instance shareability flag
     *
     * @var bool
     */
    protected $_isShared = null;

    /**
     * Proxy constructor
     *
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     * @param string $instanceName
     * @param bool $shared
     */
    public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $instanceName = '\\Magento\\Catalog\\Api\\ProductRepositoryInterface', $shared = true)
    {
        $this->_objectManager = $objectManager;
        $this->_instanceName = $instanceName;
        $this->_isShared = $shared;
    }

    /**
     * @return array
     */
    public function __sleep()
    {
        return ['_subject', '_isShared', '_instanceName'];
    }

    /**
     * Retrieve ObjectManager from global scope
     */
    public function __wakeup()
    {
        $this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    }

    /**
     * Clone proxied instance
     */
    public function __clone()
    {
        $this->_subject = clone $this->_getSubject();
    }

    /**
     * Get proxied instance
     *
     * @return \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected function _getSubject()
    {
        if (!$this->_subject) {
            $this->_subject = true === $this->_isShared
                ? $this->_objectManager->get($this->_instanceName)
                : $this->_objectManager->create($this->_instanceName);
        }
        return $this->_subject;
    }

    /**
     * {@inheritdoc}
     */
    public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false)
    {
        return $this->_getSubject()->save($product, $saveOptions);
    }

    /**
     * {@inheritdoc}
     */
    public function get($sku, $editMode = false, $storeId = null, $forceReload = false)
    {
        return $this->_getSubject()->get($sku, $editMode, $storeId, $forceReload);
    }

    /**
     * {@inheritdoc}
     */
    public function getById($productId, $editMode = false, $storeId = null, $forceReload = false)
    {
        return $this->_getSubject()->getById($productId, $editMode, $storeId, $forceReload);
    }

    /**
     * {@inheritdoc}
     */
    public function delete(\Magento\Catalog\Api\Data\ProductInterface $product)
    {
        return $this->_getSubject()->delete($product);
    }

    /**
     * {@inheritdoc}
     */
    public function deleteById($sku)
    {
        return $this->_getSubject()->deleteById($sku);
    }

    /**
     * {@inheritdoc}
     */
    public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
    {
        return $this->_getSubject()->getList($searchCriteria);
    }
}

from phpstan-magento.

shochdoerfer avatar shochdoerfer commented on August 26, 2024

Thanks for pointing out. Will check how to fix this.

from phpstan-magento.

shochdoerfer avatar shochdoerfer commented on August 26, 2024

Still on this, got a wired side effect due some additional autoloading. Still need to figure out what the source of the problem is...

from phpstan-magento.

shochdoerfer avatar shochdoerfer commented on August 26, 2024

@nicojust could you please give #13 a try. Seems to work in my test case, wondering if it would fix your problem as well.

from phpstan-magento.

nicojust avatar nicojust commented on August 26, 2024

@shochdoerfer
Thank you for your effort.

Unfortunately it still seems to be causing problems.
In one of my classes i'm injecting the ProductRepositoryInterface via Proxy like this.

ProductHelper

<?php

use Magento\Catalog\Api\ProductRepositoryInterface;

class ProductHelper
{
    /**
     * @var ProductRepositoryInterface
     */
    private $productRepository;

    /**
     * @param ProductRepositoryInterface\Proxy $productRepository
     */
    public function __construct(ProductRepositoryInterface\Proxy $productRepository)
    {
        $this->productRepository = $productRepository;
    }
}

While analysing the following error messages pop up.

PHP Warning: Uncaught PHPStan\Broker\ClassAutoloadingException: Class Magento\Catalog\Api\ProductRepositoryInterface\Magento\Catalog\Api\Data\ProductInterface not found and could not be autoloaded. in vendor/phpstan/phpstan/src/Broker/Broker.php:358

PHP Fatal error: Declaration of Magento\Catalog\Api\ProductRepositoryInterface\Proxy::save(Magento\Catalog\Api\ProductRepositoryInterface\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = NULL) must be compatible with Magento\Catalog\Api\ProductRepositoryInterface::save(Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false) in /tmp/PSMP9fViSr on line 6

/tmp/PSMP9fViSr

<?php
namespace Magento\Catalog\Api\ProductRepositoryInterface;
/**
 * Proxy class for @see Magento\Catalog\Api\ProductRepositoryInterface
 */
class Proxy implements \Magento\Framework\ObjectManager\NoninterceptableInterface, \Magento\Catalog\Api\ProductRepositoryInterface
{
    /**
     * @return array
     */
    public function __sleep() {}
    /**
     * Retrieve ObjectManager from global scope
     */
    public function __wakeup() {}
    /**
     * Clone proxied instance
     */
    public function __clone() {}
    public function save(Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = NULL) {}

    public function get($sku, $editMode = NULL, $storeId = NULL, $forceReload = NULL) {}

    public function getById($productId, $editMode = NULL, $storeId = NULL, $forceReload = NULL) {}

    public function delete(Magento\Catalog\Api\Data\ProductInterface $product) {}

    public function deleteById($sku) {}

    public function getList(Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) {}

}

I think the problem is that the type hints for parameters are searched within the current namespace.
For example it should be:

public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = NULL) {}

and not

public function save(Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = NULL) {}

I managed to fix it by checking if the provided type is built in or not and prefixing it with \.

ProxyAutoloader.php:42

$paramType = $parameter->getType() ?: '';
if ($paramType instanceof \ReflectionType) {
    if ($paramType->isBuiltin()) {
        $paramType = $paramType->getName() . ' ';
    } else {
        $paramType = '\\' . $paramType->getName() . ' ';
    }
}

Not sure if that is the correct way to fix this problem.

from phpstan-magento.

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.