Skip to content

Commit 15beba9

Browse files
committed
Allow arrays in extension attributes
1 parent 821bd32 commit 15beba9

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/bitExpert/PHPStan/Magento/Autoload/DataProvider/ExtensionAttributeDataProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected function getAttrType(DOMElement $attr): string
113113
$cleanType = str_replace('[]', '', $type);
114114

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

119119
/**

src/bitExpert/PHPStan/Magento/Autoload/ExtensionAutoloader.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Laminas\Code\Generator\DocBlock\Tag\ReturnTag;
1919
use Laminas\Code\Generator\DocBlockGenerator;
2020
use Laminas\Code\Generator\MethodGenerator;
21+
use Laminas\Code\Generator\ParameterGenerator;
2122
use PHPStan\Cache\Cache;
2223

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

92+
// treat array types properly in the generated code. Similar to Magento core MyInterface[] type gets
93+
// converted to just an array
94+
$paramType = $type;
95+
if (strpos($type, '[]') !== false) {
96+
$paramType = '?array';
97+
}
98+
9199
$generator->addMethodFromGenerator(
92100
MethodGenerator::fromArray([
93101
'name' => 'get' . ucfirst($propertyName),
@@ -101,7 +109,7 @@ public function getFileContents(string $className): string
101109
$generator->addMethodFromGenerator(
102110
MethodGenerator::fromArray([
103111
'name' => 'set' . ucfirst($propertyName),
104-
'parameters' => [$propertyName],
112+
'parameters' => [new ParameterGenerator($propertyName, $paramType)],
105113
'docblock' => DocBlockGenerator::fromArray([
106114
'tags' => [
107115
new ParamTag($propertyName, [$type]),

src/bitExpert/PHPStan/Magento/Autoload/ExtensionInterfaceAutoloader.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Laminas\Code\Generator\DocBlockGenerator;
2020
use Laminas\Code\Generator\InterfaceGenerator;
2121
use Laminas\Code\Generator\MethodGenerator;
22+
use Laminas\Code\Generator\ParameterGenerator;
2223
use PHPStan\Cache\Cache;
2324

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

108+
// treat array types properly in the generated code. Similar to Magento core MyInterface[] type gets
109+
// converted to just an array
110+
$paramType = $type;
111+
if (strpos($type, '[]') !== false) {
112+
$paramType = '?array';
113+
}
114+
107115
$generator->addMethodFromGenerator(
108116
MethodGenerator::fromArray([
109117
'name' => 'get' . ucfirst($propertyName),
@@ -117,7 +125,7 @@ public function getFileContents(string $interfaceName): string
117125
$generator->addMethodFromGenerator(
118126
MethodGenerator::fromArray([
119127
'name' => 'set' . ucfirst($propertyName),
120-
'parameters' => [$propertyName],
128+
'parameters' => [new ParameterGenerator($propertyName, $paramType)],
121129
'docblock' => DocBlockGenerator::fromArray([
122130
'tags' => [
123131
new ParamTag($propertyName, [$type]),

0 commit comments

Comments
 (0)