From aea6e81437bb238e5f0e5b5ce06337433908e63b Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 5 Jul 2019 14:59:57 +0200 Subject: [PATCH] PsrPrinter: fixed indentation of dumps [Closes #41] --- src/PhpGenerator/Printer.php | 12 ++++++++--- src/PhpGenerator/PsrPrinter.php | 6 ++++++ .../expected/PsrPrinter.class.expect | 20 +++++++++---------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 420c89f3..c516939e 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -98,14 +98,14 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st foreach ($class->getConstants() as $const) { $consts[] = Helpers::formatDocComment((string) $const->getComment()) . ($const->getVisibility() ? $const->getVisibility() . ' ' : '') - . 'const ' . $const->getName() . ' = ' . Helpers::dump($const->getValue()) . ";\n"; + . 'const ' . $const->getName() . ' = ' . $this->dump($const->getValue()) . ";\n"; } $properties = []; foreach ($class->getProperties() as $property) { $properties[] = Helpers::formatDocComment((string) $property->getComment()) . ($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName() - . ($property->getValue() === null ? '' : ' = ' . Helpers::dump($property->getValue())) + . ($property->getValue() === null ? '' : ' = ' . $this->dump($property->getValue())) . ";\n"; } @@ -194,6 +194,12 @@ protected function indent(string $s): string } + protected function dump($var): string + { + return Helpers::dump($var); + } + + protected function printUses(PhpNamespace $namespace): string { $name = $namespace->getName(); @@ -225,7 +231,7 @@ protected function printParameters($function, ?PhpNamespace $namespace): string . ($param->isReference() ? '&' : '') . ($variadic ? '...' : '') . '$' . $param->getName() - . ($param->hasDefaultValue() && !$variadic ? ' = ' . Helpers::dump($param->getDefaultValue()) : ''); + . ($param->hasDefaultValue() && !$variadic ? ' = ' . $this->dump($param->getDefaultValue()) : ''); } return strlen($tmp = implode(', ', $params)) > Helpers::WRAP_LENGTH && count($params) > 1 diff --git a/src/PhpGenerator/PsrPrinter.php b/src/PhpGenerator/PsrPrinter.php index aef0f7d0..b9259dd8 100644 --- a/src/PhpGenerator/PsrPrinter.php +++ b/src/PhpGenerator/PsrPrinter.php @@ -20,4 +20,10 @@ final class PsrPrinter extends Printer /** @var int */ protected $linesBetweenMethods = 1; + + + protected function dump($var): string + { + return str_replace("\t", $this->indentation, Helpers::dump($var)); + } } diff --git a/tests/PhpGenerator/expected/PsrPrinter.class.expect b/tests/PhpGenerator/expected/PsrPrinter.class.expect index 734e571d..3d684973 100644 --- a/tests/PhpGenerator/expected/PsrPrinter.class.expect +++ b/tests/PhpGenerator/expected/PsrPrinter.class.expect @@ -12,11 +12,11 @@ final class Example extends ParentClass implements IExample /** Commented */ private const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY; const MULTILINE = [ - 'aaaaaaaaaaaa' => 1, - 'bbbbbbbbbbb' => 2, - 'cccccccccccccc' => 3, - 'dddddddddddd' => 4, - 'eeeeeeeeeeee' => 5, + 'aaaaaaaaaaaa' => 1, + 'bbbbbbbbbbb' => 2, + 'cccccccccccccc' => 3, + 'dddddddddddd' => 4, + 'eeeeeeeeeeee' => 5, ]; /** @var resource orignal file handle */ @@ -25,11 +25,11 @@ final class Example extends ParentClass implements IExample public $order = RecursiveIteratorIterator::SELF_FIRST; public $multiline = [ - 'aaaaaaaaaaaa' => 1, - 'bbbbbbbbbbb' => 2, - 'cccccccccccccc' => 3, - 'dddddddddddd' => 4, - 'eeeeeeeeeeee' => 5, + 'aaaaaaaaaaaa' => 1, + 'bbbbbbbbbbb' => 2, + 'cccccccccccccc' => 3, + 'dddddddddddd' => 4, + 'eeeeeeeeeeee' => 5, ]; /**