Skip to content

Commit e586d77

Browse files
committed
phpstan
1 parent 7276afd commit e586d77

10 files changed

Lines changed: 72 additions & 51 deletions

phpstan.neon

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,9 @@ parameters:
7777
inferPrivatePropertyTypeFromConstructor: true
7878
ignoreErrors:
7979
# False positive
80-
- '#Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition::children\(\)\.#'
80+
-
81+
message: '#Possibly invalid array key type string\|null\.#'
82+
path: src/AttributeGenerator/ConfigurationAttributeGenerator.php
83+
-
84+
message: '#Possibly invalid array key type string\|null\.#'
85+
path: src/TypesGenerator.php

src/AttributeGenerator/ApiPlatformCoreAttributeGenerator.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static function (?string $type) use ($class): void {
137137
*/
138138
private static function extractParameters(string $type, array $values): array
139139
{
140-
$types = self::$parameterTypes[$type] ??=
140+
$types = self::$parameterTypes[$type] ??= (
141141
(static::PRAMETER_TYPE_HINTS[$type] ?? []) + array_reduce(
142142
(new \ReflectionClass($type))->getConstructor()?->getParameters() ?? [],
143143
static fn (array $types, \ReflectionParameter $refl): array => $types + [
@@ -147,28 +147,8 @@ private static function extractParameters(string $type, array $values): array
147147
: null,
148148
],
149149
[]
150-
);
151-
if (isset(self::$parameterTypes[$type])) {
152-
$types = self::$parameterTypes[$type];
153-
} else {
154-
$types = static::PRAMETER_TYPE_HINTS[$type] ?? [];
155-
$parameterRefls = (new \ReflectionClass($type))
156-
->getConstructor()
157-
?->getParameters() ?? [];
158-
foreach ($parameterRefls as $refl) {
159-
$paramName = $refl->getName();
160-
if (\array_key_exists($paramName, $types)) {
161-
continue;
162-
}
163-
$paramType = $refl->getType();
164-
if ($paramType instanceof \ReflectionNamedType && !$paramType->isBuiltin()) {
165-
$types[$paramName] = $paramType->getName();
166-
} else {
167-
$types[$paramName] = null;
168-
}
169-
}
170-
self::$parameterTypes[$type] = $types;
171-
}
150+
)
151+
);
172152

173153
$parameters = array_intersect_key($values, $types);
174154
foreach ($parameters as $name => $parameter) {

src/AttributeGenerator/ConfigurationAttributeGenerator.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,19 @@ public function generateClassAttributes(Class_ $class): array
2929
$vocabAttributes = $this->config['vocabularies'][$class->resource()->getGraph()->getUri()]['attributes'] ?? [[]];
3030
}
3131

32-
$getAttributesNames = static fn (array $config) => $config === [[]]
33-
? []
34-
: array_unique(array_map(static fn (array $v) => array_keys($v)[0], $config));
32+
$getAttributesNames = static function (array $config): array {
33+
if ($config === [[]]) {
34+
return [];
35+
}
36+
37+
$attributeNames = array_filter(array_map(static function (array $v): ?string {
38+
$firstKey = array_key_first($v);
39+
40+
return \is_string($firstKey) ? $firstKey : null;
41+
}, $config), static fn (?string $name): bool => null !== $name);
42+
43+
return array_unique($attributeNames);
44+
};
3545
$typeAttributesNames = $getAttributesNames($typeAttributes);
3646
$vocabAttributesNames = $getAttributesNames($vocabAttributes);
3747

src/ClassMutator/ClassParentMutator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ public function __invoke(Class_ $class, array $context): void
5858
}
5959
}
6060

