1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Abstract builder package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2017 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\AbstractBuilder\Ast; |
11
|
|
|
|
12
|
|
|
use PhpParser\Builder\Class_; |
13
|
|
|
use PhpParser\BuilderFactory; |
14
|
|
|
use PhpParser\Node; |
15
|
|
|
use PhpParser\PrettyPrinter\Standard; |
16
|
|
|
use RunOpenCode\AbstractBuilder\ReflectiveAbstractBuilder; |
17
|
|
|
|
18
|
|
|
class ClassBuilder |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ClassMetadata |
22
|
|
|
*/ |
23
|
|
|
private $builderClass; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ClassMetadata |
27
|
|
|
*/ |
28
|
|
|
private $buildingClass; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var BuilderFactory |
32
|
|
|
*/ |
33
|
|
|
private $factory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Node |
37
|
|
|
*/ |
38
|
|
|
private $namespaceNode; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Class_ |
42
|
|
|
*/ |
43
|
|
|
private $classNode; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Standard |
47
|
|
|
*/ |
48
|
|
|
private $printer; |
49
|
|
|
|
50
|
|
|
public function __construct(ClassMetadata $buildingClass, ClassMetadata $builderClass, array $methods) |
51
|
|
|
{ |
52
|
|
|
$this->buildingClass = $buildingClass; |
53
|
|
|
$this->builderClass = $builderClass; |
54
|
|
|
$this->factory = new BuilderFactory(); |
55
|
|
|
$this->printer = new Standard(); |
56
|
|
|
|
57
|
|
|
$this->namespaceNode = $this->factory->namespace($this->builderClass->getNamespace()); |
|
|
|
|
58
|
|
|
|
59
|
|
|
if (null !== $builderClass->getAst()) { |
60
|
|
|
$this->classNode = $this->builderClass->getAst(); |
|
|
|
|
61
|
|
|
$this->namespaceNode->addStmt($this->classNode); |
|
|
|
|
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->initializeBuilderClass($methods); |
66
|
|
|
|
67
|
|
|
$this->namespaceNode->addStmt($this->classNode->getNode()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function dump() |
71
|
|
|
{ |
72
|
|
|
file_put_contents($this->builderClass->getFilename(), $this->display()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function display() |
76
|
|
|
{ |
77
|
|
|
return $this->printer->prettyPrintFile([$this->namespaceNode->getNode()]); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function create(ClassMetadata $buildingClass, ClassMetadata $builderClass, array $methods) |
81
|
|
|
{ |
82
|
|
|
return new static($buildingClass, $builderClass, $methods); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function initializeBuilderClass($methods) |
86
|
|
|
{ |
87
|
|
|
$this->classNode = $this->factory |
88
|
|
|
->class($this->builderClass->getClass()) |
89
|
|
|
->extend('\\'.ReflectiveAbstractBuilder::class) |
90
|
|
|
->setDocComment(sprintf( |
91
|
|
|
'/** |
92
|
|
|
* Class %s |
93
|
|
|
* |
94
|
|
|
* This class is implementation of builder pattern |
95
|
|
|
* for class %s. |
96
|
|
|
* |
97
|
|
|
* This class is autogenerated by runopencode/abstract-builder library. |
98
|
|
|
* |
99
|
|
|
* @package %s |
100
|
|
|
* |
101
|
|
|
* @see %s |
102
|
|
|
* @see https://en.wikipedia.org/wiki/Builder_pattern |
103
|
|
|
*/', |
104
|
|
|
$this->builderClass->getClass(), $this->buildingClass->getFqcn(), $this->builderClass->getNamespace(), $this->buildingClass->getFqcn())); |
105
|
|
|
|
106
|
|
|
if ($this->buildingClass->isAbstract()) { |
107
|
|
|
$this->classNode->makeAbstract(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($this->buildingClass->isFinal()) { |
111
|
|
|
$this->classNode->makeFinal(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var MethodMetadata $method |
116
|
|
|
*/ |
117
|
|
|
foreach ($methods as $method) { |
118
|
|
|
|
119
|
|
|
$methodFactory = $this->factory |
120
|
|
|
->method($method->getName()) |
121
|
|
|
->makePublic(); |
122
|
|
|
|
123
|
|
|
if ($method->getReturnType()) { |
124
|
|
|
$methodFactory->setReturnType($method->getReturnType() instanceof ClassMetadata ? (string) $method->getReturnType() : $method->getReturnType()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
foreach ($method->getParameters() as $parameter) { |
128
|
|
|
$parameterFactory = $this->factory |
129
|
|
|
->param($parameter->getName()); |
130
|
|
|
|
131
|
|
|
if ($parameter->getType()) { |
132
|
|
|
$parameterFactory->setTypeHint($parameter->getType()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$methodFactory->addParam($parameterFactory->getNode()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->classNode->addStmt($methodFactory->getNode()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$getObjectFqcnMethod = $this->factory->method('getObjectFqcn') |
142
|
|
|
->makeProtected() |
143
|
|
|
->addStmt(new Node\Stmt\Return_(new Node\Scalar\String_($this->buildingClass->getFqcn()))) |
144
|
|
|
->setDocComment(sprintf( |
145
|
|
|
' |
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/' |
149
|
|
|
)); |
150
|
|
|
|
151
|
|
|
$this->classNode->addStmt($getObjectFqcnMethod->getNode()); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..