imposter
imposter copied to clipboard
fix: resolve namespace section and traits correctly
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
usestatement - When found, get the content before the match
- Get all previous
class {andnamespace {definitions (yeah, multiplenamespace {definitions are allowed in PHP) - Get the last found match and check if it is a
classdefintion - 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.