rectorphp / rector

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
https://getrector.com
MIT License
8.61k stars 680 forks source link

Incorrect behavior of RenameForeachValueVariableToMatchExprVariableRector #8796

Closed frankii91 closed 1 week ago

frankii91 commented 3 weeks ago

Bug Report

error with more complex code...

Subject Details
Rector version now()

Minimal PHP Code Causing Issue

code before using rector:

    if (isset($parameter['smarty_internal_index'])) {
        $output = "<?php \$_smarty_tpl->createLocalArrayVariable($_attr[var], {$_nocache}, {$_scope});\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value$parameter[smarty_internal_index] = $_attr[value];";
    } elseif ($compiler->template->smarty instanceof SmartyBC) {
        // implement Smarty2's behaviour of variables assigned by reference
        $output = "<?php if (isset(\$_smarty_tpl->tpl_vars[$_attr[var]])) {\$_smarty_tpl->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
        $output .= "\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value = $_attr[value]; \$_smarty_tpl->tpl_vars[$_attr[var]]->nocache = $_nocache; \$_smarty_tpl->tpl_vars[$_attr[var]]->scope = $_scope;";
        $output .= "\n} else \$_smarty_tpl->tpl_vars[$_attr[var]] = new SmartyVariable($_attr[value], $_nocache, $_scope);";
    } else {
        $output = "<?php \$_smarty_tpl->tpl_vars[$_attr[var]] = new SmartyVariable($_attr[value], $_nocache, $_scope);";
    }

code with syntax errors after changing: if (isset($parameter['smarty_internal_index'])) { $output = "<?php \$_smarty_tpl->createLocalArrayVariable($_attr[var], {$_nocache}, {$_scope});\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value$parameter[smarty_internal_index] = $_attr[value];"; } elseif ($compiler->template->smarty instanceof SmartyBC) { // implement Smarty2's behaviour of variables assigned by reference $output = sprintf('<?php if (isset($_smarty_tpl->tpl_vars[%s])) {$_smarty_tpl->tpl_vars[%s] = clone $_smarty_tpl->tpl_vars[%s];', $_attr[var], $_attr[var], $_attr[var]); $output .= "\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value = $_attr[value]; \$_smarty_tpl->tpl_vars[$_attr[var]]->nocache = {$_nocache}; \$_smarty_tpl->tpl_vars[$_attr[var]]->scope = {$_scope};"; $output .= "\n} else \$_smarty_tpl->tpl_vars[$_attr[var]] = new SmartyVariable($_attr[value], {$_nocache}, {$_scope});"; } else { $output = sprintf('<?php $_smarty_tpl->tpl_vars[%s] = new SmartyVariable(%s, %s, %s);', $_attr[var], $_attr[value], $_nocache, $_scope); }

samsonasik commented 3 weeks ago

Could you reproduce at https://getrector.com/demo ? Thank you

samsonasik commented 1 week ago

I am closing it as no code and config provided. If I check your example, it seems you're changing smarty, that third party vendor, my suggestion is to not change vendor or third party code.

You can skip the path of it https://getrector.com/documentation/ignoring-rules-or-paths

frankii91 commented 1 week ago

t4o że smarty to nie znaczy że w pełni zewnętrzne odłamane w około 2015 i optymalizowane na własną rękę ...

pt., 30 sie 2024 o 20:42 Abdul Malik Ikhsan @.***> napisał(a):

I am closing it as no code and config provided. If I check your example, it seems you're changing smarty, that third party vendor, my suggestion is to not change vendor or third party code.

You can skip the path of it https://getrector.com/documentation/ignoring-rules-or-paths

— Reply to this email directly, view it on GitHub https://github.com/rectorphp/rector/issues/8796#issuecomment-2322136304, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADLSI3WZQXNYUWM7HBJYK2DZUC4K7AVCNFSM6AAAAABMYQCQUGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRSGEZTMMZQGQ . You are receiving this because you authored the thread.Message ID: @.***>