Skip to content

Commit

Permalink
- Move ParserInterface.php into the Contract.
Browse files Browse the repository at this point in the history
- Move AstMap related data classes into the contract.

This is in preparation to allow custom implementations of the AstParser for Deptrac purposes. This is useful for 2 reasons:
- It will allow closing qossmic/deptrac#1402 by recommending a custom version of the parser implemented in the userspace with a supported contract to implement by the custom version
- allow for replacing the Nikic parser by PHPStan Parser.

Neither of those are implemented in this commit, but the rest of the required dependencies for the first will be exposed in the next commit.
  • Loading branch information
patrickkusebauch committed Jan 14, 2025
1 parent 5497d3c commit 6341411
Show file tree
Hide file tree
Showing 127 changed files with 390 additions and 427 deletions.
4 changes: 2 additions & 2 deletions baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<code><![CDATA[list<T>]]></code>
</InvalidReturnType>
</file>
<file src="src/Core/Ast/AstMap/ClassLike/ClassLikeToken.php">
<file src="src/Contract/Ast/AstMap/ClassLikeToken.php">
<ArgumentTypeCoercion>
<code>$pattern</code>
</ArgumentTypeCoercion>
</file>
<file src="src/Core/Ast/AstMap/Function/FunctionToken.php">
<file src="src/Contract/Ast/AstMap/FunctionToken.php">
<ArgumentTypeCoercion>
<code>$pattern</code>
</ArgumentTypeCoercion>
Expand Down
2 changes: 1 addition & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\ParserFactory;
use Psr\EventDispatcher\EventDispatcherInterface;
use Qossmic\Deptrac\Contract\Analyser\EventHelper;
use Qossmic\Deptrac\Contract\Ast\ParserInterface;
use Qossmic\Deptrac\Contract\Config\CollectorType;
use Qossmic\Deptrac\Contract\Config\EmitterType;
use Qossmic\Deptrac\Contract\Layer\LayerProvider;
Expand Down Expand Up @@ -38,7 +39,6 @@
use Qossmic\Deptrac\Core\Ast\Parser\Extractors\StaticExtractor;
use Qossmic\Deptrac\Core\Ast\Parser\Extractors\VariableExtractor;
use Qossmic\Deptrac\Core\Ast\Parser\NikicPhpParser\NikicPhpParser;
use Qossmic\Deptrac\Core\Ast\Parser\ParserInterface;
use Qossmic\Deptrac\Core\Ast\Parser\TypeResolver;
use Qossmic\Deptrac\Core\Dependency\DependencyResolver;
use Qossmic\Deptrac\Core\Dependency\Emitter\ClassDependencyEmitter;
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/Analyser/ProcessEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Qossmic\Deptrac\Contract\Analyser;

use Qossmic\Deptrac\Contract\Ast\TokenReferenceInterface;
use Qossmic\Deptrac\Contract\Ast\AstMap\TokenReferenceInterface;
use Qossmic\Deptrac\Contract\Dependency\DependencyInterface;
use Symfony\Contracts\EventDispatcher\Event;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

use Qossmic\Deptrac\Contract\Ast\FileOccurrence;
use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeToken;
use Stringable;

use function array_reverse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

enum AstInheritType: string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap\ClassLike;

