Skip to content

Commit

Permalink
Optimize parser FullClassName
Browse files Browse the repository at this point in the history
  • Loading branch information
nreynis authored Sep 5, 2021
1 parent 6d39e1c commit 54958a8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
11 changes: 6 additions & 5 deletions src/Parser/Ast/FullClassName.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ class FullClassName implements ClassLike
{
private $namespace;
private $name;
private $fqcn;

public function __construct(string $namespace, string $name)
private function __construct(string $namespace, string $name, string $fqcn)
{
$this->namespace = $namespace;
$this->name = $name;
$this->fqcn = $fqcn;
}

public static function createFromFQCN(string $fqcn): self
{
$parts = explode('\\', ltrim($fqcn, '\\'));
$name = array_pop($parts);
$normalizedFqcn = empty($parts) ? $name : $fqcn;

return new self(implode('\\', $parts), $name);
return new self(implode('\\', $parts), $name, $normalizedFqcn);
}

public function getNamespace(): string
Expand All @@ -33,9 +36,7 @@ public function getName(): string

public function getFQCN(): string
{
return (empty($this->getNamespace()))
? $this->getName()
: $this->getNamespace() . '\\' . $this->getName();
return $this->fqcn;
}

public function matches(string $name): bool
Expand Down
28 changes: 14 additions & 14 deletions tests/unit/Rule/Assertion/AbstractAssertionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,25 @@ protected function getMap(): ReferenceMap
[
new SrcNode(
new \SplFileInfo('folder/Example/ClassExample.php'),
new FullClassName('Example', 'ClassExample'),
FullClassName::createFromFQCN('Example\\ClassExample'),
[
new Inheritance(0, new FullClassName('Example', 'ParentClassExample')),
new Inheritance(0, new FullClassName('', 'FilterIterator')),
new Dependency(0, new FullClassName('Example', 'AnotherClassExample')),
new Dependency(0, new FullClassName('Vendor', 'ThirdPartyExample')),
new Dependency(0, new FullClassName('', 'iterable')),
new Composition(0, new FullClassName('Example', 'InterfaceExample')),
new Composition(0, new FullClassName('Example', 'AnotherInterface')),
new Composition(0, new FullClassName('', 'iterable')),
new Mixin(0, new FullClassName('Example', 'TraitExample')),
new Mixin(0, new FullClassName('', 'PHPDocElement'))
new Inheritance(0, FullClassName::createFromFQCN('Example\\ParentClassExample')),
new Inheritance(0, FullClassName::createFromFQCN('\\FilterIterator')),
new Dependency(0, FullClassName::createFromFQCN('Example\\AnotherClassExample')),
new Dependency(0, FullClassName::createFromFQCN('Vendor\\ThirdPartyExample')),
new Dependency(0, FullClassName::createFromFQCN('iterable')),
new Composition(0, FullClassName::createFromFQCN('Example\\InterfaceExample')),
new Composition(0, FullClassName::createFromFQCN('Example\\AnotherInterface')),
new Composition(0, FullClassName::createFromFQCN('iterable')),
new Mixin(0, FullClassName::createFromFQCN('Example\\TraitExample')),
new Mixin(0, FullClassName::createFromFQCN('PHPDocElement'))
]
)
],
[
new FullClassName('', 'iterable'),
new FullClassName('', 'FilterIterator'),
new FullClassName('', 'PHPDocElement'),
FullClassName::createFromFQCN('iterable'),
FullClassName::createFromFQCN('\\FilterIterator'),
FullClassName::createFromFQCN('PHPDocElement'),
],
[
new ComposerPackage('main', [], [], [], [])
Expand Down
28 changes: 14 additions & 14 deletions tests/unit/Selector/AbstractSelectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,25 @@ protected function getMap(): ReferenceMap
[
new SrcNode(
new \SplFileInfo('folder/Example/ClassExample.php'),
new FullClassName('Example', 'ClassExample'),
FullClassName::createFromFQCN('Example\\ClassExample'),
[
new Inheritance(0, new FullClassName('Example', 'ParentClassExample')),
new Inheritance(0, new FullClassName('', 'FilterIterator')),
new Dependency(0, new FullClassName('Example', 'AnotherClassExample')),
new Dependency(0, new FullClassName('Vendor', 'ThirdPartyExample')),
new Dependency(0, new FullClassName('', 'iterable')),
new Composition(0, new FullClassName('Example', 'InterfaceExample')),
new Composition(0, new FullClassName('Example', 'AnotherInterface')),
new Composition(0, new FullClassName('', 'iterable')),
new Mixin(0, new FullClassName('Example', 'TraitExample')),
new Mixin(0, new FullClassName('', 'PHPDocElement'))
new Inheritance(0, FullClassName::createFromFQCN('Example\\ParentClassExample')),
new Inheritance(0, FullClassName::createFromFQCN('\\FilterIterator')),
new Dependency(0, FullClassName::createFromFQCN('Example\\AnotherClassExample')),
new Dependency(0, FullClassName::createFromFQCN('Vendor\\ThirdPartyExample')),
new Dependency(0, FullClassName::createFromFQCN('iterable')),
new Composition(0, FullClassName::createFromFQCN('Example\\InterfaceExample')),
new Composition(0, FullClassName::createFromFQCN('Example\\AnotherInterface')),
new Composition(0, FullClassName::createFromFQCN('iterable')),
new Mixin(0, FullClassName::createFromFQCN('Example\\TraitExample')),
new Mixin(0, FullClassName::createFromFQCN('PHPDocElement'))
]
)
],
[
new FullClassName('', 'iterable'),
new FullClassName('', 'FilterIterator'),
new FullClassName('', 'PHPDocElement'),
FullClassName::createFromFQCN('iterable'),
FullClassName::createFromFQCN('\\FilterIterator'),
FullClassName::createFromFQCN('PHPDocElement'),
],
[
new ComposerPackage('main', [], [], [], [])
Expand Down

0 comments on commit 54958a8

Please sign in to comment.