Skip to content

Commit 4150b61

Browse files
committed
Delegate typed option to method generators
1 parent 041daf8 commit 4150b61

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"require": {
3535
"php": "^7.4 || ^8.0",
3636
"open-code-modeling/json-schema-to-php": "dev-master",
37-
"open-code-modeling/php-code-ast": "^0.2.1|^0.3.0"
37+
"open-code-modeling/php-code-ast": "^0.3.0|^0.4.0|dev-master"
3838
},
3939
"require-dev": {
4040
"jangregor/phpstan-prophecy": "^0.8.0",

src/ValueObject/BooleanFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ final class BooleanFactory
5959
{
6060
private Parser $parser;
6161
private PropertyFactory $propertyFactory;
62+
private bool $typed;
6263

6364
public function __construct(Parser $parser, bool $typed)
6465
{
6566
$this->parser = $parser;
67+
$this->typed = $typed;
6668
$this->propertyFactory = new PropertyFactory($typed);
6769
}
6870

@@ -103,6 +105,7 @@ public function methodFromBool(string $argumentName): NodeVisitor
103105
MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PUBLIC,
104106
new BodyGenerator($this->parser, 'return new self($' . $argumentName . ');')
105107
);
108+
$method->setTyped($this->typed);
106109
$method->setReturnType('self');
107110

108111
return new ClassMethod($method);
@@ -118,6 +121,7 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor
118121
MethodGenerator::FLAG_PRIVATE,
119122
new BodyGenerator($this->parser, \sprintf('$this->%s = $%s;', $argumentName, $argumentName))
120123
);
124+
$method->setTyped($this->typed);
121125

122126
return new ClassMethod($method);
123127
}
@@ -130,6 +134,7 @@ public function methodToBool(string $argumentName): NodeVisitor
130134
MethodGenerator::FLAG_PUBLIC,
131135
new BodyGenerator($this->parser, 'return $this->' . $argumentName . ';')
132136
);
137+
$method->setTyped($this->typed);
133138
$method->setReturnType('bool');
134139

135140
return new ClassMethod($method);
@@ -153,6 +158,7 @@ public function methodEquals(string $propertyName, string $argumentName = 'other
153158
MethodGenerator::FLAG_PUBLIC,
154159
new BodyGenerator($this->parser, $body)
155160
);
161+
$method->setTyped($this->typed);
156162
$method->setReturnType('bool');
157163

158164
return new ClassMethod($method);
@@ -166,6 +172,7 @@ public function methodMagicToString(string $argumentName): NodeVisitor
166172
MethodGenerator::FLAG_PUBLIC,
167173
new BodyGenerator($this->parser, 'return $this->' . $argumentName . " ? 'TRUE' : 'FALSE';")
168174
);
175+
$method->setTyped($this->typed);
169176
$method->setReturnType('string');
170177

171178
return new ClassMethod($method);

src/ValueObject/DateTimeFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ final class DateTimeFactory
8787
{
8888
private Parser $parser;
8989
private PropertyFactory $propertyFactory;
90+
private bool $typed;
9091

9192
public function __construct(Parser $parser, bool $typed)
9293
{
9394
$this->parser = $parser;
95+
$this->typed = $typed;
9496
$this->propertyFactory = new PropertyFactory($typed);
9597
}
9698

@@ -149,6 +151,7 @@ public function methodFromDateTime(string $argumentName): NodeVisitor
149151
MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PUBLIC,
150152
new BodyGenerator($this->parser, 'return new self(self::ensureUtc($' . $argumentName . '));')
151153
);
154+
$method->setTyped($this->typed);
152155
$method->setReturnType('self');
153156

154157
return new ClassMethod($method);
@@ -176,6 +179,7 @@ public function methodFromString(string $argumentName): NodeVisitor
176179
MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PUBLIC,
177180
new BodyGenerator($this->parser, $body)
178181
);
182+
$method->setTyped($this->typed);
179183
$method->setReturnType('self');
180184

