Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions src/ReflectionDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ public function loadSubAttributes(?object $attribute, \Reflector $reflection): v
if ($this->isMultivalueAttribute($type)) {
$subs = $this->parser->getInheritedAttributes($reflection, $type);
foreach ($subs as $sub) {
if ($sub instanceof Finalizable) {
$sub->finalize();
}
$this->loadSubAttributes($sub, $reflection);
$this->applySubattributeFeatures($sub, $reflection);
}
if ($callback instanceof \Closure) {
$callback($subs);
Expand All @@ -142,10 +139,9 @@ public function loadSubAttributes(?object $attribute, \Reflector $reflection): v
}
} else {
$sub = $this->parser->getInheritedAttribute($reflection, $type);
if ($sub instanceof Finalizable) {
$sub->finalize();
if ($sub) {
$this->applySubattributeFeatures($sub, $reflection);
}
$this->loadSubAttributes($sub, $reflection);
if ($callback instanceof \Closure) {
$callback($sub);
} else {
Expand All @@ -156,6 +152,39 @@ public function loadSubAttributes(?object $attribute, \Reflector $reflection): v
}
}

protected function applySubattributeFeatures(object $attribute, \Reflector $reflection): void
{
// For each possible type, check for a FromReflection interface.
if ($reflection instanceof \ReflectionClass && ! $reflection instanceof \ReflectionEnum && $attribute instanceof FromReflectionClass) {
/** @var \ReflectionClass<object> $reflection */
$attribute->fromReflection($reflection);
}
if ($reflection instanceof \ReflectionEnum && $attribute instanceof FromReflectionEnum) {
$attribute->fromReflection($reflection);
}
if ($reflection instanceof \ReflectionFunction && $attribute instanceof FromReflectionFunction) {
$attribute->fromReflection($reflection);
}
if ($reflection instanceof \ReflectionProperty && $attribute instanceof FromReflectionProperty) {
$attribute->fromReflection($reflection);
}
if ($reflection instanceof \ReflectionMethod && $attribute instanceof FromReflectionMethod) {
$attribute->fromReflection($reflection);
}
if ($reflection instanceof \ReflectionClassConstant && $attribute instanceof FromReflectionClassConstant) {
$attribute->fromReflection($reflection);
}
if ($reflection instanceof \ReflectionParameter && $attribute instanceof FromReflectionParameter) {
$attribute->fromReflection($reflection);
}

if ($attribute instanceof Finalizable) {
$attribute->finalize();
}
// Call recursively to allow sub-attributes on sub-attributes. (Yo Dawg.)
$this->loadSubAttributes($attribute, $reflection);
}

/**
* Determines if a given attribute class allows repeating.
*
Expand Down
1 change: 0 additions & 1 deletion tests/Attributes/ClassWithOwnSubAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Attribute;
use Crell\AttributeUtils\HasSubAttributes;
use Crell\AttributeUtils\ParseProperties;

#[Attribute(Attribute::TARGET_CLASS)]
class ClassWithOwnSubAttributes implements HasSubAttributes
Expand Down
2 changes: 1 addition & 1 deletion tests/Attributes/ClassWithPropertiesWithSubAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function includePropertiesByDefault(): bool

public function propertyAttribute(): string
{
return PropertyWithSubAttributes::class;
return ConfigurablePropertyWithSubAttributes::class;
}

public function subAttributes(): array
Expand Down
3 changes: 2 additions & 1 deletion tests/Attributes/ClassWithSubSubAttributeLevelOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace Crell\AttributeUtils\Attributes;

use \Attribute;
use Attribute;
use Crell\AttributeUtils\HasSubAttributes;

use function Crell\fp\amap;
use function Crell\fp\prop;

Expand Down
1 change: 0 additions & 1 deletion tests/Attributes/ClassWithSubSubAttributeLevelTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Crell\AttributeUtils\Attributes;

use Attribute;
use Crell\AttributeUtils\HasSubAttributes;

#[Attribute(Attribute::TARGET_CLASS)]
class ClassWithSubSubAttributeLevelTwo
Expand Down
1 change: 0 additions & 1 deletion tests/Attributes/ClassWithSubSubAttributeLevelTwoMulti.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Crell\AttributeUtils\Attributes;

use Attribute;
use Crell\AttributeUtils\HasSubAttributes;
use Crell\AttributeUtils\Multivalue;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
Expand Down
2 changes: 1 addition & 1 deletion tests/Attributes/ClassWithSubSubAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Crell\AttributeUtils\Attributes;

use \Attribute;
use Attribute;
use Crell\AttributeUtils\HasSubAttributes;

#[Attribute(Attribute::TARGET_CLASS)]
Expand Down
38 changes: 38 additions & 0 deletions tests/Attributes/ConfigurableClassWithProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Crell\AttributeUtils\Attributes;

use Attribute;
use Crell\AttributeUtils\ParseProperties;

#[Attribute(Attribute::TARGET_CLASS)]
class ConfigurableClassWithProperties implements ParseProperties
{
/**
* @var array<string, ?object>
*/
public array $properties = [];

public function __construct(
public string $propertyAttribute,
public bool $includeByDefault = false,
public string $a = 'A',
) {}

public function setProperties(array $properties): void
{
$this->properties = $properties;
}

public function includePropertiesByDefault(): bool
{
return $this->includeByDefault;
}

public function propertyAttribute(): string
{
return $this->propertyAttribute;
}
}
30 changes: 30 additions & 0 deletions tests/Attributes/ConfigurablePropertyWithSubAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Crell\AttributeUtils\Attributes;

