1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\ResourceGenerator\FileGenerators; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Resource\ResourceInterface; |
6
|
|
|
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; |
7
|
|
|
use Doctrine\Common\Inflector\Inflector; |
8
|
|
|
use PhpParser\BuilderFactory; |
9
|
|
|
use PhpParser\Node; |
10
|
|
|
use function ApiClients\Tools\ResourceGenerator\exists; |
11
|
|
|
|
12
|
|
|
final class InterfaceGenerator implements FileGeneratorInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $yaml; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $uses = [ |
23
|
|
|
ResourceInterface::class => true, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* InterfaceGenerator constructor. |
28
|
|
|
* @param array $yaml |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $yaml) |
31
|
|
|
{ |
32
|
|
|
$this->yaml = $yaml; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function getFilename(): string |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
return $this->yaml['src']['path'] . |
41
|
|
|
DIRECTORY_SEPARATOR . |
42
|
|
|
str_replace('\\', DIRECTORY_SEPARATOR, $this->yaml['class']) . |
43
|
|
|
'Interface.php' |
44
|
|
|
; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return Node |
49
|
|
|
*/ |
50
|
|
|
public function generate(): Node |
51
|
|
|
{ |
52
|
|
|
$classChunks = explode('\\', $this->yaml['class']); |
53
|
|
|
$baseClass = array_pop($classChunks); |
54
|
|
|
$className = $baseClass . 'Interface'; |
55
|
|
|
$namespace = $this->yaml['src']['namespace']; |
56
|
|
View Code Duplication |
if (count($classChunks) > 0) { |
|
|
|
|
57
|
|
|
$namespace .= '\\' . implode('\\', $classChunks); |
58
|
|
|
$namespace = str_replace('\\\\', '\\', $namespace); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$factory = new BuilderFactory(); |
62
|
|
|
|
63
|
|
|
$class = $factory->interface($className) |
64
|
|
|
->extend('ResourceInterface'); |
65
|
|
|
|
66
|
|
|
$class->addStmt( |
67
|
|
|
new Node\Stmt\ClassConst( |
68
|
|
|
[ |
69
|
|
|
new Node\Const_( |
70
|
|
|
'HYDRATE_CLASS', |
71
|
|
|
new Node\Scalar\String_( |
72
|
|
|
$this->yaml['class'] |
73
|
|
|
) |
74
|
|
|
) |
75
|
|
|
] |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
foreach ($this->yaml['properties'] as $name => $details) { |
80
|
|
|
$type = $details; |
81
|
|
|
if (is_array($details)) { |
82
|
|
|
$type = $details['type']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (exists($type)) { |
86
|
|
|
$this->uses[$type] = true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$methodName = Inflector::camelize($name); |
90
|
|
|
if (is_array($details) && isset($details['method'])) { |
91
|
|
|
$methodName = $details['method']; |
92
|
|
|
} |
93
|
|
|
$class->addStmt( |
94
|
|
|
$factory->method($methodName) |
95
|
|
|
->makePublic() |
96
|
|
|
->setReturnType($type) |
97
|
|
|
->setDocComment( |
98
|
|
|
"/**\r\n * @return " . $type . "\r\n */" |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$stmt = $factory->namespace($namespace); |
104
|
|
|
|
105
|
|
|
ksort($this->uses); |
106
|
|
|
foreach ($this->uses as $useClass => $bool) { |
107
|
|
|
$stmt = $stmt |
108
|
|
|
->addStmt($factory->use($useClass)) |
109
|
|
|
; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $stmt->addStmt($class) |
113
|
|
|
->getNode() |
114
|
|
|
; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.