Skip to content

Commit bbb0aa0

Browse files
committed
Refactor: Replace type+typeId with DTO
1 parent 003b186 commit bbb0aa0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+307
-390
lines changed

src/Parser/Ast.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Jerowork\GraphqlAttributeSchema\Parser;
66

77
use Jerowork\GraphqlAttributeSchema\Parser\Node\Node;
8+
use Jerowork\GraphqlAttributeSchema\Parser\Node\Type;
89

910
final readonly class Ast
1011
{
@@ -21,22 +22,19 @@ public function __construct(Node ...$nodes)
2122
/**
2223
* @template T of Node
2324
*
24-
* @param class-string<T> $type
25+
* @param class-string<T> $nodeType
2526
*
2627
* @return list<T>
2728
*/
28-
public function getNodesByType(string $type): array
29+
public function getNodesByNodeType(string $nodeType): array
2930
{
30-
return array_values(array_filter($this->nodes, fn($node) => $node instanceof $type));
31+
return array_values(array_filter($this->nodes, fn($node) => $node instanceof $nodeType));
3132
}
3233

33-
/**
34-
* @param class-string $typeId
35-
*/
36-
public function getNodeByTypeId(string $typeId): ?Node
34+
public function getNodeByType(Type $type): ?Node
3735
{
3836
foreach ($this->nodes as $node) {
39-
if ($node->getTypeId() !== $typeId) {
37+
if (!$node->getType()->equals($type)) {
4038
continue;
4139
}
4240

src/Parser/Node/ArgNode.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@
66

77
final readonly class ArgNode implements Node
88
{
9-
/**
10-
* @param class-string|null $typeId
11-
*/
129
public function __construct(
13-
public ?string $typeId,
14-
public ?string $type,
10+
public Type $type,
1511
public string $name,
1612
public ?string $description,
1713
public bool $isRequired,
1814
public string $propertyName,
1915
) {}
2016

21-
public function getTypeId(): ?string
17+
public function getType(): Type
2218
{
23-
return $this->typeId;
19+
return $this->type;
2420
}
2521
}

src/Parser/Node/EnumNode.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
final readonly class EnumNode implements Node
88
{
99
/**
10-
* @param class-string $typeId
1110
* @param list<string> $cases
1211
*/
1312
public function __construct(
14-
public string $typeId,
13+
public Type $type,
1514
public string $name,
1615
public ?string $description,
1716
public array $cases,
1817
) {}
1918

20-
public function getTypeId(): string
19+
public function getType(): Type
2120
{
22-
return $this->typeId;
21+
return $this->type;
2322
}
2423
}

src/Parser/Node/FieldNode.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
final readonly class FieldNode implements Node
88
{
99
/**
10-
* @param class-string|null $typeId
1110
* @param list<ArgNode> $argNodes
1211
*/
1312
public function __construct(
14-
public ?string $typeId,
15-
public ?string $type,
13+
public Type $type,
1614
public string $name,
1715
public ?string $description,
1816
public bool $isRequired,
@@ -22,8 +20,8 @@ public function __construct(
2220
public ?string $propertyName,
2321
) {}
2422

25-
public function getTypeId(): ?string
23+
public function getType(): Type
2624
{
27-
return $this->typeId;
25+
return $this->type;
2826
}
2927
}

src/Parser/Node/InputTypeNode.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
final readonly class InputTypeNode implements Node
88
{
99
/**
10-
* @param class-string $typeId
1110
* @param list<FieldNode> $fieldNodes
1211
*/
1312
public function __construct(
14-
public string $typeId,
13+
public Type $type,
1514
public string $name,
1615
public ?string $description,
1716
public array $fieldNodes,
1817
) {}
1918

20-
public function getTypeId(): string
19+
public function getType(): Type
2120
{
22-
return $this->typeId;
21+
return $this->type;
2322
}
2423
}

src/Parser/Node/MutationNode.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,20 @@
77
final readonly class MutationNode implements Node
88
{
99
/**
10-
* @param class-string $typeId
1110
* @param list<ArgNode> $argNodes
12-
* @param class-string|null $outputTypeId
1311
*/
1412
public function __construct(
15-
public string $typeId,
13+
public Type $type,
1614
public string $name,
1715
public ?string $description,
1816
public array $argNodes,
19-
public ?string $outputTypeId,
20-
public ?string $outputType,
17+
public Type $outputType,
2118
public bool $isRequired,
2219
public string $methodName,
2320
) {}
2421

25-
public function getTypeId(): string
22+
public function getType(): Type
2623
{
27-
return $this->typeId;
24+
return $this->type;
2825
}
2926
}

src/Parser/Node/Node.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface Node
88
{
9-
public function getTypeId(): ?string;
9+
public function getType(): Type;
1010
}

src/Parser/Node/QueryNode.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,20 @@
77
final readonly class QueryNode implements Node
88
{
99
/**
10-
* @param class-string $typeId
1110
* @param list<ArgNode> $argNodes
12-
* @param class-string|null $outputTypeId
1311
*/
1412
public function __construct(
15-
public string $typeId,
13+
public Type $type,
1614
public string $name,
1715
public ?string $description,
1816
public array $argNodes,
19-
public ?string $outputTypeId,
20-
public ?string $outputType,
17+
public Type $outputType,
2118
public bool $isRequired,
2219
public string $methodName,
2320
) {}
2421

25-
public function getTypeId(): string
22+
public function getType(): Type
2623
{
27-
return $this->typeId;
24+
return $this->type;
2825
}
2926
}

src/Parser/Node/Type.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\GraphqlAttributeSchema\Parser\Node;
6+
7+
final readonly class Type
8+
{
9+
/**
10+
* @param string|class-string $id
11+
*/
12+
public function __construct(
13+
public string $id,
14+
public TypeType $type,
15+
) {}
16+
17+
public static function createScalar(string $name): self
18+
{
19+
return new self($name, TypeType::Scalar);
20+
}
21+
22+
public static function createObject(string $name): self
23+
{
24+
return new self($name, TypeType::Object);
25+
}
26+
27+
public function isScalar(): bool
28+
{
29+
return $this->type === TypeType::Scalar;
30+
}
31+
32+
public function isObject(): bool
33+
{
34+
return $this->type === TypeType::Object;
35+
}
36+
37+
public function equals(Type $type): bool
38+
{
39+
return $this->id === $type->id && $this->type === $type->type;
40+
}
41+
}

src/Parser/Node/TypeNode.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
final readonly class TypeNode implements Node
88
{
99
/**
10-
* @param class-string $typeId
1110
* @param list<FieldNode> $fieldNodes
1211
*/
1312
public function __construct(
14-
public string $typeId,
13+
public Type $type,
1514
public string $name,
1615
public ?string $description,
1716
public array $fieldNodes,
1817
) {}
1918

20-
public function getTypeId(): string
19+
public function getType(): Type
2120
{
22-
return $this->typeId;
21+
return $this->type;
2322
}
2423
}

0 commit comments

Comments
 (0)