use Qossmic\Deptrac\Core\Ast\AstMap\AstInherit;
use Qossmic\Deptrac\Core\Ast\AstMap\DependencyToken;
use Qossmic\Deptrac\Core\Ast\AstMap\File\FileReference;
use Qossmic\Deptrac\Core\Ast\AstMap\TaggedTokenReference;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* @psalm-immutable
*/
class ClassLikeReference extends TaggedTokenReference
final class ClassLikeReference extends TaggedTokenReference
{
public readonly ClassLikeType $type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap\ClassLike;

use Qossmic\Deptrac\Contract\Ast\TokenInterface;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* @psalm-immutable
*/
final class ClassLikeToken implements TokenInterface
{
private function __construct(private readonly string $className) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap\ClassLike;

use Qossmic\Deptrac\Contract\Ast\TokenInterface;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

enum ClassLikeType: string implements TokenInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Contract\Ast;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* @psalm-immutable
*
* Context of the dependency.
*
* Any additional info about where the dependency occurred.
*/
final class DependencyContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap;

use Qossmic\Deptrac\Contract\Ast\DependencyContext;
use Qossmic\Deptrac\Contract\Ast\TokenInterface;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* @psalm-immutable
*/
class DependencyToken
final class DependencyToken
{
public function __construct(
public readonly TokenInterface $token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Contract\Ast;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* Specifies the type of AST dependency.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Contract\Ast;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* @psalm-immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap\File;

use Qossmic\Deptrac\Contract\Ast\TokenInterface;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceInterface;
use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeReference;
use Qossmic\Deptrac\Core\Ast\AstMap\DependencyToken;
use Qossmic\Deptrac\Core\Ast\AstMap\Function\FunctionReference;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* @psalm-immutable
*/
class FileReference implements TokenReferenceInterface
final class FileReference implements TokenReferenceInterface
{
/** @var ClassLikeReference[] */
public readonly array $classLikeReferences;
Expand Down Expand Up @@ -44,7 +38,7 @@ public function __construct(
);
}

public function getFilepath(): ?string
public function getFilepath(): string
{
return $this->filepath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap\File;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

use Qossmic\Deptrac\Contract\Ast\TokenInterface;
use Symfony\Component\Filesystem\Path;

final class FileToken implements TokenInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap\Function;

use Qossmic\Deptrac\Contract\Ast\TokenInterface;
use Qossmic\Deptrac\Core\Ast\AstMap\DependencyToken;
use Qossmic\Deptrac\Core\Ast\AstMap\File\FileReference;
use Qossmic\Deptrac\Core\Ast\AstMap\TaggedTokenReference;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* @psalm-immutable
*/
class FunctionReference extends TaggedTokenReference
final class FunctionReference extends TaggedTokenReference
{
/**
* @param DependencyToken[] $dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap\Function;

use Qossmic\Deptrac\Contract\Ast\TokenInterface;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

final class FunctionToken implements TokenInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap\Variable;

use Qossmic\Deptrac\Contract\Ast\TokenInterface;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

enum SuperGlobalToken: string implements TokenInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap;

use Qossmic\Deptrac\Contract\Ast\TaggedTokenReferenceInterface;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* Helper trait for implementing TaggedTokenReferenceInterface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Contract\Ast;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* Represents the AST-Token, its location, and associated tags.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Contract\Ast;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* Represents an AST-Token, which can be referenced as dependency.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Contract\Ast;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* Represents the AST-Token and its location.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

declare(strict_types=1);

namespace Qossmic\Deptrac\Core\Ast\AstMap\Variable;

use Qossmic\Deptrac\Contract\Ast\TokenInterface;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceInterface;
namespace Qossmic\Deptrac\Contract\Ast\AstMap;

/**
* @psalm-immutable
*/
class VariableReference implements TokenReferenceInterface
final class VariableReference implements TokenReferenceInterface
{
public function __construct(private readonly SuperGlobalToken $tokenName) {}

Expand Down
23 changes: 23 additions & 0 deletions src/Contract/Ast/ParserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Qossmic\Deptrac\Contract\Ast;

use Qossmic\Deptrac\Contract\Ast\AstMap\ClassLikeReference;
use Qossmic\Deptrac\Contract\Ast\AstMap\FileReference;

interface ParserInterface
{
/**
* @throws CouldNotParseFileException
*/
public function parseFile(string $file): FileReference;

/**
* @return list<string> list of method names for a given class-like reference
*
* @throws CouldNotParseFileException
*/
public function getMethodNamesForClassLikeReference(ClassLikeReference $classReference): array;
}
4 changes: 2 additions & 2 deletions src/Contract/Dependency/DependencyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Qossmic\Deptrac\Contract\Dependency;

use Qossmic\Deptrac\Contract\Ast\DependencyContext;
use Qossmic\Deptrac\Contract\Ast\TokenInterface;
use Qossmic\Deptrac\Contract\Ast\AstMap\DependencyContext;
use Qossmic\Deptrac\Contract\Ast\AstMap\TokenInterface;

/**
* Represents a dependency between 2 tokens (depender and dependent).
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/Layer/CollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Qossmic\Deptrac\Contract\Layer;

use Qossmic\Deptrac\Contract\Ast\AstMap\TokenReferenceInterface;
use Qossmic\Deptrac\Contract\Ast\CouldNotParseFileException;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceInterface;

/**
* A collector is responsible to tell whether an AST node (e.g. a specific class) is part of a layer.
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Analyser/EventHandler/DependsOnInternalToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Qossmic\Deptrac\Contract\Analyser\EventHelper;
use Qossmic\Deptrac\Contract\Analyser\ProcessEvent;
use Qossmic\Deptrac\Contract\Analyser\ViolationCreatingInterface;
use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeReference;
use Qossmic\Deptrac\Contract\Ast\AstMap\ClassLikeReference;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use JetBrains\PHPStormStub\PhpStormStubsMap;
use Qossmic\Deptrac\Contract\Analyser\ProcessEvent;
use Qossmic\Deptrac\Contract\Ast\AstMap\ClassLikeToken;
use Qossmic\Deptrac\Contract\Result\Uncovered;
use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeToken;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Analyser/LayerForTokenAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Qossmic\Deptrac\Core\Analyser;

use Qossmic\Deptrac\Contract\Ast\AstMap\TokenReferenceInterface;
use Qossmic\Deptrac\Contract\Ast\CouldNotParseFileException;
use Qossmic\Deptrac\Contract\Ast\TokenReferenceInterface;
use Qossmic\Deptrac\Contract\Layer\InvalidCollectorDefinitionException;
use Qossmic\Deptrac\Contract\Layer\InvalidLayerDefinitionException;
use Qossmic\Deptrac\Core\Ast\AstException;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Ast/AstLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use Qossmic\Deptrac\Contract\Ast\AstFileAnalysedEvent;
use Qossmic\Deptrac\Contract\Ast\AstFileSyntaxErrorEvent;
use Qossmic\Deptrac\Contract\Ast\CouldNotParseFileException;
use Qossmic\Deptrac\Contract\Ast\ParserInterface;
use Qossmic\Deptrac\Contract\Ast\PostCreateAstMapEvent;
use Qossmic\Deptrac\Contract\Ast\PreCreateAstMapEvent;
use Qossmic\Deptrac\Core\Ast\AstMap\AstMap;
use Qossmic\Deptrac\Core\Ast\Parser\ParserInterface;

class AstLoader
{
Expand Down
Loading

0 comments on commit 6341411

Please sign in to comment.