Skip to content

Commit

Permalink
fix - echo with function shouldn't have an space before parenthesis, …
Browse files Browse the repository at this point in the history
…missing space before closing tag - kokororin/vscode-phpfmt#160
  • Loading branch information
driade committed Jan 8, 2025
1 parent a462a58 commit e24b9fa
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
13 changes: 11 additions & 2 deletions fmt.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4829,6 +4829,7 @@ public function format($source) {
$touchedUse = false;
$touchedGroupedUse = false;
$hasEchoAfterOpenTag = false;
$hasOpenTagWithEcho = false;
$attributeStack = [];

while (list($index, $token) = $this->each($this->tkns)) {
Expand Down Expand Up @@ -5148,7 +5149,7 @@ public function format($source) {
if ($this->leftMemoUsefulTokenIs(T_OPEN_TAG)) {
$hasEchoAfterOpenTag = false;
}
$this->appendCode($text . $this->getSpace(!$this->rightTokenIs(ST_SEMI_COLON)));
$this->appendCode($text . $this->getSpace(!$this->rightTokenIs(ST_SEMI_COLON) && !$this->rightTokenIs(ST_PARENTHESES_OPEN)));
break;
case T_RETURN:
case T_YIELD:
Expand Down Expand Up @@ -5379,13 +5380,21 @@ public function format($source) {
break;

case T_CLOSE_TAG:
$this->appendCode($this->getSpace(!$hasEchoAfterOpenTag && !$this->hasLnBefore()));
$space = $this->getSpace(!$hasEchoAfterOpenTag && !$this->hasLnBefore());
if ($space === '') {
if ($this->leftTokenIs([ST_SEMI_COLON]) && !$this->hasLnBefore() && !$hasOpenTagWithEcho) {
$space = $this->getSpace();
}
}
$this->appendCode($space);
$this->appendCode($text);
$hasEchoAfterOpenTag = false;
$hasOpenTagWithEcho = false;
break;

case T_OPEN_TAG_WITH_ECHO:
$hasEchoAfterOpenTag = true;
$hasOpenTagWithEcho = true;
$this->appendCode($text);
break;

Expand Down
10 changes: 5 additions & 5 deletions tests/Original/087-arithmetic-spacing.out
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
//version:5.6.0
$x = (10 - 5 + 8);
echo (5 % 3) . "\n"; // prints 2
echo (5 % -3) . "\n"; // prints 2
echo (-5 % 3) . "\n"; // prints -2
echo (-5 % -3) . "\n"; // prints -2
echo(5 % 3) . "\n"; // prints 2
echo(5 % -3) . "\n"; // prints 2
echo(-5 % 3) . "\n"; // prints -2
echo(-5 % -3) . "\n"; // prints -2

echo (10 ** 2);
echo(10 ** 2);

$b = $a - 10;
$b = $a - $c;
Expand Down
2 changes: 1 addition & 1 deletion tests/Original/332-autosemicolon-switch.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
//passes:AutoSemicolon

echo ($a);
echo($a);

foreach ($a as $v) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//passes:ResizeSpaces
<h1 class="wp-heading-inline"><?php echo esc_html( get_admin_page_title() ); ?></h1>
<option selected="selected" value=""><?php esc_html_e( 'Select attribute', 'woodmart-child' );?></option>
<option selected="selected" value=""><?php echa("hello"); ?></option>
<option selected="selected" value=""><?php echo "hello" ; ?></option>
<option selected="selected" value=""><?php echo("hello"); ?></option>
<option selected="selected" value=""><?php printf("hello"); ?></option>
<option selected="selected" value=""><?=fun("hello");?></option>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//passes:ResizeSpaces
<h1 class="wp-heading-inline"><?php echo esc_html(get_admin_page_title()); ?></h1>
<option selected="selected" value=""><?php esc_html_e('Select attribute', 'woodmart-child'); ?></option>
<option selected="selected" value=""><?php echa("hello"); ?></option>
<option selected="selected" value=""><?php echo "hello"; ?></option>
<option selected="selected" value=""><?php echo("hello"); ?></option>
<option selected="selected" value=""><?php printf("hello"); ?></option>
<option selected="selected" value=""><?=fun("hello");?></option>
2 changes: 1 addition & 1 deletion tests/PSR/047-short-open-tags.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

//skipShortTag
;?>
; ?>
<?php /*test*/;

0 comments on commit e24b9fa

Please sign in to comment.