Skip to content

Property validUndefinedVariableNames is ignored inside else statement #238

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
udovicic opened this issue Jul 6, 2021 · 1 comment · Fixed by #239
Closed

Property validUndefinedVariableNames is ignored inside else statement #238

udovicic opened this issue Jul 6, 2021 · 1 comment · Fixed by #239
Labels

Comments

@udovicic
Copy link

udovicic commented Jul 6, 2021

Consider the following code to be part of some template, where variable $block originates from somewhere else. Its defined somewhere else, before include was called so it indeed is available in the template:

<?php

$block->doSomething();

if (3  < 5) {
    $block->doThis();
} else {
    $block-doThat();
}

If you format ruelset to look like following:

    <rule ref="VariableAnalysis">
        <properties>
            <property name="validUndefinedVariableNames" value="block"/>
        </properties>
    </rule>

the output would be:

php,phtml test.phtml

FILE: /project/test.phtml
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
 8 | WARNING | Variable $block is undefined.
----------------------------------------------------------------------

Time: 85ms; Memory: 8MB

Which is not the desired behavior.
Else statement requires special attention, but current implementation leads to false positives.

@sirbrillig sirbrillig added the bug label Jul 6, 2021
@sirbrillig
Copy link
Owner

sirbrillig commented Jul 6, 2021

Weird! I've reproduced in the test cases. Thanks for discovering this. I'll see what I can do.

In the meantime, you might find allowUndefinedVariablesInFileScope helpful for templates. It won't solve the problem if there are functions in there, and it means you won't get any warnings about undefined variables in the template, but it could be a compromise while this bug is being fixed.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants