Skip to content

Commit

Permalink
Printer: added $linesBetweenProperties [Closes #60]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 27, 2020
1 parent ea2c8e8 commit 995ed5b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Printer
/** @var string */
protected $indentation = "\t";

/** @var int */
protected $linesBetweenProperties = 0;

/** @var int */
protected $linesBetweenMethods = 2;

Expand Down Expand Up @@ -138,8 +141,8 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st

$members = array_filter([
implode('', $traits),
preg_replace('#^(\w.*\n)\n(?=\w.*;)#m', '$1', implode("\n", $consts)),
preg_replace('#^(\w.*\n)\n(?=\w.*;)#m', '$1', implode("\n", $properties)),
$this->joinProperties($consts),
$this->joinProperties($properties),
($methods && $properties ? str_repeat("\n", $this->linesBetweenMethods - 1) : '')
. implode(str_repeat("\n", $this->linesBetweenMethods), $methods),
]);
Expand Down Expand Up @@ -278,4 +281,12 @@ private function printReturnType($function, ?PhpNamespace $namespace): string
? ': ' . $tmp
: '';
}


private function joinProperties(array $props)
{
return $this->linesBetweenProperties
? implode(str_repeat("\n", $this->linesBetweenProperties), $props)
: preg_replace('#^(\w.*\n)\n(?=\w.*;)#m', '$1', implode("\n", $props));
}
}
8 changes: 8 additions & 0 deletions tests/PhpGenerator/Printer.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ sameFile(__DIR__ . '/expected/Printer.class.expect', $printer->printClass($class
sameFile(__DIR__ . '/expected/Printer.method.expect', $printer->printMethod($class->getMethod('first')));


Assert::with($printer, function () {
$this->linesBetweenProperties = 1;
$this->linesBetweenMethods = 3;
});
sameFile(__DIR__ . '/expected/Printer.class-alt.expect', $printer->printClass($class));



$function = new Nette\PhpGenerator\GlobalFunction('func');
$function
->setReturnType('stdClass')
Expand Down
65 changes: 65 additions & 0 deletions tests/PhpGenerator/expected/Printer.class-alt.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Description of class.
* This is example
*/
final class Example extends ParentClass implements IExample
{
use ObjectTrait;
use AnotherTrait {
sayHello as protected;
}

/** Commented */
private const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY;

const MULTILINE_LONG = [
'aaaaaaaa' => 1,
'bbbbbbbb' => 2,
'cccccccc' => 3,
'dddddddd' => 4,
'eeeeeeee' => 5,
'ffffffff' => 6,
];

const SHORT = ['aaaaaaaa' => 1, 'bbbbbbbb' => 2, 'cccccccc' => 3, 'dddddddd' => 4, 'eeeeeeee' => 5, 'ffffffff' => 6];

/** @var resource orignal file handle */
private $handle;

public $order = RecursiveIteratorIterator::SELF_FIRST;

public $multilineLong = [
'aaaaaaaa' => 1,
'bbbbbbbb' => 2,
'cccccccc' => 3,
'dddddddd' => 4,
'eeeeeeee' => 5,
'ffffffff' => 6,
];

public $short = ['aaaaaaaa' => 1, 'bbbbbbbb' => 2, 'cccccccc' => 3, 'dddddddd' => 4, 'eeeeeeee' => 5, 'ffffffff' => 6];



/**
* @return resource
*/
final public function first(stdClass $var): stdClass
{
func();
return [
'aaaaaaaaaaaa' => 1,
'bbbbbbbbbbb' => 2,
'cccccccccccccc' => 3,
'dddddddddddd' => 4,
'eeeeeeeeeeee' => 5,
'ffffffff' => 6,
];
}



public function second()
{
}
}

0 comments on commit 995ed5b

Please sign in to comment.