Skip to content

Commit

Permalink
Make usable with PHP<8.0
Browse files Browse the repository at this point in the history
As the library is set to be usable with PHP7.4 and the
ReflectionProperty::has/getDefaultValue is only available for PHP8+ I
needed to slightly refactor the class.
  • Loading branch information
heiglandreas committed Nov 7, 2024
1 parent 43eca97 commit a04bce4
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/ModelDescriber/Annotations/ReflectionReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,14 @@ public function updateProperty(
OA\Property $property,
?array $validationGroups = null
): void {
if (PHP_VERSION_ID < 80000) {
return;
}
// The default has been set by an Annotation or Attribute
// We leave that as it is!
if (Generator::UNDEFINED !== $property->default) {
return;
}

$serializedName = $reflection->getName();
foreach (['', 'get', 'is', 'has', 'can', 'add', 'remove', 'set'] as $prefix) {
foreach (['get', 'is', 'has', 'can', 'add', 'remove', 'set'] as $prefix) {
if (0 === strpos($serializedName, $prefix)) {
$serializedName = substr($serializedName, strlen($prefix));
}
Expand Down Expand Up @@ -136,15 +133,12 @@ public function getDefaultFromPropertyReflection(\ReflectionProperty $reflection
return Generator::UNDEFINED;
}

$reflectionProp = $reflection->getDeclaringClass()->getProperty($propertyName);
if (!$reflectionProp->hasDefaultValue()) {
return Generator::UNDEFINED;
}
$defaultValue = $reflection->getDeclaringClass()->getDefaultProperties()[$propertyName] ?? null;

if (null === $reflectionProp->getDefaultValue()) {
if (null === $defaultValue) {
return Generator::UNDEFINED;
}

return $reflectionProp->getDefaultValue();
return $defaultValue;
}
}

0 comments on commit a04bce4

Please sign in to comment.