Skip to content

Commit

Permalink
Refactor DependencyInterface to use DependencyContext instead of File…
Browse files Browse the repository at this point in the history
…Occurence and DependencyType, so that we do not add a new method to the interface and constructor parameters every time we want to add a piece of information about the dependency.

Created to enable #1359
  • Loading branch information
patrickkusebauch committed Feb 7, 2024
1 parent 5dd4bbc commit bfb213d
Show file tree
Hide file tree
Showing 41 changed files with 216 additions and 214 deletions.
20 changes: 20 additions & 0 deletions src/Contract/Ast/DependencyContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Qossmic\Deptrac\Contract\Ast;

/**
* @psalm-immutable
*
* Context of the dependency.
*
* Any additional info about where the dependency occurred.
*/
final class DependencyContext
{
public function __construct(
public readonly FileOccurrence $fileOccurrence,
public readonly DependencyType $dependencyType
) {}
}
7 changes: 2 additions & 5 deletions src/Contract/Dependency/DependencyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Qossmic\Deptrac\Contract\Dependency;

use Qossmic\Deptrac\Contract\Ast\DependencyType;
use Qossmic\Deptrac\Contract\Ast\FileOccurrence;
use Qossmic\Deptrac\Contract\Ast\DependencyContext;
use Qossmic\Deptrac\Contract\Ast\TokenInterface;

/**
Expand All @@ -17,12 +16,10 @@ public function getDepender(): TokenInterface;

public function getDependent(): TokenInterface;

public function getFileOccurrence(): FileOccurrence;
public function getContext(): DependencyContext;

/**
* @return array<array{name:string, line:int}>
*/
public function serialize(): array;

public function getType(): DependencyType;
}
6 changes: 2 additions & 4 deletions src/Core/Ast/AstMap/DependencyToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Qossmic\Deptrac\Core\Ast\AstMap;

use Qossmic\Deptrac\Contract\Ast\DependencyType;
use Qossmic\Deptrac\Contract\Ast\FileOccurrence;
use Qossmic\Deptrac\Contract\Ast\DependencyContext;
use Qossmic\Deptrac\Contract\Ast\TokenInterface;

