slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.37k stars 170 forks source link

False positive and false fix for `DisallowYodaComparison` via `<?=` echo short tag #1658

Open herndlm opened 5 months ago

herndlm commented 5 months ago

The following PHP HTML template code causes a false positive via DisallowYodaComparison:

<input
    type="radio"
    <?= $disabledInputs ? 'disabled' : '' ?>
    <?= $policy === Policy::PUBLIC->value ? 'checked' : '' ?>
    value="<?= esc_attr(Policy::PUBLIC->value) ?>">

image

even more interesting though maybe, if I try to auto-fix this I end up with the following which is syntactically very different:

<input
    type="radio"
    <?= $disabledInputs ? 'disabled' : Policy::PUBLIC->value === '' ?>
    <?= $policy ? 'checked' : '' ?>
    value="<?= esc_attr(Policy::PUBLIC->value) ?>">

it looks like the 2 independent ternaries are somehow influencing each other. adding a semicolon after the first ternary or not using the <?= short tag but use echo() instead seems to work around it.

kukulich commented 4 months ago

Our sniffs are not tested for PHP templates. I'm ok to merge PR that will fix it but we will not try to fix it ourself.