Skip to content

Commit

Permalink
Merge pull request #527 from tugmaks/php-attributes
Browse files Browse the repository at this point in the history
Support attributes
  • Loading branch information
Ocramius authored Mar 5, 2024
2 parents 4ad3678 + ec3264b commit 0a6f7ba
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/ComposerRequireChecker/Cli/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

namespace ComposerRequireChecker\Cli;

use AllowDynamicProperties;
use Attribute;
use ComposerRequireChecker\FileLocator\LocateComposerPackageSourceFiles;
use InvalidArgumentException;
use Override;
use ReturnTypeWillChange;
use SensitiveParameter;

use function array_merge;
use function array_unique;
Expand Down Expand Up @@ -36,8 +41,16 @@ class Options
'never',
];

private const PHP_ATTRIBUTES = [
AllowDynamicProperties::class,
Attribute::class,
Override::class,
ReturnTypeWillChange::class,
SensitiveParameter::class,
];

/** @var array<string> */
private array $symbolWhitelist = self::PHP_LANGUAGE_TYPES;
private array $symbolWhitelist;

/** @var array<string> */
private array $phpCoreExtensions = [
Expand All @@ -63,6 +76,8 @@ class Options
/** @param array<string, mixed> $options */
public function __construct(array $options = [])
{
$this->symbolWhitelist = array_merge(self::PHP_LANGUAGE_TYPES, self::PHP_ATTRIBUTES);

/** @var mixed $option */
foreach ($options as $key => $option) {
$methodName = 'set' . $this->getCamelCase($key);
Expand Down Expand Up @@ -97,6 +112,7 @@ public function setSymbolWhitelist(array $symbolWhitelist): void
*/
$this->symbolWhitelist = array_unique(array_merge(
self::PHP_LANGUAGE_TYPES,
self::PHP_ATTRIBUTES,
$symbolWhitelist,
));
}
Expand Down
10 changes: 10 additions & 0 deletions src/ComposerRequireChecker/NodeVisitor/UsedSymbolCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function enterNode(Node $node)
$this->recordConstantFetchUsage($node);
$this->recordTraitUsage($node);
$this->recordPropertyTypeUsage($node);
$this->recordAttributeUsage($node);

return parent::enterNode($node);
}
Expand Down Expand Up @@ -204,6 +205,15 @@ private function recordPropertyTypeUsage(Node $node): void
$this->recordUsageOf($node->type);
}

private function recordAttributeUsage(Node $node): void
{
if (! $node instanceof Node\Attribute) {
return;
}

$this->recordUsageOf($node->name);
}

private function recordUsageOf(Node\Name $symbol): void
{
$this->recordUsageOfByString($symbol->toString());
Expand Down
2 changes: 1 addition & 1 deletion test/ComposerRequireCheckerTest/BinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testUnknownSymbols(): void
$path = __DIR__ . '/../fixtures/unknownSymbols/composer.json';
exec(sprintf('%s check %s 2>&1', $this->bin, $path), $output, $return);
$this->assertSame(1, $return);
$this->assertStringContainsString('The following 6 unknown symbols were found', implode("\n", $output));
$this->assertStringContainsString('The following 7 unknown symbols were found', implode("\n", $output));
}

public function testInvalidConfiguration(): void
Expand Down
17 changes: 16 additions & 1 deletion test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ public function testUnknownSymbolsFound(): void
$this->assertSame(Command::FAILURE, $this->commandTester->getStatusCode());
$display = $this->commandTester->getDisplay();

$this->assertStringContainsString('The following 6 unknown symbols were found:', $display);
$this->assertStringContainsString('The following 7 unknown symbols were found:', $display);
$this->assertStringContainsString('Doctrine\Common\Collections\ArrayCollection', $display);
$this->assertStringContainsString('Example\Library\Dependency', $display);
$this->assertStringContainsString('FILTER_VALIDATE_URL', $display);
$this->assertStringContainsString('filter_var', $display);
$this->assertStringContainsString('Foo\Bar\Baz', $display);
$this->assertStringContainsString('libxml_clear_errors', $display);
$this->assertStringContainsString('Vendor\UnknownAttribute ', $display);
}

public function testInvalidOutputOptionValue(): void
Expand Down Expand Up @@ -111,6 +112,7 @@ public function testUnknownSymbolsFoundJsonReport(): void
'filter_var' => ['ext-filter'],
'Foo\Bar\Baz' => [],
'libxml_clear_errors' => ['ext-libxml'],
'Vendor\UnknownAttribute' => [],
],
$actual['unknown-symbols'],
);
Expand Down Expand Up @@ -349,4 +351,17 @@ public function testNoUnknownEnumSymbolsFound(): void
$this->commandTester->getDisplay(),
);
}

public function testNoUnknownAttributeSymbolsFound(): void
{
$this->commandTester->execute([
'composer-json' => dirname(__DIR__, 2) . '/fixtures/noUnknownAttributeSymbols/composer.json',
]);

self::assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
self::assertStringContainsString(
'There were no unknown symbols found.',
$this->commandTester->getDisplay(),
);
}
}
10 changes: 10 additions & 0 deletions test/ComposerRequireCheckerTest/Cli/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function testOptionsAcceptSymbolWhitelistAndFiltersDuplicates(): void
'object',
'mixed',
'never',
'AllowDynamicProperties',
'Attribute',
'Override',
'ReturnTypeWillChange',
'SensitiveParameter',
'foo',
'bar',
], $options->getSymbolWhitelist());
Expand Down Expand Up @@ -100,6 +105,11 @@ public function testPublicSetters(): void
'object',
'mixed',
'never',
'AllowDynamicProperties',
'Attribute',
'Override',
'ReturnTypeWillChange',
'SensitiveParameter',
'foo',
'bar',
], $options->getSymbolWhitelist());
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/noUnknownAttributeSymbols/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "example/library",
"require": {
"php": "8.1.*"
},
"autoload": {
"psr-4": {
"Example\\Library\\": "src/"
}
}
}
22 changes: 22 additions & 0 deletions test/fixtures/noUnknownAttributeSymbols/src/SomeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Example\Library;

use AllowDynamicProperties;
use ReturnTypeWillChange;

#[WellKnownAttribute]
final class SomeClass
{
#[ReturnTypeWillChange]
public function test(): void
{
}

#[AllowDynamicProperties]
public function make(): void
{
}
}
12 changes: 12 additions & 0 deletions test/fixtures/noUnknownAttributeSymbols/src/WellKnownAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Example\Library;

use Attribute;

#[Attribute]
final class WellKnownAttribute
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
11 changes: 11 additions & 0 deletions test/fixtures/unknownSymbols/src/AttributeThing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Example\Library;


#[\Vendor\UnknownAttribute]
class AttributeThing
{
}

0 comments on commit 0a6f7ba

Please sign in to comment.