Skip to content

Commit

Permalink
Add the InheritsAttributes attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Oct 1, 2023
1 parent e740af7 commit 4975f41
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 15 deletions.
24 changes: 24 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# Migration

## v2.0 to v2.1

### New Requirements

None

### New features

- The `InheritsAttributes` attribute can be used on classes that inherit their attributes from traits, properties, or methods, and were previously ignored by the collection process.

### Backward Incompatible Changes

None

### Deprecated Features

None

### Other Changes

- Just like interfaces, traits are excluded from the collection.



## v1.2 to v2.0

### New Requirements
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ You could also use [spatie/file-system-watcher][], it only requires PHP. If the
for your liking, try running the command with `COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE=yes`, it will
enable caching and speed up consecutive runs.

**How do I include a class that inherits its attributes?**

To speed up the collection process, the plugin first looks at PHP files as plain text for hints of attribute usage. If a
class inherits its attributes from traits, properties, or methods, it is ignored. Use the attribute
`[#\olvlvl\ComposerAttributeCollector\InheritsAttributes]` to force the collection.


----------
Expand Down
2 changes: 1 addition & 1 deletion src/ClassAttributeCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use ReflectionAttribute;
use ReflectionClass;
use ReflectionException;
use Throwable;

/**
* @internal
Expand Down Expand Up @@ -124,6 +123,7 @@ private static function isAttributeIgnored(ReflectionAttribute $attribute): bool
{
static $ignored = [
\ReturnTypeWillChange::class => true,
InheritsAttributes::class => true,
];

return isset($ignored[$attribute->getName()]);
Expand Down
7 changes: 5 additions & 2 deletions src/Filter/InterfaceFilter.php → src/Filter/ClassFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@

use function interface_exists;

final class InterfaceFilter implements Filter
/**
* Filters classes—removes interfaces and traits.
*/
final class ClassFilter implements Filter
{
public function filter(string $filepath, string $class, IOInterface $io): bool
{
try {
if (interface_exists($class)) {
if (!class_exists($class)) {
return false;
}
} catch (Throwable $e) {
Expand Down
13 changes: 13 additions & 0 deletions src/InheritsAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace olvlvl\ComposerAttributeCollector;

use Attribute;

/**
* Use this attribute when a class inherits attributes from traits or parents and is ignored by the collector.
*/
#[Attribute(Attribute::TARGET_CLASS)]
final class InheritsAttributes
{
}
4 changes: 2 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use olvlvl\ComposerAttributeCollector\Datastore\FileDatastore;
use olvlvl\ComposerAttributeCollector\Datastore\RuntimeDatastore;
use olvlvl\ComposerAttributeCollector\Filter\ContentFilter;
use olvlvl\ComposerAttributeCollector\Filter\InterfaceFilter;
use olvlvl\ComposerAttributeCollector\Filter\ClassFilter;

use function file_exists;
use function file_put_contents;
Expand Down Expand Up @@ -162,7 +162,7 @@ private static function buildFileFilter(): Filter
{
return new Filter\Chain([
new ContentFilter(),
new InterfaceFilter()
new ClassFilter()
]);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Acme/Attribute/Routing/UrlGetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Acme\Attribute\Routing;

use Attribute;

#[Attribute(Attribute::TARGET_METHOD)]
final class UrlGetter
{
}
12 changes: 12 additions & 0 deletions tests/Acme/PSR4/InheritedAttributeSample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Acme\PSR4;

use Acme\PSR4\Routing\UrlTrait;
use olvlvl\ComposerAttributeCollector\InheritsAttributes;

#[InheritsAttributes]
class InheritedAttributeSample
{
use UrlTrait;
}
14 changes: 14 additions & 0 deletions tests/Acme/PSR4/Routing/UrlTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Acme\PSR4\Routing;

use Acme\Attribute\Routing\UrlGetter;

trait UrlTrait
{
#[UrlGetter]
public function get_url(): string
{
return '/url';
}
}
17 changes: 7 additions & 10 deletions tests/PluginTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/*
* (c) Olivier Laviale <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace tests\olvlvl\ComposerAttributeCollector;

use Acme\Attribute\ActiveRecord\Boolean;
Expand All @@ -21,6 +14,7 @@
use Acme\Attribute\Permission;
use Acme\Attribute\Resource;
use Acme\Attribute\Route;
use Acme\Attribute\Routing\UrlGetter;
use Acme\Attribute\Subscribe;
use Acme\PSR4\Presentation\ArticleController;
use Composer\IO\NullIO;
Expand All @@ -44,9 +38,6 @@ final class PluginTest extends TestCase
{
private static bool $initialized = false;

/**
* @throws ReflectionException
*/
protected function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -184,6 +175,12 @@ public static function provideTargetMethods(): array
[ new Subscribe(), 'Acme\PSR4\SubscriberB::onEventA' ],
]
],
[
UrlGetter::class,
[
[ new UrlGetter(), 'Acme\PSR4\InheritedAttributeSample::get_url' ]
]
],

];
}
Expand Down

0 comments on commit 4975f41

Please sign in to comment.