Skip to content

Commit 713b15e

Browse files
committed
Bugfix when reading constant values
1 parent 9086d9f commit 713b15e

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

src/model/PhpClass.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,16 @@ public static function fromReflection(\ReflectionClass $ref) {
6666

6767
// constants
6868
// TODO: https://github.com/gossi/php-code-generator/issues/19
69-
$class->setConstants($ref->getConstants());
69+
foreach ($ref->getConstants() as $name => $value) {
70+
$const = new PhpConstant($name);
71+
72+
if (is_string($value)) {
73+
$const->setValue($value);
74+
} else {
75+
$const->setExpression($value);
76+
}
77+
$class->setConstant($const);
78+
}
7079

7180
return $class;
7281
}

src/parser/visitor/AbstractPhpStructVisitor.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
use PhpParser\Node\Stmt\UseUse;
2828
use PhpParser\NodeVisitorAbstract;
2929
use PhpParser\PrettyPrinter\Standard;
30+
use PhpParser\Node\Scalar\LNumber;
31+
use PhpParser\Node\Scalar\DNumber;
32+
use PhpParser\Node\Scalar\MagicConst;
3033

3134
abstract class AbstractPhpStructVisitor extends NodeVisitorAbstract {
3235

@@ -122,9 +125,8 @@ protected function visitConstants(ClassConst $node) {
122125
}
123126

124127
protected function visitConstant(Const_ $node, Doc $doc = null) {
125-
$const = new PhpConstant($node->name, $this->getValue($node));
126-
$const->setValue($this->getValue($node->value));
127-
128+
$const = new PhpConstant($node->name);
129+
$this->setDefault($const, $node->value);
128130
$this->parseMemberDocblock($const, $doc);
129131

130132
$this->struct->setConstant($const);
@@ -244,7 +246,7 @@ private function parseMemberDocblock($member, Doc $doc = null) {
244246
}
245247
}
246248

247-
private function setDefault($obj, $default) {
249+
private function setDefault($obj, Node $default) {
248250
if ($default instanceof String_) {
249251
$obj->setValue($this->getValue($default));
250252
} else {
@@ -268,9 +270,13 @@ private function getValue(Node $node) {
268270
return $const;
269271
}
270272

271-
if ($node instanceof String_) {
273+
if ($node instanceof String_ || $node instanceof LNumber || $node instanceof DNumber) {
272274
return $node->value;
273275
}
276+
277+
if ($node instanceof MagicConst) {
278+
return $node->getName();
279+
}
274280
}
275281

276282
/**

tests/fixture/ClassWithConstants.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
class ClassWithConstants {
55

66
const FOO = 'bar';
7-
7+
8+
const NMBR = 300;
89
}

tests/model/PhpClassTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function testFromReflection() {
8585

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

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

334335
$this->assertTrue($class->hasConstant('FOO'));
336+
$this->assertTrue($class->hasConstant('NMBR'));
337+
$this->assertEquals(300, $class->getConstant('NMBR')->getExpression());
335338
}
336339

337340
public function testFromFileWithTraits() {

0 commit comments

Comments
 (0)