Skip to content

Trait parsing with context#23

Closed
ahungry wants to merge 3 commits intophpstan:masterfrom
ahungry:TraitParsingWithContext
Closed

Trait parsing with context#23
ahungry wants to merge 3 commits intophpstan:masterfrom
ahungry:TraitParsingWithContext

Conversation

@ahungry
Copy link

@ahungry ahungry commented Dec 6, 2016

This fixes the issues in #19 that I was complaining about, by parsing over every possible context of the trait use given a code base / file listing.

Please let me know what you think.

@ondrejmirtes
Copy link
Member

I was already thinking how would I approach this and I have a different way in mind - one that would not require modifying all of related rules because that is easily breakable by forgetting about it 😊

This approach consists of:

  • Skipping analysis of traits themselves (by preventing calling $nodeCallback in NodeScopeResolver)
  • When the node traversing stumbles upon a TraitUse, switch the $scope->file to the trait file (but not $scope->class!)
  • Start analyzing the node tree of the trait body in the context of the using class

This also avoid potentially expensive operations like get_declared_classes from your PR.

@ondrejmirtes
Copy link
Member

Also, I realized that get_declared_classes does not work because not all analyzed classes might be autoloaded when you start analyzing a trait - autoloading is lazy in its nature.

@ahungry
Copy link
Author

ahungry commented Dec 6, 2016

Good catch - I had actually modified the main Analyser.php class locally as well to run a require_once across each element in the $files array (around line 121), to preload all the classes, and that worked well - of course, I forgot to include in the PR (doh) - that should teach me for working directly out of the vendor/ directory in a project using this and copying files across to the git checkout 😄 .

I do like your suggestion better, this one was definitely a bit clunky (I don't like having the 2 diverging paths in each rule file).

@ondrejmirtes
Copy link
Member

Can you try out current master? I implemented the idea here: e59d160 (but use current dev-master in composer because later commits also affect this behaviour).

If you're OK with the results I will release 0.5.

@ahungry
Copy link
Author

ahungry commented Dec 12, 2016

I'll try today and get back to you.

@ahungry
Copy link
Author

ahungry commented Dec 12, 2016

This is great, it works perfect on both the small test environment and on the real code base that I initially found the issue in.

Thanks!

@ondrejmirtes
Copy link
Member

Cool, glad to hear that. I've bumped into an issue and will have to fix it before releasing 0.5. I will describe it once I fix it, it will be quite obvious from the code and tests. (Until then, don't be nervous about undefined variables errors in trait methods when the parameter names do not match between the trait and the class.)

@igads igads mentioned this pull request Aug 27, 2021
@evandrojr evandrojr mentioned this pull request Jul 10, 2023
@wehostadm wehostadm mentioned this pull request Nov 17, 2023
@johntheteam johntheteam mentioned this pull request Aug 17, 2024
@klewi1983 klewi1983 mentioned this pull request May 13, 2025
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.

2 participants