use Attribute;
use Crell\AttributeUtils\HasSubAttributes;

#[Attribute(Attribute::TARGET_PROPERTY)]
class ConfigurablePropertyWithSubAttributes implements HasSubAttributes
{
public ?object $subattrib;

public function __construct(
public string $subattribute = PropertySubAttribute::class,
public string $a = 'A',
) {}

public function subAttributes(): array
{
return [$this->subattribute => 'fromSubAttribute'];
}

public function fromSubAttribute(?object $sub): void
{
$sub ??= new ($this->subattribute)();
$this->subattrib = $sub;
}
}
18 changes: 18 additions & 0 deletions tests/Attributes/PropertySubAttributeWithReflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Crell\AttributeUtils\Attributes;

use Crell\AttributeUtils\FromReflectionProperty;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class PropertySubAttributeWithReflection implements FromReflectionProperty
{
public string $name;

public function fromReflection(\ReflectionProperty $subject): void
{
$this->name = $subject->getName();
}
}
30 changes: 0 additions & 30 deletions tests/Attributes/PropertyWithSubAttributes.php

This file was deleted.

1 change: 0 additions & 1 deletion tests/Attributes/ScopedClassMulti.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Crell\AttributeUtils\Attributes;

use Attribute;
use Crell\AttributeUtils\Attributes\Reflect\CollectMethods;
use Crell\AttributeUtils\Attributes\Reflect\CollectProperties;
use Crell\AttributeUtils\ParseProperties;
use Crell\AttributeUtils\SupportsScopes;
Expand Down
35 changes: 29 additions & 6 deletions tests/ClassAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@
use Crell\AttributeUtils\Attributes\ClosureSubAttributeMain;
use Crell\AttributeUtils\Attributes\FinalizableClassAttribute;
use Crell\AttributeUtils\Attributes\GenericClass;
use Crell\AttributeUtils\Attributes\InheritableClassAttributeMain;
use Crell\AttributeUtils\Attributes\Labeled;
use Crell\AttributeUtils\Attributes\PropertyTakesClassDefaultClass;
use Crell\AttributeUtils\Attributes\ScopedClass;
use Crell\AttributeUtils\Attributes\InheritableClassAttributeMain;
use Crell\AttributeUtils\Attributes\ScopedClassMulti;
use Crell\AttributeUtils\Attributes\ScopedClassNoDefaultInclude;
use Crell\AttributeUtils\ExclusiveOptions\Audio;
use Crell\AttributeUtils\ExclusiveOptions\AudioData;
use Crell\AttributeUtils\ExclusiveOptions\BothData;
use Crell\AttributeUtils\ExclusiveOptions\DisplayInfo;
use Crell\AttributeUtils\ExclusiveOptions\DisplayType;
use Crell\AttributeUtils\ExclusiveOptions\NoData;
use Crell\AttributeUtils\ExclusiveOptions\Screen;
use Crell\AttributeUtils\ExclusiveOptions\ScreenData;
use Crell\AttributeUtils\InterfaceAttributes\Hero;
use Crell\AttributeUtils\InterfaceAttributes\Name;
use Crell\AttributeUtils\InterfaceAttributes\Names;
use Crell\AttributeUtils\Records\AttributesInheritChild;
use Crell\AttributeUtils\Records\ClassWithClosureSubAttributes;
Expand All @@ -43,11 +41,11 @@
use Crell\AttributeUtils\Records\ClassWithExcludedProperties;
use Crell\AttributeUtils\Records\ClassWithExtraAnalysisSource;
use Crell\AttributeUtils\Records\ClassWithFinalizableAttributes;
use Crell\AttributeUtils\Records\ClassWithScopes;
use Crell\AttributeUtils\Records\ClassWithInterface;
use Crell\AttributeUtils\Records\ClassWithMethodsAndProperties;
use Crell\AttributeUtils\Records\ClassWithPropertiesWithReflection;
use Crell\AttributeUtils\Records\ClassWithRecursiveSubAttributes;
use Crell\AttributeUtils\Records\ClassWithScopes;
use Crell\AttributeUtils\Records\ClassWithScopesMulti;
use Crell\AttributeUtils\Records\ClassWithScopesNotDefault;
use Crell\AttributeUtils\Records\ClassWithSubAttributes;
Expand All @@ -60,6 +58,9 @@
use Crell\AttributeUtils\Records\PropertiesWithMultipleSubattributes;
use Crell\AttributeUtils\Records\PropertyThatTakesClassDefault;
use Crell\AttributeUtils\Records\TransitiveFieldClass;
use Crell\AttributeUtils\SubattributeReflection\ClassAllFeaturesForSubAttrib;
use Crell\AttributeUtils\SubattributeReflection\ClassWithAllFeaturesForSubAttribReflection;
use Crell\AttributeUtils\SubattributeReflection\EnumForSubAttrib;
use Crell\AttributeUtils\TypeDef\Suit;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -201,9 +202,31 @@ public static function attributeTestProvider(): \Generator
'test' => static function(ClassWithPropertiesWithSubAttributes $classDef) {
static::assertEquals('C', $classDef->c);
static::assertEquals('A', $classDef->properties['hasSub']->a);
static::assertEquals('B', $classDef->properties['hasSub']->b);
static::assertEquals('B', $classDef->properties['hasSub']->subattrib->b);
static::assertEquals('A', $classDef->properties['noSub']->a);
static::assertEquals('B', $classDef->properties['noSub']->b);
static::assertEquals('B', $classDef->properties['noSub']->subattrib->b);
},
];

