diff --git a/src/model/PhpClass.php b/src/model/PhpClass.php index efc0e21..49f8ace 100644 --- a/src/model/PhpClass.php +++ b/src/model/PhpClass.php @@ -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; } diff --git a/src/parser/visitor/AbstractPhpStructVisitor.php b/src/parser/visitor/AbstractPhpStructVisitor.php index 98c2706..4a122c3 100644 --- a/src/parser/visitor/AbstractPhpStructVisitor.php +++ b/src/parser/visitor/AbstractPhpStructVisitor.php @@ -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 { @@ -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); @@ -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 { @@ -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(); + } } /** diff --git a/tests/fixture/ClassWithConstants.php b/tests/fixture/ClassWithConstants.php index 4f26b25..20b6980 100644 --- a/tests/fixture/ClassWithConstants.php +++ b/tests/fixture/ClassWithConstants.php @@ -4,5 +4,6 @@ class ClassWithConstants { const FOO = 'bar'; - + + const NMBR = 300; } \ No newline at end of file diff --git a/tests/model/PhpClassTest.php b/tests/model/PhpClassTest.php index 699dd63..da3e41e 100644 --- a/tests/model/PhpClassTest.php +++ b/tests/model/PhpClassTest.php @@ -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'))); } @@ -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() {