generator
generator copied to clipboard
Parsing function and method *bodies* for constant declarations
Moved here from README.
:wave: Hi there,
I just implemented parsing nested define expressions in a library that is using php-stubs/generator internally:
-> inpsyde/wp-stubs#7
We are extending the default NodeVisitor class, so my approach was to parse for any nested define expressions inside functions and class methods, and then add them back into the function/method body after it was emptied (by the default/parent enterNode method).
Now, should parsing nested define constants (and maybe also const constants?) be made possible in the generator itself? I am happy to help implement this, if yes.
We would need to define how this would happen. That is, should it happen by default if the StubsGenerator::CONSTANTS symbol is passed? Or should it be turned on explicitly with a dedicated symbol, for example, StubsGenerator::NESTED_CONSTANTS? If the latter, should the StubsGenerator::CONSTANTS symbol have to be defined as well, or does the new StubsGenerator::NESTED_CONSTANTS symbol imply the existing one?
Regarding the implementation, is replacing the function/method body with the define (and const?) expressions what you have in mind?
This would then, of course, be implemented in the current Function_ || ClassMethod case in the enterNode method.
cc @GiacoCorsiglia
Hello @tfrommen!
Thank you for your comment. I raise my voice against using legacy technologies (even in WordPress plugins)
❌ Global constants
https://github.com/szepeviktor/starter-plugin/blob/master/README.md#what-to-avoid
This repo exists as I need a working tool to generate stubs. https://github.com/php-stubs/generator/compare/3e3f6f42dd257b81f2d7e8846cf96f5cf71b7f0b...master
I would like WordPress to have a class autoloader, so it would need no stubs. e.g. https://core.trac.wordpress.org/ticket/60414
If you force me to accept your PR I will merge it :)