Skip to content

Commit 99fc316

Browse files
committed
Add typed property support and some fixes for StringFactory
1 parent 10d8905 commit 99fc316

File tree

3 files changed

+102
-12
lines changed

3 files changed

+102
-12
lines changed

src/PropertyFactory.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020

2121
final class PropertyFactory
2222
{
23+
/**
24+
* @var bool
25+
**/
26+
private $typed;
27+
28+
public function __construct(bool $typed)
29+
{
30+
$this->typed = $typed;
31+
}
32+
2333
/**
2434
* @param TypeSet $typeSet
2535
* @return array<NodeVisitor>
@@ -83,7 +93,7 @@ public function nodeVisitorFromNative(string $name, string $type): array
8393
{
8494
return [
8595
new \OpenCodeModeling\CodeAst\NodeVisitor\Property(
86-
new PropertyGenerator($name, $type)
96+
new PropertyGenerator($name, $type, null, $this->typed)
8797
),
8898
];
8999
}

src/ValueObject/StringFactory.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ final class StringFactory
6060
private Parser $parser;
6161
private PropertyFactory $propertyFactory;
6262

63-
public function __construct(Parser $parser)
63+
public function __construct(Parser $parser, bool $typed)
6464
{
6565
$this->parser = $parser;
66-
$this->propertyFactory = new PropertyFactory();
66+
$this->propertyFactory = new PropertyFactory($typed);
6767
}
6868

6969
/**
@@ -73,19 +73,17 @@ public function __construct(Parser $parser)
7373
public function nodeVisitors(StringType $typeDefinition): array
7474
{
7575
$name = $typeDefinition->name() ?: 'text';
76-
$type = $typeDefinition->type();
7776

78-
return $this->nodeVisitorsFromNative($type, $name);
77+
return $this->nodeVisitorsFromNative($name);
7978
}
8079

8180
/**
82-
* @param string $type
8381
* @param string $name
8482
* @return array<NodeVisitor>
8583
*/
86-
public function nodeVisitorsFromNative(string $type, string $name): array
84+
public function nodeVisitorsFromNative(string $name): array
8785
{
88-
$nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, $type);
86+
$nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, 'string');
8987
$nodeVisitors[] = $this->methodFromString($name);
9088
$nodeVisitors[] = $this->methodMagicConstruct($name);
9189
$nodeVisitors[] = $this->methodToString($name);
@@ -128,9 +126,7 @@ public function methodToString(string $argumentName): NodeVisitor
128126
{
129127
$method = new MethodGenerator(
130128
'toString',
131-
[
132-
new ParameterGenerator($argumentName, 'string'),
133-
],
129+
[],
134130
MethodGenerator::FLAG_PUBLIC,
135131
new BodyGenerator($this->parser, 'return $this->' . $argumentName . ';')
136132
);
@@ -152,7 +148,7 @@ public function methodEquals(string $propertyName, string $argumentName = 'other
152148
$method = new MethodGenerator(
153149
'equals',
154150
[
155-
new ParameterGenerator($argumentName, 'string'),
151+
new ParameterGenerator($argumentName),
156152
],
157153
MethodGenerator::FLAG_PUBLIC,
158154
new BodyGenerator($this->parser, $body)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenCodeModelingTest\JsonSchemaToPhpAst\ValueObject;
6+
7+
use OpenCodeModeling\JsonSchemaToPhpAst\ValueObject\StringFactory;
8+
use PhpParser\NodeTraverser;
9+
use PhpParser\NodeVisitor;
10+
use PhpParser\Parser;
11+
use PhpParser\ParserFactory;
12+
use PhpParser\PrettyPrinter\Standard;
13+
use PhpParser\PrettyPrinterAbstract;
14+
use PHPUnit\Framework\TestCase;
15+
16+
final class StringFactoryTest extends TestCase
17+
{
18+
private Parser $parser;
19+
private PrettyPrinterAbstract $printer;
20+
private StringFactory $stringFactory;
21+
22+
public function setUp(): void
23+
{
24+
$this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
25+
$this->printer = new Standard(['shortArraySyntax' => true]);
26+
$this->stringFactory = new StringFactory($this->parser, true);
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function it_generates_code_from_native(): void
33+
{
34+
$this->assertCode($this->stringFactory->nodeVisitorsFromNative('name'));
35+
}
36+
37+
/**
38+
* @param array<NodeVisitor> $nodeVisitors
39+
*/
40+
private function assertCode(array $nodeVisitors): void
41+
{
42+
$ast = $this->parser->parse('<?php final class StringVO {}');
43+
44+
$nodeTraverser = new NodeTraverser();
45+
46+
foreach ($nodeVisitors as $nodeVisitor) {
47+
$nodeTraverser->addVisitor($nodeVisitor);
48+
}
49+
50+
$expected = <<<'EOF'
51+
<?php
52+
53+
final class StringVO
54+
{
55+
private string $name;
56+
public static function fromString(string $name) : self
57+
{
58+
return new self($name);
59+
}
60+
private function __construct(string $name)
61+
{
62+
$this->name = $name;
63+
}
64+
public function toString() : string
65+
{
66+
return $this->name;
67+
}
68+
public function equals($other) : bool
69+
{
70+
if (!$other instanceof self) {
71+
return false;
72+
}
73+
return $this->name === $other->name;
74+
}
75+
public function __toString() : string
76+
{
77+
return $this->name;
78+
}
79+
}
80+
EOF;
81+
82+
$this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast)));
83+
}
84+
}

0 commit comments

Comments
 (0)