diff --git a/composer.json b/composer.json index 6e35baa..1d18ae0 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "minimum-stability": "stable", "require": { - "php": "^8.0" + "php": "^8.1", + "ext-reflection": "*" }, "autoload": { "psr-4": { diff --git a/src/DebugInfo/DebugClass.php b/src/DebugInfo/DebugClass.php new file mode 100644 index 0000000..4bb8e59 --- /dev/null +++ b/src/DebugInfo/DebugClass.php @@ -0,0 +1,37 @@ +minimalScope = $minimalScope; + $this->inherited = $inherited; + $this->namePrefix = $namePrefix; + } +} \ No newline at end of file diff --git a/src/DebugInfo/DebugClassMinimalScope.php b/src/DebugInfo/DebugClassMinimalScope.php new file mode 100644 index 0000000..7ad7a19 --- /dev/null +++ b/src/DebugInfo/DebugClassMinimalScope.php @@ -0,0 +1,51 @@ +getAttributes(DebugIgnoredProperty::class, ReflectionAttribute::IS_INSTANCEOF)) > 0) { + return false; + } + if (count($reflectionProperty->getAttributes(DebugProperty::class, ReflectionAttribute::IS_INSTANCEOF)) > 0) { + return true; + } + + return match ($this) { + self::Private => $reflectionProperty->isPrivate() || $reflectionProperty->isProtected() || $reflectionProperty->isPublic(), + self::Protected => $reflectionProperty->isProtected() || $reflectionProperty->isPublic(), + self::Public => $reflectionProperty->isPublic(), + default => false + }; + } +} \ No newline at end of file diff --git a/src/DebugInfo/DebugIgnoredProperty.php b/src/DebugInfo/DebugIgnoredProperty.php new file mode 100644 index 0000000..29c0bb5 --- /dev/null +++ b/src/DebugInfo/DebugIgnoredProperty.php @@ -0,0 +1,12 @@ +getAttributes(DebugClass::class, ReflectionAttribute::IS_INSTANCEOF)[0] ?? null; + $objectDebugClassAttribute = $objectDebugClassAttributeReflection === null ? new DebugClass() : $objectDebugClassAttributeReflection->newInstance(); + $namePrefix = $objectDebugClassAttribute->namePrefix ?? $objectReflection->getName(); + + foreach ($objectReflection->getProperties() as $propertyReflection) { + if ($propertyReflection->getDeclaringClass()->getName() !== $objectReflection->getName()) { + continue; + } + if (!$objectDebugClassAttribute->minimalScope->isValidReflectionProperty($propertyReflection)) { + continue; + } + + $propertyDebugAttributeReflection = $propertyReflection->getAttributes(DebugProperty::class, ReflectionAttribute::IS_INSTANCEOF)[0] ?? null; + $propertyDebugAttribute = $propertyDebugAttributeReflection === null ? new DebugProperty() : $propertyDebugAttributeReflection->newInstance(); + $propertyName = $propertyDebugAttribute->name ?? $propertyReflection->getName(); + + $debugInformation[$namePrefix . ':' + . $propertyName . ':' + . match (true) { + $propertyReflection->isPrivate() => 'private', + $propertyReflection->isProtected() => 'protected', + $propertyReflection->isPublic() => 'public', + } + . ($propertyReflection->isStatic() ? ',static' : '')] = $propertyReflection->isInitialized($object) + ? $propertyReflection->getValue($object) + : new UninitializedValue((string)$propertyReflection->getType()); + } + + if ($objectDebugClassAttribute->inherited && ($parentReflection = $objectReflection->getParentClass()) !== false) { + try { + $parentDebugInformation = $parentReflection->hasMethod('__debugInfo') + ? $parentReflection->getMethod('__debugInfo')->invoke($object) + : static::collectThroughReflection($object, $parentReflection); + } + catch (ReflectionException) { + $parentDebugInformation = static::collectThroughReflection($object, $parentReflection); + } + + $debugInformation += $parentDebugInformation; + } + + return $debugInformation; + } +} \ No newline at end of file diff --git a/src/DebugInfo/DebugProperty.php b/src/DebugInfo/DebugProperty.php new file mode 100644 index 0000000..c0df3e3 --- /dev/null +++ b/src/DebugInfo/DebugProperty.php @@ -0,0 +1,25 @@ +name = $name; + } +} \ No newline at end of file diff --git a/src/DebugInfo/TDebugInfoCollector.php b/src/DebugInfo/TDebugInfoCollector.php new file mode 100644 index 0000000..0b7ceee --- /dev/null +++ b/src/DebugInfo/TDebugInfoCollector.php @@ -0,0 +1,17 @@ +type = $type; + } +} \ No newline at end of file diff --git a/tests/test.php b/tests/test.php new file mode 100644 index 0000000..805e070 --- /dev/null +++ b/tests/test.php @@ -0,0 +1,34 @@ +