Skip to content

Commit

Permalink
naming + explicit toString method + fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smoench committed Feb 5, 2020
1 parent fd88140 commit 3e2b334
Show file tree
Hide file tree
Showing 50 changed files with 282 additions and 279 deletions.
22 changes: 12 additions & 10 deletions src/AstRunner/AstMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +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 @@ -49,15 +51,15 @@ public function getAstFileReferences(): array

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

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

if (null === $classReference) {
return [];
Expand All @@ -74,26 +76,26 @@ public function getClassInherits(ClassLikeName $className): iterable
*/
private function resolveDepsRecursive(
AstInherit $inheritDependency,
\ArrayObject $alreadyResolved = null,
\SplStack $path = null
ArrayObject $alreadyResolved = null,
SplStack $path = null
): iterable {
if (null === $alreadyResolved) {
$alreadyResolved = new \ArrayObject();
$alreadyResolved = new ArrayObject();
}
if (null === $path) {
$path = new \SplStack();
$path = new SplStack();
$path->push($inheritDependency);
}

$className = (string) $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 [];
Expand All @@ -115,7 +117,7 @@ private function resolveDepsRecursive(

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

private function addAstFileReference(AstFileReference $astFileReference): void
Expand Down
22 changes: 6 additions & 16 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 @@ -19,22 +19,22 @@ class AstClassReference
* @param AstInherit[] $inherits
* @param AstDependency[] $dependencies
*/
public function __construct(ClassLikeName $className, AstFileReference $fileReference = null, array $inherits = [], array $dependencies = [])
public function __construct(ClassLikeName $classLikeName, AstFileReference $fileReference = null, array $inherits = [], array $dependencies = [])
{
$this->className = $className;
$this->classLikeName = $classLikeName;
$this->fileReference = $fileReference;
$this->dependencies = $dependencies;
$this->inherits = $inherits;
}

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

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

/**
Expand All @@ -52,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;
}
}
60 changes: 30 additions & 30 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;

private function __construct(ClassLikeName $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 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(ClassLikeName $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(ClassLikeName $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(ClassLikeName $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(ClassLikeName $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(ClassLikeName $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(ClassLikeName $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(ClassLikeName $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(ClassLikeName $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(ClassLikeName $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(ClassLikeName $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(ClassLikeName $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(ClassLikeName $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(ClassLikeName $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');
}
}
10 changes: 5 additions & 5 deletions src/AstRunner/AstMap/AstInherit.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AstInherit
private const TYPE_IMPLEMENTS = 2;
private const TYPE_USES = 3;

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

Expand All @@ -19,7 +19,7 @@ class AstInherit

private function __construct(ClassLikeName $className, FileOccurrence $fileOccurrence, int $type)
{
$this->className = $className;
$this->classLikeName = $className;
$this->fileOccurrence = $fileOccurrence;
$this->type = $type;
$this->path = [];
Expand Down Expand Up @@ -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(): ClassLikeName
public function getClassLikeName(): ClassLikeName
{
return $this->className;
return $this->classLikeName;
}

public function getFileOccurrence(): FileOccurrence
Expand Down
8 changes: 4 additions & 4 deletions src/AstRunner/AstMap/ClassLikeName.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ final class ClassLikeName

private function __construct(string $className)
{
$this->className = ltrim($className, '\\');
$this->className = $className;
}

public static function fromString(string $className): self
public static function fromFQCN(string $className): self
{
return new self($className);
return new self(ltrim($className, '\\'));
}

public function match(string $pattern): bool
{
return 1 === preg_match($pattern, $this->className);
}

public function __toString()
public function toString(): string
{
return $this->className;
}
Expand Down
Loading

0 comments on commit 3e2b334

Please sign in to comment.