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 bc59009
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 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 @@ -106,7 +103,10 @@ public function setSchema(OA\Schema $schema): void
$this->schema = $schema;
}

private function getDefaultFromMethodReflection(\ReflectionMethod $reflection): mixed
/**
* @return mixed|string
*/
private function getDefaultFromMethodReflection(\ReflectionMethod $reflection)
{
if (0 !== strpos($reflection->name, 'set')) {
return Generator::UNDEFINED;
Expand All @@ -129,22 +129,22 @@ private function getDefaultFromMethodReflection(\ReflectionMethod $reflection):
return $param->getDefaultValue();
}

public function getDefaultFromPropertyReflection(\ReflectionProperty $reflection): mixed
/**
* @return mixed|string
*/
public function getDefaultFromPropertyReflection(\ReflectionProperty $reflection)
{
$propertyName = $reflection->name;
if (!$reflection->getDeclaringClass()->hasProperty($propertyName)) {
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 bc59009

Please sign in to comment.