phpstan-magento: Generated class ProductExtension is passed on incorrectly to phpstan
Hi there! Just updated from v0.23.1 (and phpstan 1.7) to v0.24.0 (and phpstan 1.8). Since the update I’m getting the following error when running phpstan:
Child process error (exit code 255): PHP Fatal error: Declaration of
Magento\Catalog\Api\Data\ProductExtension::setWebsiteIds(?array
$websiteIds) must be compatible with
Magento\Catalog\Api\Data\ProductExtensionInterface::setWebsiteIds($websit
eIds) in
/tmp/phpstan/cache/PHPStan/95/c3/95c3cd23c340083ec99b41a2ad173115a2e32d1c
.php on line 109
Fatal error: Declaration of
Magento\Catalog\Api\Data\ProductExtension::setWebsiteIds(?array
$websiteIds) must be compatible with
Magento\Catalog\Api\Data\ProductExtensionInterface::setWebsiteIds($websit
eIds) in
/tmp/phpstan/cache/PHPStan/95/c3/95c3cd23c340083ec99b41a2ad173115a2e32d1c
.php on line 109
Those are two generated classes from Magento: ProductExtensionInterface
<?php
namespace Magento\Catalog\Api\Data;
/**
* ExtensionInterface class for @see \Magento\Catalog\Api\Data\ProductInterface
*/
interface ProductExtensionInterface extends \Magento\Framework\Api\ExtensionAttributesInterface
{
/**
* @return int[]|null
*/
public function getWebsiteIds();
/**
* @param int[] $websiteIds
* @return $this
*/
public function setWebsiteIds($websiteIds);
ProductExtension
<?php
namespace Magento\Catalog\Api\Data;
/**
* Extension class for @see \Magento\Catalog\Api\Data\ProductInterface
*/
class ProductExtension extends \Magento\Framework\Api\AbstractSimpleObject implements ProductExtensionInterface
{
/**
* @return int[]|null
*/
public function getWebsiteIds()
{
return $this->_get('website_ids');
}
/**
* @param int[] $websiteIds
* @return $this
*/
public function setWebsiteIds($websiteIds)
{
$this->setData('website_ids', $websiteIds);
return $this;
}
As we can see both classes are generated correctly. In the error message it says that Magento\Catalog\Api\Data\ProductExtension::setWebsiteIds(?array $websiteIds)
must be compatible with its interface declaration but in the actual code there is no typed parameter ?array
so it has to be somehow generated and passed to phpstan virtually.
FYI: I deleted the phpstan cache before running phpstan.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 30 (15 by maintainers)
I’ve just published version 0.25.0 of the extension.
Thanks for the update @shochdoerfer
It fixed the issue for me. After upgrading to dev-master + clearing phpstan cache, error is gone.
@tszmyt apologies for this mess. I realize that the naive approach to simply generate the files based on the XML configuration is not enough. If extension interfaces already exist, we need to inspect them to see if typehints are used in the method declarations or not.
Unfortunately, the issue is still the same in my case, when trying it with your fix. @oneserv-heuser , yes, indeed, we do. Maybe that’s the reason? @shochdoerfer , I can not come this weekend, but @sprankhub will be there and will be happy to show you the code. Do you have any idea, what we could check, to make it easier to debug for you?
Here are the two classes ProductExtension and ProductExtensionInterface as generated as of Magento 2.4.4 and 2.4.5:
Magento 2.4.4
ProductExtensionInterface
ProductExtension
Magento 2.4.5
ProductExtensionInterface
ProductExtension
FYI: The functions getCalculationSchemaId and setCalculationSchemaId are added by us via ExtensionAttribute functionality.
Edit: Just did a text-compare, they are exactly the same, only the functions getDiscounts and setDiscounts are new, but they appear correctly in the Interface and the concrete class