Skip to content

Commit d82f26a

Browse files
committed
Boolean, float and double values not working properly in Code\ValueGenerator - Close #36
1 parent 89ee113 commit d82f26a

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/Code/ValueGenerator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ public function getAutoDeterminedType($value): string
153153
case 'string':
154154
return self::TYPE_STRING;
155155
case 'double':
156+
return self::TYPE_DOUBLE;
156157
case 'float':
158+
return self::TYPE_FLOAT;
157159
case 'integer':
158160
return self::TYPE_NUMBER;
159161
case 'array':
@@ -184,7 +186,7 @@ public function generate(): Node\Expr
184186
return new Node\Expr\ConstFetch(new Node\Name('null'));
185187
case self::TYPE_BOOLEAN:
186188
case self::TYPE_BOOL:
187-
return new Node\Expr\ConstFetch(new Node\Name($this->value));
189+
return new Node\Expr\ConstFetch(new Node\Name($this->value ? 'true' : 'false'));
188190
case self::TYPE_STRING:
189191
return new Node\Scalar\String_($this->value);
190192
case self::TYPE_NUMBER:
@@ -210,8 +212,11 @@ public function generate(): Node\Expr
210212
$arrayItems,
211213
['kind' => Node\Expr\Array_::KIND_SHORT]
212214
);
213-
break;
214215
case self::TYPE_OTHER:
216+
if ($this->value instanceof Node\Expr) {
217+
return $this->value;
218+
}
219+
// no break
215220
default:
216221
throw new Exception\RuntimeException(
217222
\sprintf('Type "%s" is unknown or cannot be used as property default value.', \get_class($value))

tests/Code/ValueGeneratorTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModelingTest\CodeAst\Code;
12+
13+
use Generator;
14+
use OpenCodeModeling\CodeAst\Code\ValueGenerator;
15+
use PhpParser\Node;
16+
use PHPUnit\Framework\TestCase;
17+
18+
final class ValueGeneratorTest extends TestCase
19+
{
20+
/**
21+
* Values are: type, expected output
22+
*
23+
* @return Generator
24+
*/
25+
public function provideTypes(): Generator
26+
{
27+
yield 'null' => [null, Node\Expr\ConstFetch::class];
28+
yield 'string' => ['test string', Node\Scalar\String_::class];
29+
yield 'bool' => [true, Node\Expr\ConstFetch::class];
30+
yield 'int' => [1, Node\Scalar\LNumber::class];
31+
yield 'integer' => [10, Node\Scalar\LNumber::class];
32+
yield 'float' => [2.523, Node\Scalar\DNumber::class];
33+
yield 'double' => [7E-10, Node\Scalar\DNumber::class];
34+
yield 'array' => [['one', 'two'], Node\Expr\Array_::class];
35+
yield 'other node expression' => [new Node\Expr\Array_(), Node\Expr\Array_::class];
36+
}
37+
38+
/**
39+
* @test
40+
* @dataProvider provideTypes
41+
* @param mixed $value
42+
* @param string $expectedGeneratedValue
43+
*/
44+
public function it_supports_type($value, string $expectedGeneratedValue): void
45+
{
46+
$value = new ValueGenerator($value);
47+
48+
$this->assertInstanceOf($expectedGeneratedValue, $value->generate());
49+
}
50+
}

0 commit comments

Comments
 (0)