Skip to content

Commit 00cf8cd

Browse files
committed
Add the InheritsAttributes attribute
1 parent c801e21 commit 00cf8cd

10 files changed

+93
-15
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
# CHANGELOG
22

3+
## v2.0 to v2.1
4+
5+
### New Requirements
6+
7+
None
8+
9+
### New features
10+
11+
- 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.
12+
13+
### Backward Incompatible Changes
14+
15+
None
16+
17+
### Deprecated Features
18+
19+
None
20+
21+
### Other Changes
22+
23+
- Just like interfaces, traits are excluded from the collection.
24+
25+
26+
327
## v1.2 to v2.0
428

529
### New Requirements

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ You could also use [spatie/file-system-watcher][], it only requires PHP. If the
271271
for your liking, try running the command with `COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE=yes`, it will
272272
enable caching and speed up consecutive runs.
273273

274+
**How do I include a class that inherits its attributes?**
275+
276+
To speed up the collection process, the plugin first looks at PHP files as plain text for hints of attribute usage. If a
277+
class inherits its attributes from traits, properties, or methods, it is ignored. Use the attribute
278+
`[#\olvlvl\ComposerAttributeCollector\InheritsAttributes]` to force the collection.
274279

275280

276281
----------

src/ClassAttributeCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use ReflectionAttribute;
88
use ReflectionClass;
99
use ReflectionException;
10-
use Throwable;
1110

1211
/**
1312
* @internal
@@ -124,6 +123,7 @@ private static function isAttributeIgnored(ReflectionAttribute $attribute): bool
124123
{
125124
static $ignored = [
126125
\ReturnTypeWillChange::class => true,
126+
InheritsAttributes::class => true,
127127
];
128128

129129
return isset($ignored[$attribute->getName()]);

src/Filter/InterfaceFilter.php renamed to src/Filter/ClassFilter.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515

1616
use function interface_exists;
1717

18-
final class InterfaceFilter implements Filter
18+
/**
19+
* Filters classes—removes interfaces and traits.
20+
*/
21+
final class ClassFilter implements Filter
1922
{
2023
public function filter(string $filepath, string $class, IOInterface $io): bool
2124
{
2225
try {
23-
if (interface_exists($class)) {
26+
if (!class_exists($class)) {
2427
return false;
2528
}
2629
} catch (Throwable $e) {

src/InheritsAttributes.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace olvlvl\ComposerAttributeCollector;
4+
5+
use Attribute;
6+
7+
/**
8+
* Use this attribute when a class inherits attributes from traits or parents and is ignored by the collector.
9+
*/
10+
#[Attribute(Attribute::TARGET_CLASS)]
11+
final class InheritsAttributes
12+
{
13+
}

src/Plugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use olvlvl\ComposerAttributeCollector\Datastore\FileDatastore;
1212
use olvlvl\ComposerAttributeCollector\Datastore\RuntimeDatastore;
1313
use olvlvl\ComposerAttributeCollector\Filter\ContentFilter;
14-
use olvlvl\ComposerAttributeCollector\Filter\InterfaceFilter;
14+
use olvlvl\ComposerAttributeCollector\Filter\ClassFilter;
1515

1616
use function file_exists;
1717
use function file_put_contents;
@@ -162,7 +162,7 @@ private static function buildFileFilter(): Filter
162162
{
163163
return new Filter\Chain([
164164
new ContentFilter(),
165-
new InterfaceFilter()
165+
new ClassFilter()
166166
]);
167167
}
168168

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Acme\Attribute\Routing;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_METHOD)]
8+
final class UrlGetter
9+
{
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Acme\PSR4;
4+
5+
use Acme\PSR4\Routing\UrlTrait;
6+
use olvlvl\ComposerAttributeCollector\InheritsAttributes;
7+
8+
#[InheritsAttributes]
9+
class InheritedAttributeSample
10+
{
11+
use UrlTrait;
12+
}

tests/Acme/PSR4/Routing/UrlTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Acme\PSR4\Routing;
4+
5+
use Acme\Attribute\Routing\UrlGetter;
6+
7+
trait UrlTrait
8+
{
9+
#[UrlGetter]
10+
public function get_url(): string
11+
{
12+
return '/url';
13+
}
14+
}

tests/PluginTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
<?php
22

3-
/*
4-
* (c) Olivier Laviale <olivier.laviale@gmail.com>
5-
*
6-
* For the full copyright and license information, please view the LICENSE
7-
* file that was distributed with this source code.
8-
*/
9-
103
namespace tests\olvlvl\ComposerAttributeCollector;
114

125
use Acme\Attribute\ActiveRecord\Boolean;
@@ -21,6 +14,7 @@
2114
use Acme\Attribute\Permission;
2215
use Acme\Attribute\Resource;
2316
use Acme\Attribute\Route;
17+
use Acme\Attribute\Routing\UrlGetter;
2418
use Acme\Attribute\Subscribe;
2519
use Acme\PSR4\Presentation\ArticleController;
2620
use Composer\IO\NullIO;
@@ -44,9 +38,6 @@ final class PluginTest extends TestCase
4438
{
4539
private static bool $initialized = false;
4640

47-
/**
48-
* @throws ReflectionException
49-
*/
5041
protected function setUp(): void
5142
{
5243
parent::setUp();
@@ -184,6 +175,12 @@ public static function provideTargetMethods(): array
184175
[ new Subscribe(), 'Acme\PSR4\SubscriberB::onEventA' ],
185176
]
186177
],
178+
[
179+
UrlGetter::class,
180+
[
181+
[ new UrlGetter(), 'Acme\PSR4\InheritedAttributeSample::get_url' ]
182+
]
183+
],
187184

188185
];
189186
}

0 commit comments

Comments
 (0)