Skip to content

Commit

Permalink
Merge pull request #32 from ajthinking/fix-multiline-arrays
Browse files Browse the repository at this point in the history
Add Multiline Arrays
  • Loading branch information
ajthinking authored Nov 20, 2021
2 parents a3a5220 + 1f13d1d commit 492838a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 35 deletions.
30 changes: 3 additions & 27 deletions src/Support/PSR2PrettyPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ protected function pStmt_Class(Class_ $node)
protected function pStmt_ClassMethod(ClassMethod $node)
{
$comments = $node->getComments();



$ln = $comments ? '' : $this->nl;

Expand All @@ -45,8 +43,9 @@ protected function pStmt_ClassMethod(ClassMethod $node)

protected function pExpr_Array(Array_ $node)
{
// Fix proper multiline here
return parent::pExpr_array($node);
$stmts = $this->pCommaSeparatedMultiline($node->items, true);
$lineBreaked = $stmts ? $stmts . $this->nl : $stmts;
return "[{$lineBreaked}]";
}

/**
Expand Down Expand Up @@ -123,29 +122,6 @@ protected function pAttrGroups(array $nodes, bool $inline = false): string
return $result;
}

protected function pMaybeMultiline(array $nodes, bool $trailingComma = false)
{
if (!$this->hasNodeWithComments($nodes)) {
return $this->pCommaSeparated($nodes);
} else {
return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl;
}
}

/**
* @param Node[] $nodes
* @return bool
*/
protected function hasNodeWithComments(array $nodes)
{
foreach ($nodes as $node) {
if ($node && $node->getComments()) {
return true;
}
}
return false;
}

/**
* Prints reformatted text of the passed comments.
*
Expand Down
19 changes: 11 additions & 8 deletions tests/Feature/PrettyPrintTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use Archetype\Facades\LaravelFile;
use Archetype\Facades\PHPFile;

class PrettyPrintTest extends Archetype\Tests\TestCase
{
public function test_arrays_are_beutiful_when_loaded_and_rendered()
Expand All @@ -10,29 +13,29 @@ public function test_arrays_are_beutiful_when_loaded_and_rendered()

public function test_arrays_are_beutiful_when_loaded_modified_and_rendered()
{
$this->markTestIncomplete();

$output = LaravelFile::user()
->add('also')->to()->property('fillable')
->render();

$this->assertMultilineArray('fillable', $output);
}

public function test_arrays_are_beutiful_when_created_and_rendered()
public function test_arrays_are_beautiful_when_created_and_rendered()
{
$this->markTestIncomplete();

$output = PHPFile::class('FillableClass')
->add()->property('fillable', ['first', 'second', 'third'])
->render();
$this->assertMultilineArray('ints', $output);

$this->assertMultilineArray('fillable', $output);
}

public function test_arrays_are_beutiful_when_empty()
{
$this->markTestIncomplete();
$output = PHPFile::class('FillableClass')
->property('fillable', [])
->render();

$this->assertSingleLineEmptyArray('fillable', $output);
}

public function test_arrays_have_trailing_comma_after_last_item()
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ public function assertMultilineArray($name, $output) {
$commas + 1
);
}

public function assertSingleLineEmptyArray($name, $output) {
$this->assertMatchesRegularExpression("/$name \= (\[\];]*)/s", $output);
}
}

0 comments on commit 492838a

Please sign in to comment.