Skip to content

Commit 6c9dcb1

Browse files
authored
Added support for return having a nl prefix if one was there previously (#11)
* Added support for return having a nl prefix if one was there previously * Added negative test case
1 parent 4e7d804 commit 6c9dcb1

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

src/Printer/ArrayPrinter.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ArrayPrinter extends Standard
2626
*/
2727

2828
public const T_PAREN_OPEN = 40;
29+
2930
/**
3031
* @var int T_PAREN_CLOSE represents the token id for `)`
3132
*/
@@ -133,6 +134,33 @@ protected function pCommaSeparatedMultiline(array $nodes, bool $trailingComma):
133134
return $result;
134135
}
135136

137+
/**
138+
* Render a return statement
139+
*
140+
* @param Stmt\Return_ $node Return statement node
141+
*
142+
* @return string Return followed by the return value
143+
*/
144+
protected function pStmt_Return(Stmt\Return_ $node): string
145+
{
146+
// Get tokens from parser
147+
$tokens = $this->parser->getTokens();
148+
149+
// Get the previous 2 tokens before the current node
150+
$previousTokens = array_splice($tokens, $node->getAttribute('startTokenPos') - 2, 2);
151+
152+
// If the last token was whitespace and the token before that was not whitespace and the
153+
// whitespace token was a double return, then prefix a \n
154+
$prefix = (
155+
count($previousTokens) > 1
156+
&& $previousTokens[1]->id === T_WHITESPACE
157+
&& $previousTokens[0]->id !== T_WHITESPACE
158+
&& $previousTokens[1]->text === PHP_EOL . PHP_EOL
159+
) ? "\n" : '';
160+
161+
return $prefix . 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';';
162+
}
163+
136164
/**
137165
* Render an array expression
138166
*

tests/ArrayFileTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,51 @@ public function testConfigImports()
245245
<?php
246246
247247
use Symfony\Component\HttpFoundation\Response;
248+
248249
return [
249250
'foo' => Response::HTTP_OK,
250251
'bar' => Response::HTTP_I_AM_A_TEAPOT,
251252
];
252253
254+
PHP;
255+
256+
$this->assertEquals(str_replace("\r", '', $expected), $arrayFile->render());
257+
}
258+
259+
public function testConfigImportComplex()
260+
{
261+
$arrayFile = ArrayFile::open(__DIR__ . '/fixtures/array/import-complex.php');
262+
263+
$expected = <<<PHP
264+
<?php
265+
266+
use Symfony\Component\HttpFoundation\Response;
267+
use Example\EnumExample;
268+
269+
return [
270+
'foo' => Response::HTTP_OK,
271+
'bar' => EnumExample::Value->value,
272+
];
273+
274+
PHP;
275+
276+
$this->assertEquals(str_replace("\r", '', $expected), $arrayFile->render());
277+
}
278+
279+
public function testConfigImportCompact()
280+
{
281+
$arrayFile = ArrayFile::open(__DIR__ . '/fixtures/array/import-compact.php');
282+
283+
$expected = <<<PHP
284+
<?php
285+
286+
use Symfony\Component\HttpFoundation\Response;
287+
use Example\EnumExample;
288+
return [
289+
'foo' => Response::HTTP_OK,
290+
'bar' => EnumExample::Value->value,
291+
];
292+
253293
PHP;
254294

255295
$this->assertEquals(str_replace("\r", '', $expected), $arrayFile->render());
@@ -264,6 +304,7 @@ public function testConfigImportsUpdating()
264304
<?php
265305
266306
use Symfony\Component\HttpFoundation\Response;
307+
267308
return [
268309
'foo' => Response::HTTP_CONFLICT,
269310
'bar' => Response::HTTP_I_AM_A_TEAPOT,
@@ -282,6 +323,7 @@ public function testConfigExpression()
282323
<?php
283324
284325
\$bar = nl2br("Hello\\nWorld");
326+
285327
return [
286328
'foo' => \$bar,
287329
];
@@ -792,6 +834,7 @@ public function testIncludeFormatting()
792834
include_once(__DIR__ . '/sample-array-file.php');
793835
require(__DIR__ . '/sample-array-file.php');
794836
require_once(__DIR__ . '/sample-array-file.php');
837+
795838
return [
796839
'foo' => array_merge(include(__DIR__ . '/sample-array-file.php'), [
797840
'bar' => 'foo',
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
use Symfony\Component\HttpFoundation\Response;
3+
use Example\EnumExample;
4+
return [
5+
'foo' => Response::HTTP_OK,
6+
'bar' => EnumExample::Value->value
7+
];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Symfony\Component\HttpFoundation\Response;
4+
5+
use Example\EnumExample;
6+
7+
return [
8+
'foo' => Response::HTTP_OK,
9+
'bar' => EnumExample::Value->value
10+
];

0 commit comments

Comments
 (0)