Skip to content

Commit

Permalink
PsrPrinter: fixed indentation of dumps [Closes #41]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 5, 2019
1 parent 502e69f commit aea6e81
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
12 changes: 9 additions & 3 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/PhpGenerator/PsrPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
20 changes: 10 additions & 10 deletions tests/PhpGenerator/expected/PsrPrinter.class.expect
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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,
];

/**
Expand Down

0 comments on commit aea6e81

Please sign in to comment.