Skip to content

Commit

Permalink
Added fixes for whitespace issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxwilko committed Mar 4, 2022
1 parent 9fda6cd commit 91f1dac
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 7 deletions.
7 changes: 1 addition & 6 deletions src/Parse/PHP/ArrayFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

/**
Expand Down
38 changes: 37 additions & 1 deletion src/Parse/PHP/ArrayPrinter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Winter\Storm\Parse\PHP;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\PrettyPrinter\Standard;
Expand Down Expand Up @@ -29,6 +30,41 @@ protected function pMaybeMultiline(array $nodes, bool $trailingComma = false)
}
}

/**
* Pretty prints a comma-separated list of nodes in multiline style, including comments.
*
* The result includes a leading newline and one level of indentation (same as pStmts).
*
* @param Node[] $nodes Array of Nodes to be printed
* @param bool $trailingComma Whether to use a trailing comma
*
* @return string Comma separated pretty printed nodes in multiline style
*/
protected function pCommaSeparatedMultiline(array $nodes, bool $trailingComma) : string {
$this->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.
*
Expand All @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions tests/Parse/ArrayFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
41 changes: 41 additions & 0 deletions tests/fixtures/parse/arrayfile/nested-comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

return [
'throttle' => [

/*
|--------------------------------------------------------------------------
| 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,
],
];

0 comments on commit 91f1dac

Please sign in to comment.