From 55b21a5e44c95bc1a9f15ad19f36998f89467593 Mon Sep 17 00:00:00 2001 From: gossi Date: Wed, 29 Jun 2016 14:29:08 +0200 Subject: [PATCH] More bugfix with constants writing --- src/model/PhpConstant.php | 26 ++++++++++--------- src/model/parts/ConstantsPart.php | 5 ++-- .../visitor/AbstractPhpStructVisitor.php | 6 ++--- src/visitor/GeneratorVisitor.php | 10 ++++++- tests/model/PhpClassTest.php | 7 +++++ 5 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/model/PhpConstant.php b/src/model/PhpConstant.php index 21f4558..ae2a3fa 100644 --- a/src/model/PhpConstant.php +++ b/src/model/PhpConstant.php @@ -29,17 +29,18 @@ class PhpConstant extends AbstractModel implements GenerateableInterface, Docblo * * @param string $name * @param mixed $value + * @param bool $isExpression * @return static */ - public static function create($name = null, $value = null) { - $constant = new static(); - $constant->setName($name); + public static function create($name = null, $value = null, $isExpression = false) { + $constant = new static($name, $value, $isExpression); +// $constant->setName($name); - if (is_string($value)) { - $constant->setValue($value); - } else { - $constant->setExpression($value); - } +// if (is_string($value)) { +// $constant->setValue($value); +// } else { +// $constant->setExpression($value); +// } return $constant; } @@ -49,14 +50,15 @@ public static function create($name = null, $value = null) { * * @param string $name * @param mixed $value + * @param bool $isExpression */ - public function __construct($name = null, $value = null) { + public function __construct($name = null, $value = null, $isExpression = false) { $this->setName($name); - if (is_string($value)) { - $this->setValue($value); - } else { + if ($isExpression) { $this->setExpression($value); + } else { + $this->setValue($value); } $this->docblock = new Docblock(); } diff --git a/src/model/parts/ConstantsPart.php b/src/model/parts/ConstantsPart.php index 11ed94b..8b0de1a 100644 --- a/src/model/parts/ConstantsPart.php +++ b/src/model/parts/ConstantsPart.php @@ -47,14 +47,13 @@ public function setConstants(array $constants) { * @param string $value * @return $this */ - public function setConstant($nameOrConstant, $value = null) { + public function setConstant($nameOrConstant, $value = null, $isExpression = false) { if ($nameOrConstant instanceof PhpConstant) { $name = $nameOrConstant->getName(); $constant = $nameOrConstant; } else { $name = $nameOrConstant; - $constant = new PhpConstant($nameOrConstant); - $constant->setValue($value); + $constant = new PhpConstant($nameOrConstant, $value, $isExpression); } $this->constants[$name] = $constant; diff --git a/src/parser/visitor/AbstractPhpStructVisitor.php b/src/parser/visitor/AbstractPhpStructVisitor.php index 4a122c3..41bc871 100644 --- a/src/parser/visitor/AbstractPhpStructVisitor.php +++ b/src/parser/visitor/AbstractPhpStructVisitor.php @@ -14,6 +14,9 @@ use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Name; use PhpParser\Node\Param; +use PhpParser\Node\Scalar\DNumber; +use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\MagicConst; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassConst; @@ -27,9 +30,6 @@ use PhpParser\Node\Stmt\UseUse; use PhpParser\NodeVisitorAbstract; use PhpParser\PrettyPrinter\Standard; -use PhpParser\Node\Scalar\LNumber; -use PhpParser\Node\Scalar\DNumber; -use PhpParser\Node\Scalar\MagicConst; abstract class AbstractPhpStructVisitor extends NodeVisitorAbstract { diff --git a/src/visitor/GeneratorVisitor.php b/src/visitor/GeneratorVisitor.php index b3209e0..2571ded 100644 --- a/src/visitor/GeneratorVisitor.php +++ b/src/visitor/GeneratorVisitor.php @@ -209,7 +209,15 @@ public function startVisitingStructConstants() { public function visitStructConstant(PhpConstant $constant) { $this->visitDocblock($constant->getDocblock()); - $this->writer->writeln('const ' . $constant->getName() . ' = ' . $this->getPhpExport($constant->getValue()) . ';'); + $this->writer->write('const ' . $constant->getName() . ' = '); + + if ($constant->isExpression()) { + $this->writer->write($constant->getExpression()); + } else { + $this->writer->write($this->getPhpExport($constant->getValue())); + } + + $this->writer->writeln(';'); } public function endVisitingStructConstants() { diff --git a/tests/model/PhpClassTest.php b/tests/model/PhpClassTest.php index da3e41e..c48bdac 100644 --- a/tests/model/PhpClassTest.php +++ b/tests/model/PhpClassTest.php @@ -117,6 +117,11 @@ public function testConstants() { $class->clearConstants(); $this->assertEmpty($class->getConstants()); + $class->setConstant('FOO', 'bar'); + $this->assertEquals('bar', $class->getConstant('FOO')->getValue()); + $class->setConstant('NMBR', 300, true); + $this->assertEquals(300, $class->getConstant('NMBR')->getExpression()); + try { $this->assertEmpty($class->getConstant('constant-not-found')); } catch (\InvalidArgumentException $e) { @@ -333,6 +338,8 @@ public function testFromFileWithConstants() { $class = PhpClass::fromFile(__DIR__ . '/../fixture/ClassWithConstants.php'); $this->assertTrue($class->hasConstant('FOO')); + $this->assertEquals('bar', $class->getConstant('FOO')->getValue()); + $this->assertTrue($class->hasConstant('NMBR')); $this->assertEquals(300, $class->getConstant('NMBR')->getExpression()); }