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
CleanCode\UndefinedVariable thrown unnecessarily with preg_match_all() #672
Comments
Thanks. It will probably need PDepend adjustments to detect passing by reference and interpret it as variable assignment instead of variable access. |
It's strange that the second example doesn't flag it though if that's the issue? |
Most of the PHPMD rules assert that you follow the best practice: 1 file for 1 symbol (class or function). So rules start to list symbols and search into them. Rules like As the project initially based its rules on PMD it even assumed that project uses OOP, this could be improved to support procedural-style files but it's not on our top-priorities list. |
the second example could be valid like so:
So phpmd doesn't know the context of your example. |
Similar case with str_replace's &$count parameter: $content = str_replace('__SALT__', $newKey, $content, $count);
if ($count === 0) {
return;
} |
We are tracking this in pdepend/pdepend#444 |
This should be fixed by #784 as for You can yet test it requiring |
@kylekatarnls Confirmed that fixes my original problematic code. |
Thanks for confirmation, normally this should be released in 2.9.0. |
…ence Fix #672 Handle passing-by-reference in native PHP functions
The problem still exists for me with dev-master as soon as I'm within a namespace, e.g. the following still gives me the same warnings: <?php
namespace X;
function test()
{
preg_match_all('/./', 'Text', $matches);
foreach ($matches[0] as $match) {
echo $match . PHP_EOL;
}
}
|
Indeed the FQN wrongly resolves to |
Fix proposed. |
Same here for |
…function-without-namespace-check Fix #672 Try global function if namespaced one is not found
Current Behavior
UndefinedVariable error thrown unnecessarily when variable is guaranteed to be defined by
preg_match_all()
Expected Behavior
UndefinedVariable error not thrown for third argument to
preg_match_all()
when used.Steps To Reproduce:
The following code triggers the following warnings:
However, PHP won't throw warnings in this case, so the error is spurious.
There's also an inconsistency as the same code when not wrapped in a function does not throw the warnings (as I'd expect), e.g. the following code (correctly IMHO) does not generate an UndefinedVariable warning from PHPMD:
The text was updated successfully, but these errors were encountered: