Skip to content

Commit

Permalink
Bugfix when reading constant values
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi committed Jun 29, 2016
1 parent 9086d9f commit 713b15e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/model/PhpClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ public static function fromReflection(\ReflectionClass $ref) {

// constants
// TODO: https://github.com/gossi/php-code-generator/issues/19
$class->setConstants($ref->getConstants());
foreach ($ref->getConstants() as $name => $value) {
$const = new PhpConstant($name);

if (is_string($value)) {
$const->setValue($value);
} else {
$const->setExpression($value);
}
$class->setConstant($const);
}

return $class;
}
Expand Down
16 changes: 11 additions & 5 deletions src/parser/visitor/AbstractPhpStructVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
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 Expand Up @@ -122,9 +125,8 @@ protected function visitConstants(ClassConst $node) {
}

protected function visitConstant(Const_ $node, Doc $doc = null) {
$const = new PhpConstant($node->name, $this->getValue($node));
$const->setValue($this->getValue($node->value));

$const = new PhpConstant($node->name);
$this->setDefault($const, $node->value);
$this->parseMemberDocblock($const, $doc);

$this->struct->setConstant($const);
Expand Down Expand Up @@ -244,7 +246,7 @@ private function parseMemberDocblock($member, Doc $doc = null) {
}
}

private function setDefault($obj, $default) {
private function setDefault($obj, Node $default) {
if ($default instanceof String_) {
$obj->setValue($this->getValue($default));
} else {
Expand All @@ -268,9 +270,13 @@ private function getValue(Node $node) {
return $const;
}

if ($node instanceof String_) {
if ($node instanceof String_ || $node instanceof LNumber || $node instanceof DNumber) {
return $node->value;
}

if ($node instanceof MagicConst) {
return $node->getName();
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/fixture/ClassWithConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
class ClassWithConstants {

const FOO = 'bar';


const NMBR = 300;
}
3 changes: 3 additions & 0 deletions tests/model/PhpClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function testFromReflection() {

$class = new PhpClass('gossi\codegen\tests\fixture\ClassWithConstants');
$class->setConstant('FOO', 'bar');
$class->setConstant(PhpConstant::create('NMBR')->setExpression(300));
$this->assertEquals($class, PhpClass::fromReflection(new \ReflectionClass('gossi\codegen\tests\fixture\ClassWithConstants')));
}

Expand Down Expand Up @@ -332,6 +333,8 @@ public function testFromFileWithConstants() {
$class = PhpClass::fromFile(__DIR__ . '/../fixture/ClassWithConstants.php');

$this->assertTrue($class->hasConstant('FOO'));
$this->assertTrue($class->hasConstant('NMBR'));
$this->assertEquals(300, $class->getConstant('NMBR')->getExpression());
}

public function testFromFileWithTraits() {
Expand Down

0 comments on commit 713b15e

Please sign in to comment.