181185
return new ClassMethod($method);
@@ -191,6 +195,7 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor
191195
MethodGenerator::FLAG_PRIVATE,
192196
new BodyGenerator($this->parser, \sprintf('$this->%s = $%s;', $argumentName, $argumentName))
193197
);
198+
$method->setTyped($this->typed);
194199

195200
return new ClassMethod($method);
196201
}
@@ -216,6 +221,7 @@ public function methodDateTime(string $argumentName): NodeVisitor
216221
MethodGenerator::FLAG_PUBLIC,
217222
new BodyGenerator($this->parser, 'return $this->' . $argumentName . ';')
218223
);
224+
$method->setTyped($this->typed);
219225
$method->setReturnType('DateTimeImmutable');
220226

221227
return new ClassMethod($method);
@@ -229,6 +235,7 @@ public function methodMagicToString(): NodeVisitor
229235
MethodGenerator::FLAG_PUBLIC,
230236
new BodyGenerator($this->parser, 'return $this->toString();')
231237
);
238+
$method->setTyped($this->typed);
232239
$method->setReturnType('string');
233240

234241
return new ClassMethod($method);
@@ -253,6 +260,7 @@ public function methodEnsureUtc(string $argumentName): NodeVisitor
253260
MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PRIVATE,
254261
new BodyGenerator($this->parser, $body)
255262
);
263+
$method->setTyped($this->typed);
256264
$method->setReturnType('DateTimeImmutable');
257265

258266
return new ClassMethod($method);

src/ValueObject/IntegerFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ final class IntegerFactory
5959
{
6060
private Parser $parser;
6161
private PropertyFactory $propertyFactory;
62+
private bool $typed;
6263

6364
public function __construct(Parser $parser, bool $typed)
6465
{
6566
$this->parser = $parser;
67+
$this->typed = $typed;
6668
$this->propertyFactory = new PropertyFactory($typed);
6769
}
6870

@@ -103,6 +105,7 @@ public function methodFromInt(string $argumentName): NodeVisitor
103105
MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PUBLIC,
104106
new BodyGenerator($this->parser, 'return new self($' . $argumentName . ');')
105107
);
108+
$method->setTyped($this->typed);
106109
$method->setReturnType('self');
107110

108111
return new ClassMethod($method);
@@ -118,6 +121,7 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor
118121
MethodGenerator::FLAG_PRIVATE,
119122
new BodyGenerator($this->parser, \sprintf('$this->%s = $%s;', $argumentName, $argumentName))
120123
);
124+
$method->setTyped($this->typed);
121125

122126
return new ClassMethod($method);
123127
}
@@ -130,6 +134,7 @@ public function methodToInt(string $argumentName): NodeVisitor
130134
MethodGenerator::FLAG_PUBLIC,
131135
new BodyGenerator($this->parser, 'return $this->' . $argumentName . ';')
132136
);
137+
$method->setTyped($this->typed);
133138
$method->setReturnType('int');
134139

135140
return new ClassMethod($method);
@@ -153,6 +158,7 @@ public function methodEquals(string $propertyName, string $argumentName = 'other
153158
MethodGenerator::FLAG_PUBLIC,
154159
new BodyGenerator($this->parser, $body)
155160
);
161+
$method->setTyped($this->typed);
156162
$method->setReturnType('bool');
157163

158164
return new ClassMethod($method);
@@ -166,6 +172,7 @@ public function methodMagicToString(string $argumentName): NodeVisitor
166172
MethodGenerator::FLAG_PUBLIC,
167173
new BodyGenerator($this->parser, 'return (string)$this->' . $argumentName . ';')
168174
);
175+
$method->setTyped($this->typed);
169176
$method->setReturnType('string');
170177

171178
return new ClassMethod($method);

src/ValueObject/NumberFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ final class NumberFactory
5959
{
6060
private Parser $parser;
6161
private PropertyFactory $propertyFactory;
62+
private bool $typed;
6263

6364
public function __construct(Parser $parser, bool $typed)
6465
{
6566
$this->parser = $parser;
67+
$this->typed = $typed;
6668
$this->propertyFactory = new PropertyFactory($typed);
6769
}
6870

@@ -103,6 +105,7 @@ public function methodFromFloat(string $argumentName): NodeVisitor
103105
MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PUBLIC,
104106
new BodyGenerator($this->parser, 'return new self($' . $argumentName . ');')
105107
);
108+
$method->setTyped($this->typed);
106109
$method->setReturnType('self');
107110

