-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
How distinct method with and without public visibility ? #136
Comments
That is caused by PhpParser\Parser, which defaults to MODIFIER_PUBLIC when no visibility modifier is given. phpparser/grammar/zend_language_parser.php line 481:
ISTM that it is not possible to distinguish an implicit from an explicit public modifier as it is now. I'll try to come up with a patch; however, I won't be able to make a PR, due to no experience with Git (still stuck with SVN). |
Yes, the parser does not distinguish between explicit and implicit visibility, because both are equivalent as far as PHP is concerned. It's a formatting difference and the syntax tree does not retain formatting. The only way to distinguish this currently is by inspecting the tokens. Using the token offset lexer: <?php
function isImplicitlyPublicProperty(array $tokens, Stmt\Property $prop) {
$i = $prop->getAttribute('startOffset');
return isset($tokens[$i]) && $tokens[$i][0] === T_VAR;
}
function isImplicitlyPublicFunction(array $tokens, Stmt\ClassMethod $method) {
$i = $method->getAttribute('startOffset');
for ($c = count($tokens); $i < $c; ++$i) {
$t = $tokens[$i];
if ($t[0] === T_PUBLIC || $t[0] === T_PROTECTED || $t[0] === T_PRIVATE) {
return false;
}
if ($t[0] === T_FUNCTION) {
break;
}
}
return true;
} If you like, I can add support for getting this information directly to the parser, but that would be a temporary solution. |
@niki I beg your pardon, but I don't see how to use / implement it in real condition. Could you explain me how you'll connect these two functions ? |
I have set up a Gist to show how isImplicitlyPublicProperty() can be used to distinguish implicit and explicit public properties. However, this uses a terrible hack to get the token list. @nikic: Is there any sane way to get the token list from the lexer or parser? Maybe adding LexerWithTokenOffsets::getTokens()? |
@cmb69 Yep, that pretty much matches what I had in mind. And yes, getting the tokens using a |
As I'm currently implemented the solution, I'll noticed that there is an error in So replace line |
@llaville Yeah, that was a copy&paste error. I've fixed the code in the comment. |
@nikic I was pretty glad of the solution after first unit test. But, there is a but, I can't solve, and I need your point of view. To use the Now, the BUT. If you don't parsed again your code, and used a cached version of the AST, like I did in php-reflect ( See lines 115 to 126 ), it does not work anymore. Any idea to solve a "cache" problem implementation ? |
If you want to see result of my implementation. Have a look on the TokenOffsets branch. See changes at commit llaville/php-reflect@4daccd5 Be carefull, this implementation run fine only if you don't add the cache plugin. (In my previous comment lines 115 to 126). |
@llaville Doesn't that just mean that you need to cache both the tokens and the AST? |
No. Unfortunately, I was not able to install the dev version of PHP_Reflect due to a dependency mismatch with PHP_CompatInfo reported by Composer.
That's most likely due to the incompatibility of token_get_all() and Lexer\Emulative. Can't you add another field "tokens" to the event object (besides "ast") on the first run, and retrieve and use this information in subsequent runs? Anyway, these details might better be discussed in the respective PHP_Reflect issue. |
Hello,
I've a problem [1] on my project php-compatinfo that used php-reflect that itself used PHP-Parser 1.0 (stable)
My question is : how can you distinct parsing nodes from code 1 to code 2
code 1
code 2
Both gave me the same nodes dump
The
PhpParser\Node\Stmt\ClassMethod::isPublic
return me always TRUE with code1 or code2 (public visibility declared or not)Thanks in advance for answer.
Laurent
[1] llaville/php-compatinfo#129
The text was updated successfully, but these errors were encountered: