Skip to content

Commit

Permalink
Added TokenReference meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
ariddlestone committed Jun 11, 2023
1 parent 20772c6 commit 204a454
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Contract/Ast/TokenReferenceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ interface TokenReferenceInterface
public function getFilepath(): ?string;

public function getToken(): TokenInterface;

/**
* @return TokenReferenceMetaInterface[]
*/
public function getMetaData(): array;
}
7 changes: 7 additions & 0 deletions src/Contract/Ast/TokenReferenceMetaInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Qossmic\Deptrac\Contract\Ast;

interface TokenReferenceMetaInterface
{
}
9 changes: 9 additions & 0 deletions src/Core/Ast/AstMap/ClassLike/ClassLikeReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Qossmic\Deptrac\Core\Ast\AstMap\ClassLike;

use Qossmic\Deptrac\Contract\Ast\TokenReferenceInterface;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceMetaInterface;
use Qossmic\Deptrac\Core\Ast\AstMap\AstInherit;
use Qossmic\Deptrac\Core\Ast\AstMap\DependencyToken;
use Qossmic\Deptrac\Core\Ast\AstMap\File\FileReference;
Expand All @@ -18,12 +19,14 @@ class ClassLikeReference implements TokenReferenceInterface

/**
* @param AstInherit[] $inherits
* @param TokenReferenceMetaInterface[] $metaData
* @param DependencyToken[] $dependencies
*/
public function __construct(
private readonly ClassLikeToken $classLikeName,
ClassLikeType $classLikeType = null,
public readonly array $inherits = [],
public readonly array $metaData = [],
public readonly array $dependencies = [],
public readonly bool $isInternal = false,
private readonly ?FileReference $fileReference = null
Expand All @@ -37,6 +40,7 @@ public function withFileReference(FileReference $astFileReference): self
$this->classLikeName,
$this->type,
$this->inherits,
$this->metaData,
$this->dependencies,
$this->isInternal,
$astFileReference
Expand All @@ -52,4 +56,9 @@ public function getToken(): ClassLikeToken
{
return $this->classLikeName;
}

public function getMetaData(): array
{
return $this->metaData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function build(): ClassLikeReference
$this->classLikeToken,
$this->classLikeType,
$this->inherits,
$this->metaData,
$this->dependencies,
$this->isInternal
);
Expand Down
8 changes: 8 additions & 0 deletions src/Core/Ast/AstMap/File/FileReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Qossmic\Deptrac\Contract\Ast\TokenInterface;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceInterface;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceMetaInterface;
use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeReference;
use Qossmic\Deptrac\Core\Ast\AstMap\DependencyToken;
use Qossmic\Deptrac\Core\Ast\AstMap\Function\FunctionReference;
Expand All @@ -24,12 +25,14 @@ class FileReference implements TokenReferenceInterface
/**
* @param ClassLikeReference[] $classLikeReferences
* @param FunctionReference[] $functionReferences
* @param TokenReferenceMetaInterface[] $metaData
* @param DependencyToken[] $dependencies
*/
public function __construct(
public readonly string $filepath,
array $classLikeReferences,
array $functionReferences,
public readonly array $metaData,
public readonly array $dependencies
) {
/** @psalm-suppress ImpureFunctionCall */
Expand All @@ -53,4 +56,9 @@ public function getToken(): TokenInterface
{
return new FileToken($this->filepath);
}

public function getMetaData(): array
{
return $this->metaData;
}
}
8 changes: 7 additions & 1 deletion src/Core/Ast/AstMap/File/FileReferenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public function build(): FileReference
$functionReferences[] = $functionReference->build();
}

return new FileReference($this->filepath, $classReferences, $functionReferences, $this->dependencies);
return new FileReference(
$this->filepath,
$classReferences,
$functionReferences,
$this->metaData,
$this->dependencies
);
}
}
9 changes: 9 additions & 0 deletions src/Core/Ast/AstMap/Function/FunctionReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Qossmic\Deptrac\Contract\Ast\TokenInterface;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceInterface;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceMetaInterface;
use Qossmic\Deptrac\Core\Ast\AstMap\DependencyToken;
use Qossmic\Deptrac\Core\Ast\AstMap\File\FileReference;