108111
return new ClassMethod($method);
@@ -118,6 +121,7 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor
118121
MethodGenerator::FLAG_PRIVATE,
119122
new BodyGenerator($this->parser, \sprintf('$this->%s = $%s;', $argumentName, $argumentName))
120123
);
124+
$method->setTyped($this->typed);
121125

122126
return new ClassMethod($method);
123127
}
@@ -130,6 +134,7 @@ public function methodToFloat(string $argumentName): NodeVisitor
130134
MethodGenerator::FLAG_PUBLIC,
131135
new BodyGenerator($this->parser, 'return $this->' . $argumentName . ';')
132136
);
137+
$method->setTyped($this->typed);
133138
$method->setReturnType('float');
134139

135140
return new ClassMethod($method);
@@ -153,6 +158,7 @@ public function methodEquals(string $propertyName, string $argumentName = 'other
153158
MethodGenerator::FLAG_PUBLIC,
154159
new BodyGenerator($this->parser, $body)
155160
);
161+
$method->setTyped($this->typed);
156162
$method->setReturnType('bool');
157163

158164
return new ClassMethod($method);
@@ -166,6 +172,7 @@ public function methodMagicToString(string $argumentName): NodeVisitor
166172
MethodGenerator::FLAG_PUBLIC,
167173
new BodyGenerator($this->parser, 'return (string)$this->' . $argumentName . ';')
168174
);
175+
$method->setTyped($this->typed);
169176
$method->setReturnType('string');
170177

171178
return new ClassMethod($method);

src/ValueObject/StringFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ final class StringFactory
5959
{
6060
private Parser $parser;
6161
private PropertyFactory $propertyFactory;
62+
private bool $typed;
6263

6364
public function __construct(Parser $parser, bool $typed)
6465
{
6566
$this->parser = $parser;
67+
$this->typed = $typed;
6668
$this->propertyFactory = new PropertyFactory($typed);
6769
}
6870

@@ -103,6 +105,7 @@ public function methodFromString(string $argumentName): NodeVisitor
103105
MethodGenerator::FLAG_STATIC | MethodGenerator::FLAG_PUBLIC,
104106
new BodyGenerator($this->parser, 'return new self($' . $argumentName . ');')
105107
);
108+
$method->setTyped($this->typed);
106109
$method->setReturnType('self');
107110

108111
return new ClassMethod($method);
@@ -118,6 +121,7 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor
118121
MethodGenerator::FLAG_PRIVATE,
119122
new BodyGenerator($this->parser, \sprintf('$this->%s = $%s;', $argumentName, $argumentName))
120123
);
124+
$method->setTyped($this->typed);
121125

122126
return new ClassMethod($method);
123127
}
@@ -130,6 +134,7 @@ public function methodToString(string $argumentName): NodeVisitor
130134
MethodGenerator::FLAG_PUBLIC,
131135
new BodyGenerator($this->parser, 'return $this->' . $argumentName . ';')
132136
);
137+
$method->setTyped($this->typed);
133138
$method->setReturnType('string');
134139

135140
return new ClassMethod($method);
@@ -153,6 +158,7 @@ public function methodEquals(string $propertyName, string $argumentName = 'other
153158
MethodGenerator::FLAG_PUBLIC,
154159
new BodyGenerator($this->parser, $body)
155160
);
161+
$method->setTyped($this->typed);
156162
$method->setReturnType('bool');
157163

158164
return new ClassMethod($method);
@@ -166,6 +172,7 @@ public function methodMagicToString(string $argumentName): NodeVisitor
166172
MethodGenerator::FLAG_PUBLIC,
167173
new BodyGenerator($this->parser, 'return $this->' . $argumentName . ';')
168174
);
175+
$method->setTyped($this->typed);
169176
$method->setReturnType('string');
170177

171178
return new ClassMethod($method);

0 commit comments

Comments
 (0)