Skip to content

Commit

Permalink
Merge pull request #266 from shochdoerfer/fix/extension_attribute_array
Browse files Browse the repository at this point in the history
Allow arrays in extension attributes
  • Loading branch information
shochdoerfer committed Aug 6, 2022
2 parents 67dd3a8 + b02e9f6 commit 217a3d7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function getAttrType(DOMElement $attr): string
$cleanType = str_replace('[]', '', $type);

$primitiveTypes = ['float', 'int', 'string', 'bool', 'boolean'];
return in_array(strtolower($cleanType), $primitiveTypes, true) ? $cleanType : '\\'.$cleanType;
return in_array(strtolower($cleanType), $primitiveTypes, true) ? $type : '\\'.$type;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/bitExpert/PHPStan/Magento/Autoload/ExtensionAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Laminas\Code\Generator\DocBlock\Tag\ReturnTag;
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\MethodGenerator;
use Laminas\Code\Generator\ParameterGenerator;
use PHPStan\Cache\Cache;

class ExtensionAutoloader implements Autoloader
Expand Down Expand Up @@ -88,6 +89,13 @@ public function getFileContents(string $className): string
* @see \Magento\Framework\Api\Code\Generator\ExtensionAttributesGenerator::_getClassMethods
*/

// treat array types properly in the generated code. Similar to Magento core MyInterface[] type gets
// converted to just an array
$paramType = $type;
if (strpos($type, '[]') !== false) {
$paramType = '?array';
}

$generator->addMethodFromGenerator(
MethodGenerator::fromArray([
'name' => 'get' . ucfirst($propertyName),
Expand All @@ -101,7 +109,7 @@ public function getFileContents(string $className): string
$generator->addMethodFromGenerator(
MethodGenerator::fromArray([
'name' => 'set' . ucfirst($propertyName),
'parameters' => [$propertyName],
'parameters' => [new ParameterGenerator($propertyName, $paramType)],
'docblock' => DocBlockGenerator::fromArray([
'tags' => [
new ParamTag($propertyName, [$type]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\InterfaceGenerator;
use Laminas\Code\Generator\MethodGenerator;
use Laminas\Code\Generator\ParameterGenerator;
use PHPStan\Cache\Cache;

class ExtensionInterfaceAutoloader implements Autoloader
Expand Down Expand Up @@ -104,6 +105,13 @@ public function getFileContents(string $interfaceName): string
* @see \Magento\Framework\Api\Code\Generator\ExtensionAttributesGenerator::_getClassMethods
*/

// treat array types properly in the generated code. Similar to Magento core MyInterface[] type gets
// converted to just an array
$paramType = $type;
if (strpos($type, '[]') !== false) {
$paramType = '?array';
}

$generator->addMethodFromGenerator(
MethodGenerator::fromArray([
'name' => 'get' . ucfirst($propertyName),
Expand All @@ -117,7 +125,7 @@ public function getFileContents(string $interfaceName): string
$generator->addMethodFromGenerator(
MethodGenerator::fromArray([
'name' => 'set' . ucfirst($propertyName),
'parameters' => [$propertyName],
'parameters' => [new ParameterGenerator($propertyName, $paramType)],
'docblock' => DocBlockGenerator::fromArray([
'tags' => [
new ParamTag($propertyName, [$type]),
Expand Down

0 comments on commit 217a3d7

Please sign in to comment.