Skip to content

Commit

Permalink
Resolve #11: Add TypedAttributeStrategy
Browse files Browse the repository at this point in the history
Using this strategy, the collector will recognize consumed symbols by typed properties.
  • Loading branch information
Andreas Frömer authored and icanhazstring committed Mar 9, 2022
1 parent 8573c25 commit 10d94a3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### Fixed
- Fixed issue where `UseStrategy`, `ExtendsParseStrategy` and `ImplementsParseStrategy` resulted in partial symbol names
### Added
- Added `TypedAttributeStrategy` to recognize usage for consumed full qualified property types
### Changed
### Removed

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PHP_VERSION=7.4
PHP_VERSION=8.1

up: ## Run all containers in all versions
docker compose up -d
Expand Down
32 changes: 32 additions & 0 deletions src/Parser/PHP/Strategy/TypedAttributeStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace ComposerUnused\SymbolParser\Parser\PHP\Strategy;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;

final class TypedAttributeStrategy implements StrategyInterface
{
public function canHandle(Node $node): bool
{
if (!$node instanceof Node\Stmt\Property) {
return false;
}

return $node->type instanceof FullyQualified;
}

/**
* @param Node&Node\Stmt\Property $node
* @return array<string>
*/
public function extractSymbolNames(Node $node): array
{
/** @var FullyQualified $type */
$type = $node->type;

return [$type->toString()];
}
}
35 changes: 35 additions & 0 deletions tests/Unit/Parser/PHP/SymbolNameParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\ExtendsParseStrategy;
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\FunctionInvocationStrategy;
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\ImplementsParseStrategy;
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\TypedAttributeStrategy;
use ComposerUnused\SymbolParser\Parser\PHP\Strategy\UseStrategy;
use ComposerUnused\SymbolParser\Parser\PHP\SymbolNameParser;
use PhpParser\ParserFactory;
Expand Down Expand Up @@ -123,4 +124,38 @@ class Foo extends MyClass implements NameSpace1\Bar, \Other\Namespace2\Baz {
self::assertSame('B\NS\MyClass', $symbols[1]);
self::assertSame('Other\Namespace2\Baz', $symbols[2]);
}

/**
* @test
*/
public function itShouldParseSymbolFromTypedAttribute(): void
{
$code = <<<CODE
<?php
namespace Testing;
use Other\Fubar;
class Foo {
private \$foo;
private Fubar \$baz;
private \My\Namespace\Bar \$bar;
}
CODE;

$symbolNameParser = new SymbolNameParser(
(new ParserFactory())->create(ParserFactory::ONLY_PHP7),
new ConsumedSymbolCollector(
[
new TypedAttributeStrategy(),
]
)
);

$symbols = iterator_to_array($symbolNameParser->parseSymbolNames($code));

self::assertCount(1, $symbols);
self::assertSame('My\Namespace\Bar', $symbols[0]);
}
}

0 comments on commit 10d94a3

Please sign in to comment.