Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor type resolving #287

Merged
merged 6 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@
<service id="SensioLabs\Deptrac\AstRunner\AstParser\NikicPhpParser\NikicPhpParser">
<argument type="service" id="SensioLabs\Deptrac\AstRunner\AstParser\NikicPhpParser\FileParser"/>
<argument type="service" id="SensioLabs\Deptrac\AstRunner\AstParser\AstFileReferenceCache"/>
<argument type="service" id="SensioLabs\Deptrac\AstRunner\Resolver\TypeResolver"/>
<argument type="service" id="SensioLabs\Deptrac\AstRunner\Resolver\AnnotationDependencyResolver"/>
<argument type="service" id="SensioLabs\Deptrac\AstRunner\Resolver\AnonymousClassResolver"/>
<argument type="service" id="SensioLabs\Deptrac\AstRunner\Resolver\ClassConstantResolver"/>
</service>

<service id="SensioLabs\Deptrac\AstRunner\Resolver\AnnotationDependencyResolver"/>
<service id="SensioLabs\Deptrac\AstRunner\Resolver\TypeResolver"/>

<service id="SensioLabs\Deptrac\AstRunner\Resolver\AnnotationDependencyResolver">
<argument type="service" id="SensioLabs\Deptrac\AstRunner\Resolver\TypeResolver"/>
</service>

<service id="SensioLabs\Deptrac\AstRunner\Resolver\AnonymousClassResolver"/>

Expand Down
33 changes: 19 additions & 14 deletions src/AstRunner/AstMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace SensioLabs\Deptrac\AstRunner;

use ArrayObject;
use SensioLabs\Deptrac\AstRunner\AstMap\AstClassReference;
use SensioLabs\Deptrac\AstRunner\AstMap\AstFileReference;
use SensioLabs\Deptrac\AstRunner\AstMap\AstInherit;
use SensioLabs\Deptrac\AstRunner\AstMap\ClassLikeName;
use SplStack;

class AstMap
{
Expand Down Expand Up @@ -46,17 +49,17 @@ public function getAstFileReferences(): array
return $this->astFileReferences;
}

public function getClassReferenceByClassName(string $className): ?AstClassReference
public function getClassReferenceByClassName(ClassLikeName $className): ?AstClassReference
{
return $this->astClassReferences[$className] ?? null;
return $this->astClassReferences[$className->toString()] ?? null;
}

