Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Printer/ArrayPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ArrayPrinter extends Standard
*/

public const T_PAREN_OPEN = 40;

/**
* @var int T_PAREN_CLOSE represents the token id for `)`
*/
Expand Down Expand Up @@ -133,6 +134,33 @@ protected function pCommaSeparatedMultiline(array $nodes, bool $trailingComma):
return $result;
}

/**
* Render a return statement
*
* @param Stmt\Return_ $node Return statement node
*
* @return string Return followed by the return value
*/
protected function pStmt_Return(Stmt\Return_ $node): string
{
// Get tokens from parser
$tokens = $this->parser->getTokens();

// Get the previous 2 tokens before the current node
$previousTokens = array_splice($tokens, $node->getAttribute('startTokenPos') - 2, 2);

// If the last token was whitespace and the token before that was not whitespace and the
// whitespace token was a double return, then prefix a \n
$prefix = (
count($previousTokens) > 1
&& $previousTokens[1]->id === T_WHITESPACE
&& $previousTokens[0]->id !== T_WHITESPACE
&& $previousTokens[1]->text === PHP_EOL . PHP_EOL
) ? "\n" : '';

return $prefix . 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';';
}

/**
* Render an array expression
*
Expand Down
43 changes: 43 additions & 0 deletions tests/ArrayFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,51 @@ public function testConfigImports()
<?php

use Symfony\Component\HttpFoundation\Response;

return [
'foo' => Response::HTTP_OK,
'bar' => Response::HTTP_I_AM_A_TEAPOT,
];

PHP;

$this->assertEquals(str_replace("\r", '', $expected), $arrayFile->render());
}

public function testConfigImportComplex()
{
$arrayFile = ArrayFile::open(__DIR__ . '/fixtures/array/import-complex.php');

$expected = <<<PHP
<?php

use Symfony\Component\HttpFoundation\Response;
use Example\EnumExample;

return [
'foo' => Response::HTTP_OK,
'bar' => EnumExample::Value->value,
];

PHP;

$this->assertEquals(str_replace("\r", '', $expected), $arrayFile->render());
}

public function testConfigImportCompact()
{
$arrayFile = ArrayFile::open(__DIR__ . '/fixtures/array/import-compact.php');

$expected = <<<PHP
<?php

use Symfony\Component\HttpFoundation\Response;
use Example\EnumExample;
return [
'foo' => Response::HTTP_OK,
'bar' => EnumExample::Value->value,
];

PHP;

$this->assertEquals(str_replace("\r", '', $expected), $arrayFile->render());
Expand All @@ -264,6 +304,7 @@ public function testConfigImportsUpdating()
<?php

use Symfony\Component\HttpFoundation\Response;

return [
'foo' => Response::HTTP_CONFLICT,
'bar' => Response::HTTP_I_AM_A_TEAPOT,
Expand All @@ -282,6 +323,7 @@ public function testConfigExpression()
<?php

\$bar = nl2br("Hello\\nWorld");

return [
'foo' => \$bar,
];
Expand Down Expand Up @@ -792,6 +834,7 @@ public function testIncludeFormatting()
include_once(__DIR__ . '/sample-array-file.php');
require(__DIR__ . '/sample-array-file.php');
require_once(__DIR__ . '/sample-array-file.php');

return [
'foo' => array_merge(include(__DIR__ . '/sample-array-file.php'), [
'bar' => 'foo',
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/array/import-compact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
use Symfony\Component\HttpFoundation\Response;
use Example\EnumExample;
return [
'foo' => Response::HTTP_OK,
'bar' => EnumExample::Value->value
];
10 changes: 10 additions & 0 deletions tests/fixtures/array/import-complex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Symfony\Component\HttpFoundation\Response;

use Example\EnumExample;

return [
'foo' => Response::HTTP_OK,
'bar' => EnumExample::Value->value
];
Loading