Skip to content

Commit

Permalink
More bugfix with constants writing
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi committed Jun 29, 2016
1 parent 713b15e commit 55b21a5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
26 changes: 14 additions & 12 deletions src/model/PhpConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
}
Expand Down
5 changes: 2 additions & 3 deletions src/model/parts/ConstantsPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/parser/visitor/AbstractPhpStructVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down
10 changes: 9 additions & 1 deletion src/visitor/GeneratorVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions tests/model/PhpClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 55b21a5

Please sign in to comment.