Skip to content

Commit ba30ea9

Browse files
committed
Fixup PHP Promotion reflection issues
1 parent 3a46eb6 commit ba30ea9

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/Exercise/PhpPromotion.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,21 @@ public function check(Input $input): ResultInterface
9898
];
9999

100100
$properties = collect($reflectionClass->getProperties());
101-
$properties = $properties->flatMap(fn (\ReflectionProperty $prop) => [
101+
$visibilities = $properties->flatMap(fn (\ReflectionProperty $prop) => [
102102
$prop->getName() => $this->getPropertyVisibility($prop)
103-
])->getArrayCopy();
104-
ksort($properties);
103+
])->ksort();
105104

106-
if ($changedVisibility = array_keys(array_diff_assoc($expectedProperties, $properties))) {
105+
if ($changedVisibility = array_keys(array_diff_assoc($expectedProperties, $visibilities->getArrayCopy()))) {
107106
return Failure::fromNameAndReason($this->getName(), pluralise(
108107
'Visibility changed for property "%s"',
109108
$changedVisibility,
110109
implode('" & "', $changedVisibility)
111110
));
112111
}
113112

114-
$types = $properties->map(fn (\ReflectionProperty $prop) => $prop->getType()?->getName());
113+
$types = $properties->flatMap(fn (\ReflectionProperty $prop) => [
114+
$prop->getName() => $this->getPropertyType($prop)
115+
]);
115116
$expected = ['visitor' => 'Closure', 'key' => 'string', 'basePath' => 'string'];
116117
if ([] !== $typeDiff = array_diff_assoc($expected, $types->getArrayCopy())) {
117118
return Failure::fromNameAndReason($this->getName(), pluralise(
@@ -132,7 +133,9 @@ public function check(Input $input): ResultInterface
132133

133134
$actual = $properties
134135
->each(fn (\ReflectionProperty $prop) => $prop->setAccessible(true))
135-
->map(fn (\ReflectionProperty $prop) => $prop->isInitialized($obj) ? $prop->getValue($obj) : null)
136+
->flatMap(fn (\ReflectionProperty $prop) => [
137+
$prop->getName() => $prop->isInitialized($obj) ? $prop->getValue($obj) : null
138+
])
136139
->getArrayCopy();
137140

138141
$dataFailures = array_filter([
@@ -160,4 +163,15 @@ private function getPropertyVisibility(\ReflectionProperty $prop): string
160163
default => 'public',
161164
};
162165
}
166+
167+
private function getPropertyType(\ReflectionProperty $prop): string
168+
{
169+
$type = $prop->getType();
170+
171+
if (null === $type || !$type instanceof \ReflectionNamedType) {
172+
throw new \RuntimeException('Invalid property "%s"', $prop->getName());
173+
}
174+
/** @var \ReflectionNamedType $type */
175+
return $type->getName();
176+
}
163177
}

0 commit comments

Comments
 (0)