Skip to content

Commit fe31ada

Browse files
authored
refactor(reflection): improve type handling and add safety checks (#1777)
1 parent c89c345 commit fe31ada

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

packages/reflection/src/PropertyReflector.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ public function getIterableType(): ?TypeReflector
9898
return null;
9999
}
100100

101-
preg_match('/@var ([\\\\\w]+)\[]/', $doc, $match);
101+
if (preg_match('/@var\s+([\\\\\w]+)\[\]/', $doc, $match)) {
102+
return new TypeReflector(ltrim($match[1], '\\'));
103+
}
102104

103-
if (! isset($match[1])) {
104-
return null;
105+
if (preg_match('/@var\s+(?:list|array)<([\\\\\w]+)>/', $doc, $match)) {
106+
return new TypeReflector(ltrim($match[1], '\\'));
105107
}
106108

107-
return new TypeReflector(ltrim($match[1], '\\'));
109+
return null;
108110
}
109111

110112
public function isUninitialized(object $object): bool
@@ -151,7 +153,7 @@ public function hasDefaultValue(): bool
151153

152154
$hasDefaultValue = $this->reflectionProperty->hasDefaultValue();
153155

154-
$hasPromotedDefaultValue = $this->isPromoted() && $constructorParameters[$this->getName()]->isDefaultValueAvailable();
156+
$hasPromotedDefaultValue = $this->isPromoted() && isset($constructorParameters[$this->getName()]) && $constructorParameters[$this->getName()]->isDefaultValueAvailable();
155157

156158
return $hasDefaultValue || $hasPromotedDefaultValue;
157159
}

packages/reflection/src/TypeReflector.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use BackedEnum;
88
use DateTimeInterface;
9-
use Exception;
109
use Generator;
1110
use Iterator;
1211
use ReflectionClass as PHPReflectionClass;
@@ -232,7 +231,13 @@ private function resolveDefinition(PHPReflector|PHPReflectionType|string $reflec
232231
}
233232

234233
if ($reflector instanceof PHPReflectionParameter || $reflector instanceof PHPReflectionProperty) {
235-
return $this->resolveDefinition($reflector->getType());
234+
$type = $reflector->getType();
235+
236+
if ($type === null) {
237+
return 'mixed';
238+
}
239+
240+
return $this->resolveDefinition($type);
236241
}
237242

238243
if ($reflector instanceof PHPReflectionClass) {
@@ -257,7 +262,9 @@ private function resolveDefinition(PHPReflector|PHPReflectionType|string $reflec
257262
));
258263
}
259264

260-
throw new Exception('Could not resolve type');
265+
throw new \InvalidArgumentException(
266+
sprintf('Could not resolve type for reflector of type: %s', get_debug_type($reflector)),
267+
);
261268
}
262269

263270
private function resolveIsNullable(PHPReflectionType|PHPReflector|string $reflector): bool
@@ -267,7 +274,9 @@ private function resolveIsNullable(PHPReflectionType|PHPReflector|string $reflec
267274
}
268275

269276
if ($reflector instanceof PHPReflectionParameter || $reflector instanceof PHPReflectionProperty) {
270-
return $reflector->getType()->allowsNull();
277+
$type = $reflector->getType();
278+
279+
return $type === null || $type->allowsNull();
271280
}
272281

273282
if ($reflector instanceof PHPReflectionType) {

0 commit comments

Comments
 (0)