imposter icon indicating copy to clipboard operation
imposter copied to clipboard

fix: resolve namespace section and traits correctly

Open matzeeable opened this issue 5 years ago • 0 comments

Hi!

I have added a test case and implementation to support PHP traits as requested here #111 and #149.

Instead of backwards-referencing in a Regexp to check if the use is inside a class, I go the following way:

  • Use T-Regex package to obtain the offset of a found use statement
  • When found, get the content before the match
  • Get all previous class { and namespace { definitions (yeah, multiple namespace { definitions are allowed in PHP)
  • Get the last found match and check if it is a class defintion
  • If yes, skip the trait transformation

Backwards-referencing in a Regexp would be much more "nice", but unfortunately PHP does not support it (I used it in JavaScript before).

RegExp is not the recommend way to do such transformations!! This is a simple workaround which works but you should really consider building your tool on top of an AST parser like php-parser.

matzeeable avatar Apr 08 '20 11:04 matzeeable