yield 'Subattributes with FromReflection (class)' => [
'subject' => ClassWithAllFeaturesForSubAttribReflection::class,
'attribute' => ClassAllFeaturesForSubAttrib::class,
'test' => static function(ClassAllFeaturesForSubAttrib $classDef) {
static::assertEquals('A', $classDef->a);
static::assertEquals(ClassWithAllFeaturesForSubAttribReflection::class, $classDef->sub->name);
static::assertEquals('classA', $classDef->properties['classA']->sub->name);
static::assertEquals('method', $classDef->methods['method']->sub->name);
static::assertEquals('parameter', $classDef->methods['method']->parameters['parameter']->sub->name);
static::assertEquals('AConstant', $classDef->constants['AConstant']->sub->name);
},
];

yield 'Subattributes with FromReflection (enum)' => [
'subject' => EnumForSubAttrib::class,
'attribute' => ClassAllFeaturesForSubAttrib::class,
'test' => static function(ClassAllFeaturesForSubAttrib $classDef) {
static::assertEquals(EnumForSubAttrib::class, $classDef->sub->name);
static::assertEquals('Case', $classDef->cases['Case']->sub->name);
},
];

Expand Down
5 changes: 0 additions & 5 deletions tests/FunctionAnalyzerCacheTestMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@

namespace Crell\AttributeUtils;

use Crell\AttributeUtils\Attributes\ClassWithProperties;
use Crell\AttributeUtils\Attributes\Functions\IncludesReflection;
use Crell\AttributeUtils\Attributes\Functions\SubParent;
use Crell\AttributeUtils\Attributes\ScopedClass;
use Crell\AttributeUtils\Records\ClassWithDefaultFields;
use Crell\AttributeUtils\Records\ClassWithScopes;
use Crell\AttributeUtils\Records\Point;
use PHPUnit\Framework\Attributes\Test;

#[IncludesReflection]
Expand Down
18 changes: 18 additions & 0 deletions tests/FunctionAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Crell\AttributeUtils\Attributes\Functions\RequiredArg;
use Crell\AttributeUtils\Attributes\Functions\SubChild;
use Crell\AttributeUtils\Attributes\Functions\SubParent;
use Crell\AttributeUtils\SubattributeReflection\ComponentAttribute;
use Crell\AttributeUtils\SubattributeReflection\FuncAllFeaturesForSubAttrib;
use Crell\AttributeUtils\SubattributeReflection\SubAttributeReflect;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
Expand All @@ -31,6 +34,12 @@ function has_parameters(
#[ParameterAttrib('Override')] int $second,
) {}


#[FuncAllFeaturesForSubAttrib, SubAttributeReflect]
function func_for_subattrib(
#[ComponentAttribute, SubAttributeReflect] string $parameter,
) {}

class FunctionAnalyzerTest extends TestCase
{
protected string $ns;
Expand Down Expand Up @@ -101,5 +110,14 @@ public static function attributeTestProvider(): \Generator
static::assertEquals('Override', $funcDef->parameters['second']->a);
},
];

yield 'Subattributes with FromReflection (function)' => [
'subject' => "{$ns}func_for_subattrib",
'attribute' => FuncAllFeaturesForSubAttrib::class,
'test' => static function(FuncAllFeaturesForSubAttrib $funcDef) use ($ns) {
static::assertEquals("{$ns}func_for_subattrib", $funcDef->sub->name);
static::assertEquals('parameter', $funcDef->parameters['parameter']->sub->name);
},
];
}
}
1 change: 0 additions & 1 deletion tests/InterfaceAttributes/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Crell\AttributeUtils\InterfaceAttributes;

use Attribute;
use Crell\AttributeUtils\Multivalue;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Alias implements Name
Expand Down
1 change: 1 addition & 0 deletions tests/InterfaceAttributes/Names.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Attribute;
use Crell\AttributeUtils\HasSubAttributes;

use function Crell\fp\method;

#[Attribute(Attribute::TARGET_CLASS)]
Expand Down
Loading