Expand All @@ -15,10 +16,12 @@
class FunctionReference implements TokenReferenceInterface
{
/**
* @param TokenReferenceMetaInterface[] $metaData
* @param DependencyToken[] $dependencies
*/
public function __construct(
private readonly FunctionToken $functionName,
public readonly array $metaData = [],
public readonly array $dependencies = [],
private readonly ?FileReference $fileReference = null
) {}
Expand All @@ -27,6 +30,7 @@ public function withFileReference(FileReference $astFileReference): self
{
return new self(
$this->functionName,
$this->metaData,
$this->dependencies,
$astFileReference
);
Expand All @@ -41,4 +45,9 @@ public function getToken(): TokenInterface
{
return $this->functionName;
}

public function getMetaData(): array
{
return $this->metaData;
}
}
1 change: 1 addition & 0 deletions src/Core/Ast/AstMap/Function/FunctionReferenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function build(): FunctionReference
{
return new FunctionReference(
FunctionToken::fromFQCN($this->functionName),
$this->metaData,
$this->dependencies
);
}
Expand Down
11 changes: 11 additions & 0 deletions src/Core/Ast/AstMap/ReferenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

use Qossmic\Deptrac\Contract\Ast\DependencyType;
use Qossmic\Deptrac\Contract\Ast\FileOccurrence;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceMetaInterface;
use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeToken;
use Qossmic\Deptrac\Core\Ast\AstMap\Function\FunctionToken;
use Qossmic\Deptrac\Core\Ast\AstMap\Variable\SuperGlobalToken;

abstract class ReferenceBuilder
{
/**
* @var TokenReferenceMetaInterface[]
*/
protected array $metaData = [];

/** @var DependencyToken[] */
protected array $dependencies = [];

Expand All @@ -28,6 +34,11 @@ final public function getTokenTemplates(): array
return $this->tokenTemplates;
}

public function addMetaData(TokenReferenceMetaInterface $metaDataItem): void
{
$this->metaData[] = $metaDataItem;
}

/**
* Unqualified function and constant names inside a namespace cannot be
* statically resolved. Inside a namespace Foo, a call to strlen() may
Expand Down
14 changes: 13 additions & 1 deletion src/Core/Ast/AstMap/Variable/VariableReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@

use Qossmic\Deptrac\Contract\Ast\TokenInterface;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceInterface;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceMetaInterface;

/**
* @psalm-immutable
*/
class VariableReference implements TokenReferenceInterface
{
public function __construct(private readonly SuperGlobalToken $tokenName) {}
/**
* @param TokenReferenceMetaInterface[] $metaData
*/
public function __construct(
private readonly SuperGlobalToken $tokenName,
private readonly array $metaData = []
) {}

public function getFilepath(): ?string
{
Expand All @@ -23,4 +30,9 @@ public function getToken(): TokenInterface
{
return $this->tokenName;
}

public function getMetaData(): array
{
return $this->metaData;
}
}
2 changes: 1 addition & 1 deletion src/Core/Dependency/TokenResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function resolve(TokenInterface $token, AstMap $astMap): TokenReferenceIn
$token instanceof ClassLikeToken => $astMap->getClassReferenceForToken($token) ?? new ClassLikeReference($token),
$token instanceof FunctionToken => $astMap->getFunctionReferenceForToken($token) ?? new FunctionReference($token),
$token instanceof SuperGlobalToken => new VariableReference($token),
$token instanceof FileToken => $astMap->getFileReferenceForToken($token) ?? new FileReference($token->path, [], [], []),
$token instanceof FileToken => $astMap->getFileReferenceForToken($token) ?? new FileReference($token->path, [], [], [], []),
default => throw UnrecognizedTokenException::cannotCreateReference($token)
};
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Core/Dependency/TokenResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function testResolvesClassLikeFromAstMap(): void
$classReference,
],
[],
[],
[]
);
$astMap = new AstMap([$fileReference]);
Expand Down Expand Up @@ -79,6 +80,7 @@ public function testResolvesFunctionFromAstMap(): void
[
$functionReference,
],
[],
[]
);
$astMap = new AstMap([$fileReference]);
Expand Down Expand Up @@ -117,6 +119,7 @@ public function testResolvesFileFromAstMap(): void
'path/to/file.php',
[],
[],
[],
[]
);
$astMap = new AstMap([$fileReference]);
Expand Down

0 comments on commit 204a454

Please sign in to comment.