1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\ResourceGenerator\FileGenerators; |
4
|
|
|
|
5
|
|
|
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; |
6
|
|
|
use PhpParser\BuilderFactory; |
7
|
|
|
use PhpParser\Node; |
8
|
|
|
|
9
|
|
|
abstract class AbstractEmptyExtendingClassGenerator implements FileGeneratorInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $yaml; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* InterfaceGenerator constructor. |
18
|
|
|
* @param array $yaml |
19
|
|
|
*/ |
20
|
1 |
|
public function __construct(array $yaml) |
21
|
|
|
{ |
22
|
1 |
|
$this->yaml = $yaml; |
23
|
1 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
1 |
View Code Duplication |
public function getFilename(): string |
|
|
|
|
29
|
|
|
{ |
30
|
1 |
|
$classChunks = explode('\\', $this->yaml['class']); |
31
|
1 |
|
$className = array_pop($classChunks); |
32
|
1 |
|
$className = 'Empty' . $className; |
33
|
1 |
|
$namespace = ''; |
34
|
1 |
|
if (count($classChunks) > 0) { |
35
|
1 |
|
$namespace .= '\\' . implode('\\', $classChunks); |
36
|
1 |
|
$namespace = str_replace('\\\\', '\\', $namespace); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
return $this->yaml['src']['path'] . |
40
|
1 |
|
DIRECTORY_SEPARATOR . |
41
|
|
|
static::NAMESPACE . |
42
|
1 |
|
DIRECTORY_SEPARATOR . |
43
|
1 |
|
str_replace( |
44
|
1 |
|
'\\', |
45
|
1 |
|
DIRECTORY_SEPARATOR, |
46
|
1 |
|
$namespace . '\\' . $className |
47
|
|
|
) . |
48
|
1 |
|
'.php' |
49
|
|
|
; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Node |
54
|
|
|
*/ |
55
|
1 |
|
public function generate(): Node |
56
|
|
|
{ |
57
|
1 |
|
$classChunks = explode('\\', $this->yaml['class']); |
58
|
1 |
|
$baseClass = array_pop($classChunks); |
59
|
1 |
|
$className = 'Empty' . $baseClass; |
60
|
1 |
|
$baseNamespace = $this->yaml['src']['namespace']; |
61
|
1 |
|
if (count($classChunks) > 0) { |
62
|
1 |
|
$baseNamespace .= '\\' . implode('\\', $classChunks); |
63
|
1 |
|
$baseNamespace = str_replace('\\\\', '\\', $baseNamespace); |
64
|
|
|
} |
65
|
|
|
$namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE; |
66
|
1 |
|
if (count($classChunks) > 0) { |
67
|
1 |
|
$namespace .= '\\' . implode('\\', $classChunks); |
68
|
1 |
|
$namespace = str_replace('\\\\', '\\', $namespace); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$factory = new BuilderFactory(); |
72
|
|
|
|
73
|
1 |
|
$stmt = $factory->namespace($namespace)-> |
74
|
1 |
|
addStmt($factory->use($baseNamespace . '\\' . $className)->as('Base' . $className))-> |
75
|
1 |
|
addStmt( |
76
|
1 |
|
$factory->class($className) |
77
|
1 |
|
->extend('Base' . $className) |
78
|
|
|
) |
79
|
|
|
; |
80
|
|
|
|
81
|
|
|
return $stmt |
82
|
1 |
|
->getNode() |
83
|
|
|
; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.