- Small fix on reflection property name

This commit is contained in:
Dave Mc Nicoll 2025-05-19 17:31:33 +00:00
parent e20157a162
commit fa4fe32277
3 changed files with 84 additions and 2 deletions

View File

@ -33,8 +33,9 @@ class AttributeReader
{
switch(true) {
case $reflect instanceof ReflectionMethod :
case $reflect instanceof ReflectionProperty :
return $reflect->class . "::" . $reflect->name;
case $reflect instanceof ReflectionProperty :
return $reflect->class . "::$" . $reflect->name;
case $reflect instanceof ReflectionClass :
return $reflect->name;

View File

@ -21,7 +21,6 @@ abstract class Reflected
$type = strtolower($type);
foreach($this->getTypes() as $item) {
if ($type === "null") {
if ($item->type === "null" || $item->nullable) {
return true;

82
src/Common/Reflected.php~ Normal file
View File

@ -0,0 +1,82 @@
<?php
namespace Notes\Common;
use Notes\Attribute\Ignore;
abstract class Reflected
{
public function hasIgnoreAttribute() : bool
{
return [] !== array_filter($this->attributes, fn($e) => $e->object instanceof Ignore);
}
public function allowsNull() : bool
{
# dump($this);
return empty($this->type) || $this->expectType("null");
}
public function expectType(string $type) : bool
{
$type = strtolower($type);
foreach($this->getTypes() as $item) {
if ($type === "null") {
if ($item->type === "null" || $item->nullable) {
return true;
}
}
elseif ($type === $item->type) {
return true;
}
}
return false;
}
public function getTypes() : array
{
return is_array($this->type) ? $this->type : [ $this->type ];
}
public function typeFromReflection(\ReflectionProperty|\ReflectionParameter $property) : void
{
if ( $property->hasType() ) {
$type = $property->getType();
if ($type instanceof \ReflectionUnionType ) {
foreach($type->getTypes() as $item) {
$this->type[] = new ReflectedPropertyType($item->getName(), $item->isBuiltIn(), $item->allowsNull());
}
}
else {
$this->type = new ReflectedPropertyType($type->getName(), $type->isBuiltIn(), $type->allowsNull());
}
}
}
public function getAttributes(null|string|array $attributeType = null): array
{
if ($attributeType) {
$list = [];
foreach($this->attributes as $attribute) {
foreach((array) $attributeType as $type) {
if ($attribute->object instanceof $type) {
$list[] = $attribute;
}
}
}
return array_reverse($list);
}
return array_reverse($this->attributes);
}
public function getAttribute(string|array $attributeType): ?object
{
return $this->getAttributes($attributeType)[0] ?? null;
}
}