@ 2021-07-22T16:09:22Z <?php
declare(strict_types=1);
namespace Crell\ObjectAnalyzer;
class ObjectAnalyzer
{
public function analyze(string|object $class, string $attribute): object
{
$subject = match (is_string($class)) {
true => new \ReflectionClass($class),
false => new \ReflectionObject($class),
};
$classDef = $this->getAttribute($subject, $attribute) ?? new $attribute;
if ($classDef instanceof ReflectionPopulatable) {
$classDef->fromReflection($subject);
}
if ($classDef instanceof Fieldable) {
$fields = $this->getPropertyDefinitions($subject, $classDef::propertyAttribute());
$classDef->setFields($fields);
}
return $classDef;
}
/**
* @return Field[]
*/
protected function getPropertyDefinitions(\ReflectionObject|\ReflectionClass $subject, string $propertyAttribute): array
{
// The list of filters here is going to grow, so wants pipes.
$rProperties = $subject->getProperties();
// Wants PFA
$properties = array_map(fn(\ReflectionProperty $p) => $this->getPropertyDefinition($p, $propertyAttribute), $rProperties);
//$fields = array_filter($fields, fn(Field $f): bool => !$f->skip);
return $properties;
}
// Wants a generic return.
protected function getPropertyDefinition(\ReflectionProperty $property, string $propertyAttribute): object
{
$propDef = $this->getAttribute($property, $propertyAttribute) ?? new $propertyAttribute();
if ($propDef instanceof ReflectionPopulatable) {
$propDef->fromReflection($property);
}
return $propDef;
}
protected function getAttribute(\Reflector $target, string $name): ?object
{
return $this->getAttributes($target, $name)[0] ?? null;
}
protected function getAttributes(\Reflector $target, string $name): array
{
return array_map(fn(\ReflectionAttribute $attrib)
=> $attrib->newInstance(), $target->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF));
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.7 0.004 0.015 18.18 8.3.6 0.015 0.006 18.18 8.3.5 0.014 0.003 17.86 8.3.4 0.013 0.006 18.54 8.3.3 0.009 0.006 18.59 8.3.2 0.008 0.000 24.18 8.3.1 0.005 0.003 24.66 8.3.0 0.006 0.003 26.16 8.2.19 0.007 0.007 16.63 8.2.18 0.009 0.006 25.92 8.2.17 0.007 0.007 18.58 8.2.16 0.014 0.004 22.96 8.2.15 0.005 0.003 25.66 8.2.14 0.003 0.005 24.66 8.2.13 0.007 0.000 26.16 8.2.12 0.005 0.003 26.16 8.2.11 0.008 0.000 20.54 8.2.10 0.006 0.003 18.85 8.1.28 0.004 0.011 25.92 8.1.27 0.009 0.000 23.99 8.1.26 0.004 0.004 26.35 8.1.25 0.004 0.004 28.09 8.1.24 0.010 0.000 18.50 8.1.23 0.003 0.006 22.20 8.0.8 0.006 0.013 16.75
preferences:dark mode live preview
43.75 ms | 401 KiB | 5 Q