61-
if ($class->hasParent() && isset($this->config['types'][$class->parent()]['namespaces']['class'])) {
62-
$parentNamespace = $this->config['types'][$class->parent()]['namespaces']['class'];
61+
if ($class->hasParent()) {
62+
$parentName = $class->parent();
63+
\assert(\is_string($parentName));
64+
if (isset($this->config['types'][$parentName]['namespaces']['class'])) {
65+
$parentNamespace = $this->config['types'][$parentName]['namespaces']['class'];
6366

64-
if (!$class->isInNamespace($parentNamespace)) {
65-
$class->addUse(new Use_($parentNamespace.'\\'.$class->parent()));
67+
if (!$class->isInNamespace($parentNamespace)) {
68+
$class->addUse(new Use_($parentNamespace.'\\'.$parentName));
69+
}
6670
}
6771
}
6872
}

src/Command/GenerateCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7676
$defaultOutput = $this->defaultOutput ? realpath($this->defaultOutput) : null;
7777
$outputDir = $input->getArgument('output');
7878
$configArgument = $input->getArgument('config');
79+
\assert(\is_string($configArgument) || null === $configArgument);
7980

8081
if ($dir = realpath($outputDir)) {
8182
if (!is_dir($dir)) {
@@ -84,7 +85,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8485
}
8586

8687
$dir = $defaultOutput;
87-
$configArgument = $outputDir;
8888
}
8989

9090
if (!is_writable($dir)) {
@@ -130,7 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
130130
$configuration = $this->processConfiguration($configContent, $outputDir, $dir === $defaultOutput ? $this->namespacePrefix : null);
131131

132132
(new SchemaGenerator())->generate($configuration, $output, $io);
133-
(new OpenApiGenerator())->generate($configuration, $configArgument ?? self::DEFAULT_CONFIG_FILE, $output, $io);
133+
(new OpenApiGenerator())->generate($configuration, (string) ($configArgument ?? self::DEFAULT_CONFIG_FILE), $output, $io);
134134

135135
return Command::SUCCESS;
136136
}

src/OpenApi/ClassGenerator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,15 @@ public function generate(OpenApi $openApi, array $config): array
151151

152152
// Try to guess the references from the property names.
153153
foreach ($class->properties() as $property) {
154-
if ($reference = $classes[preg_replace('/Ids?$/', '', $this->inflector->singularize(u($property->name())->title()->toString())[0])] ?? null) {
154+
$singularized = $this->inflector->singularize(u($property->name())->title()->toString());
155+
if (empty($singularized)) {
156+
continue;
157+
}
158+
/** @var string $singularKey */
159+
$singularKey = $singularized[0];
160+
/** @var string $className */
161+
$className = preg_replace('/Ids?$/', '', $singularKey);
162+
if ($reference = $classes[$className] ?? null) {
155163
$property->reference = $reference;
156164
$property->cardinality = $property->isNullable ? CardinalitiesExtractor::CARDINALITY_0_1 : CardinalitiesExtractor::CARDINALITY_1_1;
157165
if ($property->isArray()) {

src/PhpTypeConverter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ private function getNonArrayType(SchemaProperty $property, array $classes): ?str
6565
}
6666

6767
$typeName = $property->rangeName;
68+
if (null === $typeName) {
69+
return null;
70+
}
6871
if ($type = (isset($classes[$typeName]) ? $classes[$typeName]->interfaceName() ?? $classes[$typeName]->name() : null)) {
6972
return $type;
7073
}

src/Schema/PropertyGenerator/PropertyGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private function getRanges(RdfResource $range, ?array $propertyConfig, array $co
201201

202202
if (
203203
(!isset($propertyConfig['range']) || $propertyConfig['range'] === $localName)
204-
&& (empty($config['types']) || isset($config['types'][$localName]) || $dataType)
204+
&& (empty($config['types']) || (null !== $localName && isset($config['types'][$localName])) || $dataType)
205205
) {
206206
return [$range];
207207
}

src/SchemaGeneratorConfiguration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public function getConfigTreeBuilder(): TreeBuilder
5252
array_keys($nodeConfig)
5353
);
5454

55-
// @phpstan-ignore-next-line node is not null
5655
$attributesNode = static fn () => (new NodeBuilder())
5756
->arrayNode('attributes')
5857
->info('Attributes (merged with generated attributes)')

src/TypesGenerator.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,13 @@ public function generate(array $graphs, array $config): array
144144
// Second pass
145145
foreach ($classes as $class) {
146146
if ($class->hasParent() && !$class->isParentEnum()) {
147-
$parentClass = $classes[$class->parent()] ?? null;
148-
if ($parentClass) {
149-
$parentClass->hasChild = true;
150-
$class->parentHasConstructor = $parentClass->hasConstructor;
147+
$parentName = $class->parent();
148+
if (null !== $parentName) {
149+
$parentClass = $classes[$parentName] ?? null;
150+
if ($parentClass) {
151+
$parentClass->hasChild = true;
152+
$class->parentHasConstructor = $parentClass->hasConstructor;
153+
}
151154
}
152155
}
153156

@@ -156,7 +159,7 @@ public function generate(array $graphs, array $config): array
156159
throw new \LogicException(\sprintf('Property "%s" has to be an instance of "%s".', $property->name(), SchemaProperty::class));
157160
}
158161
$typeName = $property->rangeName;
159-
if (isset($classes[$typeName])) {
162+
if (null !== $typeName && isset($classes[$typeName])) {
160163
$property->reference = $classes[$typeName];
161164
$referencedByClasses[$typeName][$class->name()] = $class;
162165
}
@@ -174,7 +177,8 @@ public function generate(array $graphs, array $config): array
174177
?? ($class->hasChild && !$class->isReferencedBy);
175178

176179
// When including all properties, ignore properties already set on parent
177-
if (($config['types'][$class->name()]['allProperties'] ?? true) && isset($classes[$class->parent()])) {
180+
$currentClassParent = $class->parent();
181+
if ($config['types'][$class->name()]['allProperties'] && null !== $currentClassParent && isset($classes[$currentClassParent])) {
178182
$type = $class->resource();
179183
foreach ($propertiesMap[$type->getUri()] as $property) {
180184
if (!\is_string($propertyName = $property->localName())) {
@@ -184,17 +188,18 @@ public function generate(array $graphs, array $config): array
184188
continue;
185189
}
186190

187-
$parentConfig = $config['types'][$class->parent()] ?? null;
188-
$parentClass = $classes[$class->parent()];
191+
$parentConfig = $config['types'][$currentClassParent] ?? null;
192+
$parentClass = $classes[$currentClassParent];
189193

190194
while ($parentClass) {
191195
if (\array_key_exists($propertyName, $parentConfig['properties'] ?? []) || \in_array($property, $propertiesMap[$parentClass->rdfType()], true)) {
192196
$class->removePropertyByName($propertyName);
193197
continue 2;
194198
}
195199

196-
$parentConfig = $parentClass->parent() ? ($config['types'][$parentClass->parent()] ?? null) : null;
197-
$parentClass = $parentClass->parent() && isset($classes[$parentClass->parent()]) ? $classes[$parentClass->parent()] : null;
200+
$parentClassParent = $parentClass->parent();
201+
$parentConfig = $parentClassParent ? ($config['types'][$parentClassParent] ?? null) : null;
202+
$parentClass = ($parentClassParent && isset($classes[$parentClassParent])) ? $classes[$parentClassParent] : null;
198203
}
199204
}
200205
}
@@ -414,7 +419,8 @@ private function addPropertyToMap(RdfResource $property, RdfResource $domain, ar
414419
continue;
415420
}
416421

417-
$propertyConfig = $config['types'][$typesResourceHierarchy['names'][$k]]['properties'][$propertyName] ?? null;
422+
$currentTypeName = $typesResourceHierarchy['names'][$k];
423+
$propertyConfig = $config['types'][$currentTypeName]['properties'][$propertyName] ?? null;
418424

419425
if ($propertyConfig['exclude'] ?? false) {
420426
continue;
@@ -452,13 +458,19 @@ private function defineTypesToGenerate(array $graphs, array $config): array
452458
continue;
453459
}
454460

455-
$typeName = $this->phpTypeConverter->escapeIdentifier($type->localName());
456-
if (!($config['types'][$typeName]['exclude'] ?? false)) {
461+
$localName = $type->localName();
462+
if (null === $localName) {
463+
continue;
464+
}
465+
/** @var string $typeNameFinal */
466+
$typeNameFinal = $this->phpTypeConverter->escapeIdentifier($localName);
467+
468+
if (!($config['types'][$typeNameFinal]['exclude'] ?? false)) {
457469
if ($config['resolveTypes'] || $vocabAllTypes) {
458-
$allTypes[$typeName] = $type;
470+
$allTypes[$typeNameFinal] = $type;
459471
}
460472
if ($vocabAllTypes) {
461-
$typeNamesToGenerate[] = $typeName;
473+
$typeNamesToGenerate[] = $typeNameFinal;
462474
}
463475
}
464476
}

0 commit comments

Comments
 (0)