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

Added package name collector #1205

Closed

Conversation

ariddlestone
Copy link
Contributor

Closes #1190

Copy link
Collaborator

@patrickkusebauch patrickkusebauch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather see extending the FileReferenceVisitor to add a property to ClassLikeReference and you then checking against that property, rather than trying to re-parse the file again in the collector.

*/
private function getCommentDoc(ClassLikeReference $reference): string
{
$node = $this->nikicPhpParser->getNodeForClassLikeReference($reference);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very expensive - it forces every class-like to be loaded and parsed at least twice during the program runtime.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also forces the second parsing to be again stored in memory.


public function satisfy(array $config, TokenReferenceInterface $reference): bool
{
if (!$reference instanceof ClassLikeReference) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Limiting this only to class-likes seems half done looking at the docs for that annotation: https://docs.phpdoc.org/3.0/guide/references/phpdoc/tags/package.html#description

public function __construct(
private readonly NikicPhpParser $nikicPhpParser
) {
$this->lexer = new Lexer();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the concept of mixing Ast Parsing (done in AST module) and collectors (done in Layer module). They are separate concerns and should be done in their respective places.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. The collector should not be aware of the parser. I think this should be an extractor instead, which can then emit a reference. I am not sure, if it should be a class reference or maybe something else/new.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment the TokenReferenceInterface only handles the Token and its filepath - could we add a more generic array of meta information, which we can then search later in collectors?

For example, if there was a new TokenReferenceMetaInterface, with an array of those in each TokenReferenceInterface, we could store a new PackageAnnotationMeta object in that array when we find the @package annotation, and then search for it in collectors.

This would prevent the TokenReferenceInterface gradually having more fields as more meta data is attached to it (if more collectors come along in the future).

The extractor you suggest could then add a TokenReferenceMeta to the TokenReference.

Sorry - I don't think I've explained this very well - I'll see if I can start to build it so you can see what I mean.

);
}

private function getPackageDocBlock(array $packageNames): Doc
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the test be done on some fixture class file instead of this synthetic doc-block? Those test then can serve as a documentation reference as well to show case how it should be used as well.

@ariddlestone ariddlestone force-pushed the feature/package-name-collector branch from ca76e7a to 20772c6 Compare June 10, 2023 01:27
@ariddlestone ariddlestone force-pushed the feature/package-name-collector branch from bfe5c46 to 80800bc Compare July 12, 2023 15:36
@ariddlestone
Copy link
Contributor Author

I've made a number of changes:

  • Added ReferenceBuilder::$metaData so that we can store arbitrary details there and inject them into the references created
  • Created a PackageName class, which is added to ReferenceBuilder::$metaData in FileReferenceVisitor - I can't use an extractor as the $referenceBuilder is a FileReferenceBuilder by the time the extractors are called for class-like nodes
  • Modified PackageNameCollector to look for PackageNames in TokenReferenceInterface::getMetaData() to determine package names
  • Modified parser tests to use a fixture, and added collector test

Some other notes:

I can't make use of file-level @package annotations, as nikic/php-parser doesn't support them. For now I'm therefore only handling class-like and function docblocks.

The recent work on pulling the docblock parsing into a separate method in FileReferenceVisitor::processClassLikeDocs() was nice. I wonder if it might be worth later looking at separating the various data-gathering processes there into their own registered classes, like extractors are currently.

@ariddlestone
Copy link
Contributor Author

ariddlestone commented Jul 12, 2023

Just re-read https://docs.phpdoc.org/3.0/guide/references/phpdoc/tags/package.html#description and thinking I should remove package name handling from global functions. They're only mentioned if the annotation is at the file-level docblock (which we can't handle).

@gennadigennadigennadi
Copy link
Member

Please update the git origin for development to https://github.com/qossmic/deptrac-src and reopen the PR.
Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add package annotation Collector
4 participants