/**
Expand All @@ -15,7 +14,6 @@ class DependencyToken
{
public function __construct(
public readonly TokenInterface $token,
public readonly FileOccurrence $fileOccurrence,
public readonly DependencyType $type
public readonly DependencyContext $context,
) {}
}
4 changes: 1 addition & 3 deletions src/Core/Ast/AstMap/File/FileReferenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Qossmic\Deptrac\Core\Ast\AstMap\File;

use Qossmic\Deptrac\Contract\Ast\DependencyType;
use Qossmic\Deptrac\Contract\Ast\FileOccurrence;
use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeReferenceBuilder;
use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeToken;
use Qossmic\Deptrac\Core\Ast\AstMap\DependencyToken;
Expand All @@ -29,8 +28,7 @@ public function useStatement(string $classLikeName, int $occursAtLine): self
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::USE
$this->createContext($occursAtLine, DependencyType::USE),
);

return $this;
Expand Down
54 changes: 22 additions & 32 deletions src/Core/Ast/AstMap/ReferenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Qossmic\Deptrac\Core\Ast\AstMap;

use Qossmic\Deptrac\Contract\Ast\DependencyContext;
use Qossmic\Deptrac\Contract\Ast\DependencyType;
use Qossmic\Deptrac\Contract\Ast\FileOccurrence;
use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeToken;
Expand All @@ -28,6 +29,11 @@ final public function getTokenTemplates(): array
return $this->tokenTemplates;
}

protected function createContext(int $occursAtLine, DependencyType $type): DependencyContext
{
return new DependencyContext(new FileOccurrence($this->filepath, $occursAtLine), $type);
}

/**
* Unqualified function and constant names inside a namespace cannot be
* statically resolved. Inside a namespace Foo, a call to strlen() may
Expand All @@ -39,8 +45,7 @@ public function unresolvedFunctionCall(string $functionName, int $occursAtLine):
{
$this->dependencies[] = new DependencyToken(
FunctionToken::fromFQCN($functionName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::UNRESOLVED_FUNCTION_CALL
$this->createContext($occursAtLine, DependencyType::UNRESOLVED_FUNCTION_CALL),
);

return $this;
Expand All @@ -50,8 +55,7 @@ public function variable(string $classLikeName, int $occursAtLine): self
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::VARIABLE
$this->createContext($occursAtLine, DependencyType::VARIABLE),
);

return $this;
Expand All @@ -61,17 +65,15 @@ public function superglobal(string $superglobalName, int $occursAtLine): void
{
$this->dependencies[] = new DependencyToken(
SuperGlobalToken::from($superglobalName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::SUPERGLOBAL_VARIABLE
$this->createContext($occursAtLine, DependencyType::SUPERGLOBAL_VARIABLE),
);
}

public function returnType(string $classLikeName, int $occursAtLine): self
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::RETURN_TYPE
$this->createContext($occursAtLine, DependencyType::RETURN_TYPE),
);

return $this;
Expand All @@ -81,8 +83,7 @@ public function throwStatement(string $classLikeName, int $occursAtLine): self
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::THROW
$this->createContext($occursAtLine, DependencyType::THROW),
);

return $this;
Expand All @@ -92,44 +93,39 @@ public function anonymousClassExtends(string $classLikeName, int $occursAtLine):
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::ANONYMOUS_CLASS_EXTENDS
$this->createContext($occursAtLine, DependencyType::ANONYMOUS_CLASS_EXTENDS),
);
}

public function anonymousClassTrait(string $classLikeName, int $occursAtLine): void
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::ANONYMOUS_CLASS_TRAIT
$this->createContext($occursAtLine, DependencyType::ANONYMOUS_CLASS_TRAIT),
);
}

public function constFetch(string $classLikeName, int $occursAtLine): void
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::CONST
$this->createContext($occursAtLine, DependencyType::CONST),
);
}

public function anonymousClassImplements(string $classLikeName, int $occursAtLine): void
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::ANONYMOUS_CLASS_IMPLEMENTS
$this->createContext($occursAtLine, DependencyType::ANONYMOUS_CLASS_IMPLEMENTS),
);
}

public function parameter(string $classLikeName, int $occursAtLine): self
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::PARAMETER
$this->createContext($occursAtLine, DependencyType::PARAMETER),
);

return $this;
Expand All @@ -139,8 +135,7 @@ public function attribute(string $classLikeName, int $occursAtLine): self
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::ATTRIBUTE
$this->createContext($occursAtLine, DependencyType::ATTRIBUTE),
);

return $this;
Expand All @@ -150,8 +145,7 @@ public function instanceof(string $classLikeName, int $occursAtLine): self
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::INSTANCEOF
$this->createContext($occursAtLine, DependencyType::INSTANCEOF),
);

return $this;
Expand All @@ -161,8 +155,7 @@ public function newStatement(string $classLikeName, int $occursAtLine): self
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::NEW
$this->createContext($occursAtLine, DependencyType::NEW),
);

return $this;
Expand All @@ -172,8 +165,7 @@ public function staticProperty(string $classLikeName, int $occursAtLine): self
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::STATIC_PROPERTY
$this->createContext($occursAtLine, DependencyType::STATIC_PROPERTY),
);

return $this;
Expand All @@ -183,8 +175,7 @@ public function staticMethod(string $classLikeName, int $occursAtLine): self
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::STATIC_METHOD
$this->createContext($occursAtLine, DependencyType::STATIC_METHOD),
);

return $this;
Expand All @@ -194,8 +185,7 @@ public function catchStmt(string $classLikeName, int $occursAtLine): self
{
$this->dependencies[] = new DependencyToken(
ClassLikeToken::fromFQCN($classLikeName),
new FileOccurrence($this->filepath, $occursAtLine),
DependencyType::CATCH
$this->createContext($occursAtLine, DependencyType::CATCH),
);

return $this;
Expand Down
17 changes: 5 additions & 12 deletions src/Core/Dependency/Dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Qossmic\Deptrac\Core\Dependency;

use Qossmic\Deptrac\Contract\Ast\DependencyType;
use Qossmic\Deptrac\Contract\Ast\FileOccurrence;
use Qossmic\Deptrac\Contract\Ast\DependencyContext;
use Qossmic\Deptrac\Contract\Ast\TokenInterface;
use Qossmic\Deptrac\Contract\Dependency\DependencyInterface;

Expand All @@ -14,15 +13,14 @@ class Dependency implements DependencyInterface
public function __construct(
private readonly TokenInterface $depender,
private readonly TokenInterface $dependent,
private readonly FileOccurrence $fileOccurrence,
private readonly DependencyType $dependencyType
private readonly DependencyContext $context,
) {}

public function serialize(): array
{
return [[
'name' => $this->dependent->toString(),
'line' => $this->fileOccurrence->line,
'line' => $this->context->fileOccurrence->line,
]];
}

Expand All @@ -36,13 +34,8 @@ public function getDependent(): TokenInterface
return $this->dependent;
}

public function getFileOccurrence(): FileOccurrence
public function getContext(): DependencyContext
{
return $this->fileOccurrence;
}

public function getType(): DependencyType
{
return $this->dependencyType;
return $this->context;
}
}
11 changes: 5 additions & 6 deletions src/Core/Dependency/Emitter/ClassDependencyEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Qossmic\Deptrac\Core\Dependency\Emitter;

use Qossmic\Deptrac\Contract\Ast\DependencyContext;
use Qossmic\Deptrac\Contract\Ast\DependencyType;
use Qossmic\Deptrac\Core\Ast\AstMap\AstMap;
use Qossmic\Deptrac\Core\Dependency\Dependency;
Expand All @@ -22,19 +23,18 @@ public function applyDependencies(AstMap $astMap, DependencyList $dependencyList
$classLikeName = $classReference->getToken();

foreach ($classReference->dependencies as $dependency) {
if (DependencyType::SUPERGLOBAL_VARIABLE === $dependency->type) {
if (DependencyType::SUPERGLOBAL_VARIABLE === $dependency->context->dependencyType) {
continue;
}
if (DependencyType::UNRESOLVED_FUNCTION_CALL === $dependency->type) {
if (DependencyType::UNRESOLVED_FUNCTION_CALL === $dependency->context->dependencyType) {
continue;
}

$dependencyList->addDependency(
new Dependency(
$classLikeName,
$dependency->token,
$dependency->fileOccurrence,
$dependency->type
$dependency->context,
)
);
}
Expand All @@ -44,8 +44,7 @@ public function applyDependencies(AstMap $astMap, DependencyList $dependencyList
new Dependency(
$classLikeName,
$inherit->classLikeName,
$inherit->fileOccurrence,
DependencyType::INHERIT
new DependencyContext($inherit->fileOccurrence, DependencyType::INHERIT),
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ public function applyDependencies(AstMap $astMap, DependencyList $dependencyList
{
foreach ($astMap->getClassLikeReferences() as $classReference) {
foreach ($classReference->dependencies as $dependency) {
if (DependencyType::SUPERGLOBAL_VARIABLE !== $dependency->type) {
if (DependencyType::SUPERGLOBAL_VARIABLE !== $dependency->context->dependencyType) {
continue;
}
$dependencyList->addDependency(
new Dependency(
$classReference->getToken(),
$dependency->token,
$dependency->fileOccurrence,
$dependency->type
$dependency->context,
)
);
}
Expand Down
7 changes: 3 additions & 4 deletions src/Core/Dependency/Emitter/FileDependencyEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ public function applyDependencies(AstMap $astMap, DependencyList $dependencyList
{
foreach ($astMap->getFileReferences() as $fileReference) {
foreach ($fileReference->dependencies as $dependency) {
if (DependencyType::USE === $dependency->type) {
if (DependencyType::USE === $dependency->context->dependencyType) {
continue;
}

if (DependencyType::UNRESOLVED_FUNCTION_CALL === $dependency->type) {
if (DependencyType::UNRESOLVED_FUNCTION_CALL === $dependency->context->dependencyType) {
continue;
}

$dependencyList->addDependency(
new Dependency(
$fileReference->getToken(),
$dependency->token,
$dependency->fileOccurrence,
$dependency->type
$dependency->context,
)
);
}
Expand Down
Loading

0 comments on commit bfb213d

Please sign in to comment.