Skip to content

Commit 55b21a5

Browse files
committed
More bugfix with constants writing
1 parent 713b15e commit 55b21a5

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

src/model/PhpConstant.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@ class PhpConstant extends AbstractModel implements GenerateableInterface, Docblo
2929
*
3030
* @param string $name
3131
* @param mixed $value
32+
* @param bool $isExpression
3233
* @return static
3334
*/
34-
public static function create($name = null, $value = null) {
35-
$constant = new static();
36-
$constant->setName($name);
35+
public static function create($name = null, $value = null, $isExpression = false) {
36+
$constant = new static($name, $value, $isExpression);
37+
// $constant->setName($name);
3738

38-
if (is_string($value)) {
39-
$constant->setValue($value);
40-
} else {
41-
$constant->setExpression($value);
42-
}
39+
// if (is_string($value)) {
40+
// $constant->setValue($value);
41+
// } else {
42+
// $constant->setExpression($value);
43+
// }
4344

4445
return $constant;
4546
}
@@ -49,14 +50,15 @@ public static function create($name = null, $value = null) {
4950
*
5051
* @param string $name
5152
* @param mixed $value
53+
* @param bool $isExpression
5254
*/
53-
public function __construct($name = null, $value = null) {
55+
public function __construct($name = null, $value = null, $isExpression = false) {
5456
$this->setName($name);
5557

56-
if (is_string($value)) {
57-
$this->setValue($value);
58-
} else {
58+
if ($isExpression) {
5959
$this->setExpression($value);
60+
} else {
61+
$this->setValue($value);
6062
}
6163
$this->docblock = new Docblock();
6264
}

src/model/parts/ConstantsPart.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ public function setConstants(array $constants) {
4747
* @param string $value
4848
* @return $this
4949
*/
50-
public function setConstant($nameOrConstant, $value = null) {
50+
public function setConstant($nameOrConstant, $value = null, $isExpression = false) {
5151
if ($nameOrConstant instanceof PhpConstant) {
5252
$name = $nameOrConstant->getName();
5353
$constant = $nameOrConstant;
5454
} else {
5555
$name = $nameOrConstant;
56-
$constant = new PhpConstant($nameOrConstant);
57-
$constant->setValue($value);
56+
$constant = new PhpConstant($nameOrConstant, $value, $isExpression);
5857
}
5958

6059
$this->constants[$name] = $constant;

src/parser/visitor/AbstractPhpStructVisitor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use PhpParser\Node\Expr\ConstFetch;
1515
use PhpParser\Node\Name;
1616
use PhpParser\Node\Param;
17+
use PhpParser\Node\Scalar\DNumber;
18+
use PhpParser\Node\Scalar\LNumber;
19+
use PhpParser\Node\Scalar\MagicConst;
1720
use PhpParser\Node\Scalar\String_;
1821
use PhpParser\Node\Stmt\Class_;
1922
use PhpParser\Node\Stmt\ClassConst;
@@ -27,9 +30,6 @@
2730
use PhpParser\Node\Stmt\UseUse;
2831
use PhpParser\NodeVisitorAbstract;
2932
use PhpParser\PrettyPrinter\Standard;
30-
use PhpParser\Node\Scalar\LNumber;
31-
use PhpParser\Node\Scalar\DNumber;
32-
use PhpParser\Node\Scalar\MagicConst;
3333

3434
abstract class AbstractPhpStructVisitor extends NodeVisitorAbstract {
3535

src/visitor/GeneratorVisitor.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,15 @@ public function startVisitingStructConstants() {
209209

210210
public function visitStructConstant(PhpConstant $constant) {
211211
$this->visitDocblock($constant->getDocblock());
212-
$this->writer->writeln('const ' . $constant->getName() . ' = ' . $this->getPhpExport($constant->getValue()) . ';');
212+
$this->writer->write('const ' . $constant->getName() . ' = ');
213+
214+
if ($constant->isExpression()) {
215+
$this->writer->write($constant->getExpression());
216+
} else {
217+
$this->writer->write($this->getPhpExport($constant->getValue()));
218+
}
219+
220+
$this->writer->writeln(';');
213221
}
214222

215223
public function endVisitingStructConstants() {

tests/model/PhpClassTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public function testConstants() {
117117
$class->clearConstants();
118118
$this->assertEmpty($class->getConstants());
119119

120+
$class->setConstant('FOO', 'bar');
121+
$this->assertEquals('bar', $class->getConstant('FOO')->getValue());
122+
$class->setConstant('NMBR', 300, true);
123+
$this->assertEquals(300, $class->getConstant('NMBR')->getExpression());
124+
120125
try {
121126
$this->assertEmpty($class->getConstant('constant-not-found'));
122127
} catch (\InvalidArgumentException $e) {
@@ -333,6 +338,8 @@ public function testFromFileWithConstants() {
333338
$class = PhpClass::fromFile(__DIR__ . '/../fixture/ClassWithConstants.php');
334339

335340
$this->assertTrue($class->hasConstant('FOO'));
341+
$this->assertEquals('bar', $class->getConstant('FOO')->getValue());
342+
336343
$this->assertTrue($class->hasConstant('NMBR'));
337344
$this->assertEquals(300, $class->getConstant('NMBR')->getExpression());
338345
}

0 commit comments

Comments
 (0)