1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\ResourceGenerator\FileGenerators; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand; |
6
|
|
|
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; |
7
|
|
|
use PhpParser\BuilderFactory; |
8
|
|
|
use PhpParser\Node; |
9
|
|
|
|
10
|
|
|
final class SyncClassGenerator extends AbstractExtendingClassGenerator implements FileGeneratorInterface |
11
|
|
|
{ |
12
|
|
|
const NAMESPACE = 'Sync'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @return Node |
16
|
|
|
*/ |
17
|
1 |
|
public function generate(): Node |
18
|
|
|
{ |
19
|
1 |
|
$classChunks = explode('\\', $this->yaml['class']); |
20
|
1 |
|
$className = array_pop($classChunks); |
21
|
|
|
$namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE; |
22
|
1 |
View Code Duplication |
if (count($classChunks) > 0) { |
|
|
|
|
23
|
1 |
|
$namespace .= '\\' . implode('\\', $classChunks); |
24
|
1 |
|
$namespace = str_replace('\\\\', '\\', $namespace); |
25
|
|
|
} |
26
|
1 |
|
$baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class']; |
27
|
|
|
|
28
|
1 |
|
$factory = new BuilderFactory(); |
29
|
|
|
|
30
|
1 |
|
$class = $factory->class($className) |
31
|
1 |
|
->extend('Base' . $className); |
32
|
|
|
|
33
|
1 |
|
$class->addStmt($factory->method('refresh') |
34
|
1 |
|
->makePublic() |
35
|
1 |
|
->setReturnType($className) |
36
|
1 |
|
->addStmt( |
37
|
1 |
|
new Node\Stmt\Return_( |
38
|
1 |
|
new Node\Expr\MethodCall( |
39
|
1 |
|
new Node\Expr\Variable('this'), |
40
|
1 |
|
'wait', |
41
|
|
|
[ |
|
|
|
|
42
|
1 |
|
new Node\Expr\MethodCall( |
43
|
1 |
|
new Node\Expr\Variable('this'), |
44
|
1 |
|
'handleCommand', |
45
|
|
|
[ |
|
|
|
|
46
|
1 |
|
new Node\Expr\New_( |
47
|
1 |
|
new Node\Name('BuildAsyncFromSyncCommand'), |
48
|
|
|
[ |
|
|
|
|
49
|
1 |
|
new Node\Scalar\String_($this->yaml['class']), |
50
|
1 |
|
new Node\Expr\Variable('this'), |
51
|
|
|
] |
52
|
|
|
), |
53
|
|
|
] |
54
|
|
|
), |
55
|
|
|
] |
56
|
|
|
) |
57
|
|
|
) |
58
|
|
|
)); |
59
|
|
|
|
60
|
1 |
|
return $factory->namespace($namespace) |
61
|
1 |
|
->addStmt($factory->use(BuildAsyncFromSyncCommand::class)) |
62
|
1 |
|
->addStmt($factory->use($baseClass)->as('Base' . $className)) |
63
|
1 |
|
->addStmt($class) |
64
|
1 |
|
->getNode() |
65
|
|
|
; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
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.