/**
* @return AstInherit[]|iterable
*/
public function getClassInherits(string $className): iterable
public function getClassInherits(ClassLikeName $classLikeName): iterable
{
$classReference = $this->getClassReferenceByClassName($className);
$classReference = $this->getClassReferenceByClassName($classLikeName);

if (null === $classReference) {
return [];
Expand All @@ -69,51 +72,53 @@ public function getClassInherits(string $className): iterable
}

/**
* @param \ArrayObject<string, bool>|null $alreadyResolved
* @param ArrayObject<string, true>|null $alreadyResolved
*
* @return iterable<AstInherit>
*/
private function resolveDepsRecursive(
AstInherit $inheritDependency,
\ArrayObject $alreadyResolved = null,
\SplStack $path = null
ArrayObject $alreadyResolved = null,
SplStack $path = null
): iterable {
$alreadyResolved = $alreadyResolved ?? new \ArrayObject();
$alreadyResolved = $alreadyResolved ?? new ArrayObject();

if (null === $path) {
$path = new \SplStack();
$path = new SplStack();
$path->push($inheritDependency);
}

if (isset($alreadyResolved[$inheritDependency->getClassName()])) {
$className = $inheritDependency->getClassLikeName()->toString();

if (isset($alreadyResolved[$className])) {
$path->pop();

return [];
}

$classReference = $this->getClassReferenceByClassName($inheritDependency->getClassName());
$classReference = $this->getClassReferenceByClassName($inheritDependency->getClassLikeName());

if (null === $classReference) {
return [];
}

foreach ($classReference->getInherits() as $inherit) {
$alreadyResolved[$inheritDependency->getClassName()] = true;
$alreadyResolved[$className] = true;

yield $inherit->withPath(iterator_to_array($path));

$path->push($inherit);

yield from $this->resolveDepsRecursive($inherit, $alreadyResolved, $path);

unset($alreadyResolved[$inheritDependency->getClassName()]);
unset($alreadyResolved[$className]);
$path->pop();
}
}

private function addAstClassReference(AstClassReference $astClassReference): void
{
$this->astClassReferences[$astClassReference->getClassName()] = $astClassReference;
$this->astClassReferences[$astClassReference->getClassLikeName()->toString()] = $astClassReference;
}

private function addAstFileReference(AstFileReference $astFileReference): void
Expand Down
30 changes: 12 additions & 18 deletions src/AstRunner/AstMap/AstClassReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class AstClassReference
{
private $className;
private $classLikeName;
private $fileReference;

/** @var AstDependency[] */
Expand All @@ -15,22 +15,26 @@ class AstClassReference
/** @var AstInherit[] */
private $inherits;

public function __construct(string $className, AstFileReference $fileReference = null)
/**
* @param AstInherit[] $inherits
* @param AstDependency[] $dependencies
*/
public function __construct(ClassLikeName $classLikeName, AstFileReference $fileReference = null, array $inherits = [], array $dependencies = [])
{
$this->className = $className;
$this->classLikeName = $classLikeName;
$this->fileReference = $fileReference;
$this->dependencies = [];
$this->inherits = [];
$this->dependencies = $dependencies;
$this->inherits = $inherits;
}

public function getFileReference(): ?AstFileReference
{
return $this->fileReference;
return $this->fileReference ? clone $this->fileReference : null;
}

public function getClassName(): string
public function getClassLikeName(): ClassLikeName
{
return $this->className;
return $this->classLikeName;
}

/**
Expand All @@ -48,14 +52,4 @@ public function getInherits(): array
{
return $this->inherits;
}

public function addDependency(AstDependency $dependency): void
{
$this->dependencies[] = $dependency;
}

public function addInherit(AstInherit $inherit): void
{
$this->inherits[] = $inherit;
}
}
62 changes: 31 additions & 31 deletions src/AstRunner/AstMap/AstDependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

class AstDependency
{
private $class;
private $classLikeName;
private $fileOccurrence;
private $type;

public function __construct(string $class, FileOccurrence $fileOccurrence, string $type)
private function __construct(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence, string $type)
{
$this->class = $class;
$this->classLikeName = $classLikeName;
$this->fileOccurrence = $fileOccurrence;
$this->type = $type;
}

public function getClass(): string
public function getClassLikeName(): ClassLikeName
{
return $this->class;
return $this->classLikeName;
}

public function getFileOccurrence(): FileOccurrence
Expand All @@ -32,68 +32,68 @@ public function getType(): string
return $this->type;
}

public static function useStmt(string $class, FileOccurrence $fileOccurrence): self
public static function useStmt(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'use');
return new self($classLikeName, $fileOccurrence, 'use');
}

public static function returnType(string $class, FileOccurrence $fileOccurrence): self
public static function returnType(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'returntype');
return new self($classLikeName, $fileOccurrence, 'returntype');
}

public static function parameter(string $class, FileOccurrence $fileOccurrence): self
public static function parameter(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'parameter');
return new self($classLikeName, $fileOccurrence, 'parameter');
}

public static function newStmt(string $class, FileOccurrence $fileOccurrence): self
public static function newStmt(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'new');
return new self($classLikeName, $fileOccurrence, 'new');
}

public static function staticProperty(string $class, FileOccurrence $fileOccurrence): self
public static function staticProperty(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'static_property');
return new self($classLikeName, $fileOccurrence, 'static_property');
}

public static function staticMethod(string $class, FileOccurrence $fileOccurrence): self
public static function staticMethod(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'static_method');
return new self($classLikeName, $fileOccurrence, 'static_method');
}

public static function instanceofExpr(string $class, FileOccurrence $fileOccurrence): self
public static function instanceofExpr(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'instanceof');
return new self($classLikeName, $fileOccurrence, 'instanceof');
}

public static function catchStmt(string $class, FileOccurrence $fileOccurrence): self
public static function catchStmt(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'catch');
return new self($classLikeName, $fileOccurrence, 'catch');
}

public static function variable(string $class, FileOccurrence $fileOccurrence): self
public static function variable(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'variable');
return new self($classLikeName, $fileOccurrence, 'variable');
}

public static function throwStmt(string $class, FileOccurrence $fileOccurrence): self
public static function throwStmt(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'throw');
return new self($classLikeName, $fileOccurrence, 'throw');
}

public static function constFetch(string $class, FileOccurrence $fileOccurrence): self
public static function constFetch(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'const');
return new self($classLikeName, $fileOccurrence, 'const');
}

public static function anonymousClassExtends(string $class, FileOccurrence $fileOccurrence): self
public static function anonymousClassExtends(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'anonymous_class_extends');
return new self($classLikeName, $fileOccurrence, 'anonymous_class_extends');
}

public static function anonymousClassImplements(string $class, FileOccurrence $fileOccurrence): self
public static function anonymousClassImplements(ClassLikeName $classLikeName, FileOccurrence $fileOccurrence): self
{
return new self($class, $fileOccurrence, 'anonymous_class_implements');
return new self($classLikeName, $fileOccurrence, 'anonymous_class_implements');
}
}
8 changes: 6 additions & 2 deletions src/AstRunner/AstMap/AstFileReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ public function __construct(string $filepath)
$this->dependencies = [];
}

public function addClassReference(string $className): AstClassReference
/**
* @param AstInherit[] $inherits
* @param AstDependency[] $dependencies
*/
public function addClassReference(ClassLikeName $className, array $inherits = [], array $dependencies = []): AstClassReference
{
$astClassReference = new AstClassReference($className, $this);
$astClassReference = new AstClassReference($className, $this, $inherits, $dependencies);

$this->astClassReferences[] = $astClassReference;

Expand Down
18 changes: 9 additions & 9 deletions src/AstRunner/AstMap/AstInherit.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ class AstInherit
private const TYPE_IMPLEMENTS = 2;
private const TYPE_USES = 3;

private $className;
private $classLikeName;
private $fileOccurrence;
private $type;

/** @var AstInherit[] */
private $path;

private function __construct(string $className, FileOccurrence $fileOccurrence, int $type)
private function __construct(ClassLikeName $className, FileOccurrence $fileOccurrence, int $type)
{
$this->className = $className;
$this->classLikeName = $className;
$this->fileOccurrence = $fileOccurrence;
$this->type = $type;
$this->path = [];
}

public static function newExtends(string $className, FileOccurrence $fileOccurrence): self
public static function newExtends(ClassLikeName $className, FileOccurrence $fileOccurrence): self
{
return new self($className, $fileOccurrence, static::TYPE_EXTENDS);
}

public static function newImplements(string $className, FileOccurrence $fileOccurrence): self
public static function newImplements(ClassLikeName $className, FileOccurrence $fileOccurrence): self
{
return new self($className, $fileOccurrence, static::TYPE_IMPLEMENTS);
}

public static function newTraitUse(string $className, FileOccurrence $fileOccurrence): self
public static function newTraitUse(ClassLikeName $className, FileOccurrence $fileOccurrence): self
{
return new self($className, $fileOccurrence, static::TYPE_USES);
}
Expand All @@ -56,7 +56,7 @@ public function __toString(): string
$type = 'Unknown';
}

$description = "{$this->className}::{$this->fileOccurrence->getLine()} ($type)";
$description = "{$this->classLikeName->toString()}::{$this->fileOccurrence->getLine()} ($type)";

if (0 === count($this->path)) {
return $description;
Expand All @@ -70,9 +70,9 @@ public function __toString(): string
return $description.' (path: '.rtrim($buffer, ' -> ').')';
}

public function getClassName(): string
public function getClassLikeName(): ClassLikeName
{
return $this->className;
return $this->classLikeName;
}

public function getFileOccurrence(): FileOccurrence
Expand Down
Loading