plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

PHP variables inside echo string are formatted to the new line 😢

Open kumardeepakme opened this issue 4 years ago • 7 comments

@prettier/plugin-php v0.17.2 Playground link

Input:

<?php

function test($var1, $var2) {
  echo '<div class="'.$var1.'"></div>
  <div class="'.$var2.'"></div>';
}

Output:

<?php

function test($var1, $var2) {
  echo '<div class="' .
    $var1 .
    '"></div>
  <div class="' .
    $var2 .
    '"></div>';
}

Expected Output:

Inline variables should not jump to the new line. Can I control this behavior? Also, valid spacing should be there between quotes and concatenation operators as below.

<?php

function test($var1, $var2) {
  echo '<div class="' . $var1 . '"></div>
  <div class="' . $var2 . '"></div>';
}

kumardeepakme avatar Jul 28 '21 06:07 kumardeepakme

We can’t analyze content inside string

alexander-akait avatar Jul 28 '21 10:07 alexander-akait

@alexander-akait and what does this exactly mean? 🤔

kumardeepakme avatar Jul 28 '21 10:07 kumardeepakme

We don’t know how better keep variables on lines based on html content in string, we don’t know that it is HTML

alexander-akait avatar Jul 28 '21 10:07 alexander-akait

@alexander-akait okay, is there any workaround for this issue? It makes the code very ugly (rather pretty) and unreadable.

kumardeepakme avatar Jul 28 '21 10:07 kumardeepakme

No workarounds, if you concatenate string without html it will be look good, try to split variable on two variables with one var

alexander-akait avatar Jul 28 '21 10:07 alexander-akait

Workaround is // prettier-ignore above the line you want left alone.

MasonD avatar Jun 15 '22 23:06 MasonD

@alexander-akait this is a string concatenation, is it possible to add a flag to not wrap the concat? this consumes a line break that is inside a string, and thinks that it is a "code" line break

echo '<tr>
  <td colspan="2"><div class="' . $msg_type . '">' . $message . '</div></td>
</tr>';

Becomes

echo '<tr>
<td colspan="2"><div class="' .
   		$msg_type .
   		'">' .
   		$message .
   		'</div></td></tr>';

But this doesn't changes

echo '<tr><td colspan="2"><div class="' . $msg_type . '">' . $message . '</div></td></tr>';

which means that the line break (which is inside a string) is considered as "code" line break (which is not).

felixmosh avatar Oct 23 '23 11:10 felixmosh