Skip to content

Commit 9086d9f

Browse files
committed
Bugfix for parsing default values/expression
1 parent 3afdd0b commit 9086d9f

File tree

8 files changed

+73
-21
lines changed

8 files changed

+73
-21
lines changed

src/model/PhpConstant.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ class PhpConstant extends AbstractModel implements GenerateableInterface, Docblo
3333
*/
3434
public static function create($name = null, $value = null) {
3535
$constant = new static();
36-
$constant->setName($name)->setValue($value);
36+
$constant->setName($name);
37+
38+
if (is_string($value)) {
39+
$constant->setValue($value);
40+
} else {
41+
$constant->setExpression($value);
42+
}
3743

3844
return $constant;
3945
}
@@ -46,7 +52,12 @@ public static function create($name = null, $value = null) {
4652
*/
4753
public function __construct($name = null, $value = null) {
4854
$this->setName($name);
49-
$this->setValue($value);
55+
56+
if (is_string($value)) {
57+
$this->setValue($value);
58+
} else {
59+
$this->setExpression($value);
60+
}
5061
$this->docblock = new Docblock();
5162
}
5263

src/model/PhpParameter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ public static function fromReflection(\ReflectionParameter $ref) {
5757
$parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference());
5858

5959
if ($ref->isDefaultValueAvailable()) {
60-
$parameter->setValue($ref->getDefaultValue());
60+
$default = $ref->getDefaultValue();
61+
if (is_string($default)) {
62+
$parameter->setValue($default);
63+
} else {
64+
$parameter->setExpression($default);
65+
}
6166
}
6267

6368
// find type and description in docblock

src/model/PhpProperty.php

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

6767
$defaultProperties = $ref->getDeclaringClass()->getDefaultProperties();
6868
if (isset($defaultProperties[$ref->name])) {
69-
$property->setValue($defaultProperties[$ref->name]);
69+
$default = $defaultProperties[$ref->name];
70+
if (is_string($default)) {
71+
$property->setValue($default);
72+
} else {
73+
$property->setExpression($default);
74+
}
7075
}
7176

7277
return $property;

src/parser/visitor/AbstractPhpStructVisitor.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ protected function visitMethod(ClassMethod $node) {
180180

181181
$default = $param->default;
182182
if ($default !== null) {
183-
$p->setValue($this->getValue($default));
183+
$this->setDefault($p, $default);
184184
}
185185

186186
$tag = $params->find($p, function (ParamTag $t, $p) {
@@ -218,7 +218,7 @@ protected function getProperty(Property $node) {
218218

219219
$default = $prop->default;
220220
if ($default !== null) {
221-
$p->setValue($this->getValue($default));
221+
$this->setDefault($p, $default);
222222
}
223223

224224
$p->setStatic($node->isStatic());
@@ -244,6 +244,14 @@ private function parseMemberDocblock($member, Doc $doc = null) {
244244
}
245245
}
246246

247+
private function setDefault($obj, $default) {
248+
if ($default instanceof String_) {
249+
$obj->setValue($this->getValue($default));
250+
} else {
251+
$obj->setExpression($this->getValue($default));
252+
}
253+
}
254+
247255
/**
248256
* Returns the value from a node
249257
*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace gossi\codegen\tests\fixture;
3+
4+
class ClassWithDefaultExpressions {
5+
6+
private $bar = false;
7+
private $baz = true;
8+
private $number = 5;
9+
private $furz = 'bumbesje';
10+
11+
public function foo($bar = null) {
12+
13+
}
14+
}

tests/model/PhpClassTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testFromReflection() {
4444
->setDescription($propDoc->getShortDescription()))
4545
->setProperty(PhpProperty::create('enabled')
4646
->setVisibility('private')
47-
->setDefaultValue(false));
47+
->setExpression(false));
4848

4949
$methodDoc = new Docblock('/**
5050
* Another doc comment.
@@ -69,7 +69,7 @@ public function testFromReflection() {
6969
->addParameter(PhpParameter::create()
7070
->setName('d')
7171
->setType('string')
72-
->setDefaultValue('foo'))
72+
->setValue('foo'))
7373
->addParameter(PhpParameter::create()
7474
->setName('e')
7575
->setType('callable'))
@@ -350,6 +350,15 @@ public function testFromFileWithComments() {
350350
$this->assertEquals(file_get_contents(__DIR__ . '/../fixture/ClassWithComments.php'), $code);
351351
}
352352

353+
public function testFromFileWithDefaultExpressions() {
354+
$class = PhpClass::fromFile(__DIR__ . '/../fixture/ClassWithDefaultExpressions.php');
355+
$bar = $class->getProperty('bar');
356+
357+
$this->assertFalse($bar->getExpression());
358+
$this->assertTrue($bar->isExpression());
359+
$this->assertNull($bar->getValue());
360+
}
361+
353362
public function testFromReflectionWithComments() {
354363
// TODO: https://github.com/gossi/php-code-generator/issues/19
355364
// $class = PhpClass::fromReflection(new \ReflectionClass('gossi\codegen\tests\fixture\ClassWithComments'));
@@ -382,7 +391,7 @@ private function assertClassWithComments(PhpClass $class) {
382391
$this->assertEquals('Aaaand we go along long long', $propper->getLongDescription());
383392
$this->assertEquals('Wer macht sauber?', $propper->getTypeDescription());
384393
$this->assertEquals('string', $propper->getType());
385-
$this->assertEquals('Meister', $propper->getDefaultValue());
394+
$this->assertEquals('Meister', $propper->getValue());
386395

387396
$setup = $class->getMethod('setup');
388397
$this->assertEquals('Short desc', $setup->getDescription());

tests/model/PhpFunctionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public function setUp() {
1616
public function testFromReflection() {
1717
$doc = new Docblock('/**
1818
* Makes foo with bar
19-
*
19+
*
2020
* @param string $baz
2121
* @return string
2222
*/');
2323
$function = new PhpFunction('wurst');
2424
$function
2525
->addParameter(PhpParameter::create('baz')
26-
->setDefaultValue(null)
26+
->setExpression(null)
2727
->setType('string')
2828
)
2929
->setBody('return \'wurst\';')

tests/model/PhpParameterTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private function paramA(\ReflectionParameter $param) {
4343

4444
$this->assertEquals('a', $param->getName());
4545
$this->assertFalse($param->isPassedByReference());
46-
$this->assertEmpty($param->getDefaultValue());
46+
$this->assertEmpty($param->getValue());
4747
$this->assertEquals('unknown_type', $param->getType());
4848
}
4949

@@ -52,7 +52,7 @@ private function paramB(\ReflectionParameter $param) {
5252

5353
$this->assertEquals('b', $param->getName());
5454
$this->assertTrue($param->isPassedByReference());
55-
$this->assertEmpty($param->getDefaultValue());
55+
$this->assertEmpty($param->getValue());
5656
$this->assertEquals('array', $param->getType());
5757
}
5858

@@ -61,7 +61,7 @@ private function paramC(\ReflectionParameter $param) {
6161

6262
$this->assertEquals('c', $param->getName());
6363
$this->assertFalse($param->isPassedByReference());
64-
$this->assertEmpty($param->getDefaultValue());
64+
$this->assertEmpty($param->getValue());
6565
$this->assertEquals('\stdClass', $param->getType());
6666
}
6767

@@ -70,7 +70,7 @@ private function paramD(\ReflectionParameter $param) {
7070

7171
$this->assertEquals('d', $param->getName());
7272
$this->assertFalse($param->isPassedByReference());
73-
$this->assertEquals('foo', $param->getDefaultValue());
73+
$this->assertEquals('foo', $param->getValue());
7474
$this->assertEquals('string', $param->getType());
7575
}
7676

@@ -79,7 +79,7 @@ private function paramE(\ReflectionParameter $param) {
7979

8080
$this->assertEquals('e', $param->getName());
8181
$this->assertFalse($param->isPassedByReference());
82-
$this->assertEmpty($param->getDefaultValue());
82+
$this->assertEmpty($param->getValue());
8383
$this->assertEquals('callable', $param->getType());
8484
}
8585

@@ -101,14 +101,14 @@ public function testSimpleParameter() {
101101
$this->assertFalse($function->hasParameter('param2'));
102102
$param1 = $function->getParameter('param1');
103103
$this->assertEquals('string', $param1->getType());
104-
$this->assertFalse($param1->hasDefaultValue());
104+
$this->assertFalse($param1->hasValue());
105105

106106
$function->addSimpleParameter('param2', 'string', null);
107107

108108
$this->assertTrue($function->hasParameter('param2'));
109109
$param2 = $function->getParameter('param2');
110110
$this->assertEquals('string', $param2->getType());
111-
$this->assertNull($param2->getDefaultValue());
111+
$this->assertNull($param2->getValue());
112112
}
113113

114114
public function testSimpleDescParameter() {
@@ -118,21 +118,21 @@ public function testSimpleDescParameter() {
118118
$this->assertFalse($function->hasParameter('param2'));
119119
$param1 = $function->getParameter('param1');
120120
$this->assertEquals('string', $param1->getType());
121-
$this->assertFalse($param1->hasDefaultValue());
121+
$this->assertFalse($param1->hasValue());
122122

123123
$function->addSimpleDescParameter('param2', 'string', 'desc');
124124

125125
$this->assertTrue($function->hasParameter('param2'));
126126
$param2 = $function->getParameter('param2');
127127
$this->assertEquals('string', $param2->getType());
128-
$this->assertFalse($param2->hasDefaultValue());
128+
$this->assertFalse($param2->hasValue());
129129

130130
$function->addSimpleDescParameter('param3', 'string', 'desc', null);
131131

132132
$this->assertTrue($function->hasParameter('param3'));
133133
$param3 = $function->getParameter('param3');
134134
$this->assertEquals('string', $param3->getType());
135-
$this->assertNull($param3->getDefaultValue());
135+
$this->assertNull($param3->getValue());
136136
}
137137

138138
}

0 commit comments

Comments
 (0)