diff --git a/src/Parse/PHP/ArrayFile.php b/src/Parse/PHP/ArrayFile.php index ddd78b23d..2de0718c4 100644 --- a/src/Parse/PHP/ArrayFile.php +++ b/src/Parse/PHP/ArrayFile.php @@ -413,12 +413,7 @@ public function constant(string $name): PHPConstant */ public function render(): string { - // Make sure lines with only indentation are trimmed - return preg_replace( - '/^\s+$/m', - '', - $this->printer->prettyPrintFile($this->ast) . "\n" - ); + return $this->printer->prettyPrintFile($this->ast) . "\n"; } /** diff --git a/src/Parse/PHP/ArrayPrinter.php b/src/Parse/PHP/ArrayPrinter.php index 6c0118e8f..3fd87db39 100644 --- a/src/Parse/PHP/ArrayPrinter.php +++ b/src/Parse/PHP/ArrayPrinter.php @@ -1,5 +1,6 @@ indent(); + + $result = ''; + $lastIdx = count($nodes) - 1; + foreach ($nodes as $idx => $node) { + if ($node !== null) { + $comments = $node->getComments(); + if ($comments) { + $result .= $this->pComments($comments); + } + + $result .= $this->nl . $this->p($node); + } else { + $result = trim($result) . "\n"; + } + if ($trailingComma || $idx !== $lastIdx) { + $result .= ','; + } + } + + $this->outdent(); + return $result; + } + /** * Prints reformatted text of the passed comments. * @@ -46,7 +82,7 @@ protected function pComments(array $comments): string $padding = $comments[0]->getStartLine() !== $comments[count($comments) - 1]->getEndLine() ? $this->nl : ''; - return $padding . implode($this->nl, $formattedComments) . $padding; + return "\n" . $this->nl . trim($padding . implode($this->nl, $formattedComments)) . "\n"; } protected function pExpr_Include(Expr\Include_ $node) diff --git a/tests/Parse/ArrayFileTest.php b/tests/Parse/ArrayFileTest.php index ccea6d8d5..965f51077 100644 --- a/tests/Parse/ArrayFileTest.php +++ b/tests/Parse/ArrayFileTest.php @@ -799,4 +799,25 @@ public function testIncludeFormatting() PHP; $this->assertEquals(str_replace("\r", '', $expected), $arrayFile->render()); } + + public function testEmptyNewLines() + { + $file = __DIR__ . '/../fixtures/parse/arrayfile/sample-array-file.php'; + $arrayFile = ArrayFile::open($file); + + preg_match('/^\s+$/m', $arrayFile->render(), $matches); + + $this->assertEmpty($matches); + } + + public function testNestedComments() + { + $file = __DIR__ . '/../fixtures/parse/arrayfile/nested-comments.php'; + $arrayFile = ArrayFile::open($file); + + $code = $arrayFile->render(); + + $this->assertStringContainsString(str_repeat(' ', 8) . '|', $code); + $this->assertStringNotContainsString(str_repeat(' ', 12) . '|', $code); + } } diff --git a/tests/fixtures/parse/arrayfile/nested-comments.php b/tests/fixtures/parse/arrayfile/nested-comments.php new file mode 100644 index 000000000..846e31a59 --- /dev/null +++ b/tests/fixtures/parse/arrayfile/nested-comments.php @@ -0,0 +1,41 @@ + [ + + /* + |-------------------------------------------------------------------------- + | Enable throttling of Backend authentication attempts + |-------------------------------------------------------------------------- + | + | If set to true, users will be given a limited number of attempts to sign + | in to the Backend before being blocked for a specified number of minutes. + | + */ + + 'enabled' => true, + + /* + |-------------------------------------------------------------------------- + | Failed Authentication Attempt Limit + |-------------------------------------------------------------------------- + | + | Number of failed attempts allowed while trying to authenticate a user. + | + */ + + 'attemptLimit' => 5, + + /* + |-------------------------------------------------------------------------- + | Suspension Time + |-------------------------------------------------------------------------- + | + | The number of minutes to suspend further attempts on authentication once + | the attempt limit is reached. + | + */ + + 'suspensionTime' => 15, + ], +];