Skip to content

Commit 8ce7606

Browse files
authored
Clean code generator (#1505)
* Remove unhandled nullable type for namespace parts * Fix variable usage * Fix invalid type annotation * Split the recursion protection from the collection of Method objects This avoids first assigning a boolean in the array before overriding it, which gets rejected by static analysis.
1 parent 65562a9 commit 8ce7606

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

src/Generator/CodeGenerator/TypeGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function generateDocblock(StructureShape $shape, ClassName $shapeClassNam
9999
$classNames[] = $className = $this->namespaceRegistry->getEnum($memberShape);
100100
$param = $className->getName() . '::*';
101101
} else {
102-
$param = $this->getNativePhpType($param);
102+
$param = $this->getNativePhpType($memberShape->getType());
103103
}
104104
}
105105

src/Generator/Naming/NamespaceRegistry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ final class NamespaceRegistry
5151
*/
5252
private $exceptionNamespace;
5353

54-
public function __construct(string $baseNamespace, ?string $inputNamespace = '\\Input', ?string $resultNamespace = '\\Result', ?string $testNamespace = '\\Tests', ?string $enumNamespace = '\\Enum', ?string $objectNamespace = '\\ValueObject', ?string $exceptionNamespace = '\\Exception')
54+
public function __construct(string $baseNamespace, string $inputNamespace = '\\Input', string $resultNamespace = '\\Result', string $testNamespace = '\\Tests', string $enumNamespace = '\\Enum', string $objectNamespace = '\\ValueObject', string $exceptionNamespace = '\\Exception')
5555
{
5656
$this->baseNamespace = $baseNamespace;
5757
$this->inputNamespace = '\\' === $inputNamespace[0] ? $baseNamespace . $inputNamespace : $inputNamespace;

src/Generator/OperationGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class OperationGenerator
7272
private $exceptionGenerator;
7373

7474
/**
75-
* @var ClassName[]
75+
* @var array<string, true>
7676
*/
7777
private $generated = [];
7878

src/Generator/ResponseParser/RestJsonParser.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class RestJsonParser implements Parser
4545
*/
4646
private $functions = [];
4747

48+
/**
49+
* @var array<string, true>
50+
*/
51+
private $generatedFunctions = [];
52+
4853
/**
4954
* @var list<ClassName>
5055
*/
@@ -65,6 +70,7 @@ public function generate(StructureShape $shape, bool $throwOnError = true): Pars
6570

6671
$properties = [];
6772
$this->functions = [];
73+
$this->generatedFunctions = [];
6874
$this->imports = [];
6975
foreach ($shape->getMembers() as $member) {
7076
if (\in_array($member->getLocation(), ['header', 'headers'])) {
@@ -205,9 +211,9 @@ private function parseElement(string $input, Shape $shape, bool $required, bool
205211
private function parseResponseStructure(StructureShape $shape, string $input, bool $required): string
206212
{
207213
$functionName = 'populateResult' . ucfirst($shape->getName());
208-
if (!isset($this->functions[$functionName])) {
214+
if (!isset($this->generatedFunctions[$functionName])) {
209215
// prevent recursion
210-
$this->functions[$functionName] = true;
216+
$this->generatedFunctions[$functionName] = true;
211217

212218
$properties = [];
213219
foreach ($shape->getMembers() as $member) {
@@ -287,9 +293,9 @@ private function parseResponseList(ListShape $shape, string $input, bool $requir
287293
{
288294
$shapeMember = $shape->getMember();
289295
$functionName = 'populateResult' . ucfirst($shape->getName());
290-
if (!isset($this->functions[$functionName])) {
296+
if (!isset($this->generatedFunctions[$functionName])) {
291297
// prevent recursion
292-
$this->functions[$functionName] = true;
298+
$this->generatedFunctions[$functionName] = true;
293299

294300
if ($shapeMember->getShape() instanceof StructureShape || $shapeMember->getShape() instanceof ListShape || $shapeMember->getShape() instanceof MapShape) {
295301
$listAccessorRequired = true;
@@ -332,9 +338,9 @@ private function parseResponseMap(MapShape $shape, string $input, bool $required
332338
{
333339
$shapeValue = $shape->getValue();
334340
$functionName = 'populateResult' . ucfirst($shape->getName());
335-
if (!isset($this->functions[$functionName])) {
341+
if (!isset($this->generatedFunctions[$functionName])) {
336342
// prevent recursion
337-
$this->functions[$functionName] = true;
343+
$this->generatedFunctions[$functionName] = true;
338344

339345
if (null === $locationName = $shape->getKey()->getLocationName()) {
340346
// We need to use array keys

0 commit comments

Comments
 (0)