Total Complexity | 51 |
Total Lines | 322 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
Complex classes like Service often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Service, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Service extends AbstractModelFile |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | const METHOD_SET_HEADER_PREFIX = 'setSoapHeader'; |
||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | const PARAM_SET_HEADER_NAMESPACE = 'nameSpace'; |
||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | const PARAM_SET_HEADER_MUSTUNDERSTAND = 'mustUnderstand'; |
||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | const PARAM_SET_HEADER_ACTOR = 'actor'; |
||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | const METHOD_GET_RESULT = 'getResult'; |
||
46 | /** |
||
47 | * Method model can't be found in case the original method's name is unclean: |
||
48 | * - ex: my.operation.name becomes my_operation_name |
||
49 | * thus the Model from Model\Service::getMethod() can't be found |
||
50 | * So we store the generated name associated to the original method object |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $methodNames = []; |
||
54 | /** |
||
55 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassConstants() |
||
56 | */ |
||
57 | protected function getClassConstants(ConstantContainer $constants) |
||
58 | { |
||
59 | } |
||
60 | /** |
||
61 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getConstantAnnotationBlock() |
||
62 | */ |
||
63 | protected function getConstantAnnotationBlock(PhpConstant $constant) |
||
64 | { |
||
65 | } |
||
66 | /** |
||
67 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassProperties() |
||
68 | */ |
||
69 | protected function getClassProperties(PropertyContainer $properties) |
||
70 | { |
||
71 | } |
||
72 | /** |
||
73 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getPropertyAnnotationBlock() |
||
74 | */ |
||
75 | protected function getPropertyAnnotationBlock(PhpProperty $property) |
||
76 | { |
||
77 | } |
||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | protected function getClassDeclarationLineText() |
||
82 | { |
||
83 | return $this->getGenerator()->getOptionGatherMethods() === GeneratorOptions::VALUE_NONE ? 'This class stands for all operations' : parent::getClassDeclarationLineText(); |
||
84 | } |
||
85 | /** |
||
86 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::fillClassMethods() |
||
87 | */ |
||
88 | protected function fillClassMethods() |
||
89 | { |
||
90 | $this |
||
91 | ->addSoapHeaderMethods() |
||
92 | ->addOperationsMethods() |
||
93 | ->addGetResultMethod(); |
||
94 | } |
||
95 | /** |
||
96 | * @return Service |
||
97 | */ |
||
98 | protected function addSoapHeaderMethods() |
||
99 | { |
||
100 | foreach ($this->getModel()->getMethods() as $method) { |
||
101 | $this->addSoapHeaderFromMethod($method); |
||
102 | } |
||
103 | return $this; |
||
104 | } |
||
105 | /** |
||
106 | * @param MethodModel $method |
||
107 | * @return Service |
||
108 | */ |
||
109 | protected function addSoapHeaderFromMethod(MethodModel $method) |
||
110 | { |
||
111 | $soapHeaderNames = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMES, []); |
||
112 | $soapHeaderNamespaces = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMESPACES, []); |
||
113 | $soapHeaderTypes = $method->getMetaValue(TagHeader::META_SOAP_HEADER_TYPES, []); |
||
114 | if (is_array($soapHeaderNames) && is_array($soapHeaderNamespaces) && is_array($soapHeaderTypes)) { |
||
115 | foreach ($soapHeaderNames as $index => $soapHeaderName) { |
||
116 | $methodName = $this->getSoapHeaderMethodName($soapHeaderName); |
||
117 | if ($this->methods->get($methodName) === null) { |
||
118 | $soapHeaderNamespace = array_key_exists($index, $soapHeaderNamespaces) ? $soapHeaderNamespaces[$index] : null; |
||
119 | $soapHeaderType = array_key_exists($index, $soapHeaderTypes) ? $soapHeaderTypes[$index] : null; |
||
120 | $this->methods->add($this->getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, $soapHeaderType)); |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | return $this; |
||
125 | } |
||
126 | /** |
||
127 | * @param string $methodName |
||
128 | * @param string $soapHeaderName |
||
129 | * @param string $soapHeaderNamespace |
||
130 | * @param string $soapHeaderType |
||
131 | * @return PhpMethod |
||
132 | */ |
||
133 | protected function getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, $soapHeaderType) |
||
134 | { |
||
135 | try { |
||
136 | $method = new PhpMethod($methodName, [ |
||
137 | $firstParameter = new PhpFunctionParameter(lcfirst($soapHeaderName), PhpFunctionParameterBase::NO_VALUE, $this->getTypeFromName($soapHeaderType)), |
||
138 | new PhpFunctionParameterBase(self::PARAM_SET_HEADER_NAMESPACE, $soapHeaderNamespace), |
||
139 | new PhpFunctionParameterBase(self::PARAM_SET_HEADER_MUSTUNDERSTAND, false), |
||
140 | new PhpFunctionParameterBase(self::PARAM_SET_HEADER_ACTOR, null), |
||
141 | ]); |
||
142 | $model = $this->getModelByName($soapHeaderType); |
||
143 | if ($model instanceof StructModel) { |
||
144 | $rules = new Rules($this, $method, new StructAttributeModel($model->getGenerator(), $soapHeaderType, $model->getName(), $model), $this->methods); |
||
145 | $rules->applyRules(lcfirst($soapHeaderName)); |
||
146 | $firstParameter->setModel($model); |
||
147 | } |
||
148 | $method->addChild(sprintf('return $this->%s($%s, \'%s\', $%s, $%s, $%s);', self::METHOD_SET_HEADER_PREFIX, self::PARAM_SET_HEADER_NAMESPACE, $soapHeaderName, lcfirst($soapHeaderName), self::PARAM_SET_HEADER_MUSTUNDERSTAND, self::PARAM_SET_HEADER_ACTOR)); |
||
149 | } catch (\InvalidArgumentException $exception) { |
||
150 | throw new \InvalidArgumentException(sprintf('Unable to create function parameter for service "%s" with type "%s"', $this->getModel()->getName(), var_export($this->getTypeFromName($soapHeaderName), true)), __LINE__, $exception); |
||
151 | } |
||
152 | return $method; |
||
153 | } |
||
154 | /** |
||
155 | * @param string $name |
||
156 | * @return string |
||
157 | */ |
||
158 | protected function getTypeFromName($name) |
||
159 | { |
||
160 | return self::getValidType($this->getStructAttributeTypeAsPhpType(new StructAttributeModel($this->generator, 'any', $name)), $this->getGenerator()->getOptionXsdTypesPath()); |
||
161 | } |
||
162 | /** |
||
163 | * @param string $soapHeaderName |
||
164 | * @return string |
||
165 | */ |
||
166 | protected function getSoapHeaderMethodName($soapHeaderName) |
||
167 | { |
||
168 | return sprintf('%s%s', self::METHOD_SET_HEADER_PREFIX, ucfirst($soapHeaderName)); |
||
169 | } |
||
170 | /** |
||
171 | * @return Service |
||
172 | */ |
||
173 | protected function addOperationsMethods() |
||
174 | { |
||
175 | foreach ($this->getModel()->getMethods() as $method) { |
||
176 | $this->addMainMethod($method); |
||
177 | } |
||
178 | return $this; |
||
179 | } |
||
180 | /** |
||
181 | * @return Service |
||
182 | */ |
||
183 | protected function addGetResultMethod() |
||
184 | { |
||
185 | $method = new PhpMethod(self::METHOD_GET_RESULT); |
||
186 | $method->addChild('return parent::getResult();'); |
||
187 | $this->methods->add($method); |
||
188 | return $this; |
||
189 | } |
||
190 | /** |
||
191 | * @param MethodModel $method |
||
192 | * @return Service |
||
193 | */ |
||
194 | protected function addMainMethod(MethodModel $method) |
||
195 | { |
||
196 | $methodFile = new Operation($method, $this->getGenerator()); |
||
197 | $mainMethod = $methodFile->getMainMethod(); |
||
198 | $this->methods->add($mainMethod); |
||
199 | $this->setModelFromMethod($mainMethod, $method); |
||
200 | return $this; |
||
201 | } |
||
202 | /** |
||
203 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getMethodAnnotationBlock() |
||
204 | */ |
||
205 | protected function getMethodAnnotationBlock(PhpMethod $method) |
||
206 | { |
||
207 | $annotationBlock = new PhpAnnotationBlock(); |
||
208 | if (mb_stripos($method->getName(), self::METHOD_SET_HEADER_PREFIX) === 0) { |
||
209 | $this->addAnnotationBlockForSoapHeaderMethod($annotationBlock, $method); |
||
210 | } elseif ($method->getName() === self::METHOD_GET_RESULT) { |
||
211 | $this->addAnnotationBlockForgetResultMethod($annotationBlock); |
||
212 | } else { |
||
213 | $this->addAnnotationBlockForOperationMethod($annotationBlock, $method); |
||
214 | } |
||
215 | return $annotationBlock; |
||
216 | } |
||
217 | /** |
||
218 | * @param PhpAnnotationBlock $annotationBlock |
||
219 | * @param PhpMethod $method |
||
220 | * @return Service |
||
221 | */ |
||
222 | protected function addAnnotationBlockForSoapHeaderMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method) |
||
223 | { |
||
224 | $methodParameters = $method->getParameters(); |
||
225 | $firstParameter = array_shift($methodParameters); |
||
226 | if ($firstParameter instanceof PhpFunctionParameter) { |
||
227 | $annotationBlock->addChild(sprintf('Sets the %s SoapHeader param', ucfirst($firstParameter->getName()))); |
||
228 | $firstParameterType = $firstParameter->getType(); |
||
229 | if ($firstParameter->getModel() instanceof StructModel) { |
||
230 | $firstParameterType = $this->getStructAttributeTypeAsPhpType(new StructAttributeModel($firstParameter->getModel()->getGenerator(), $firstParameter->getName(), $firstParameter->getModel()->getName(), $firstParameter->getModel())); |
||
231 | if ($firstParameter->getModel()->isRestriction()) { |
||
232 | $annotationBlock |
||
233 | ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID))) |
||
234 | ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES))) |
||
235 | ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException')); |
||
236 | } |
||
237 | } |
||
238 | $annotationBlock |
||
239 | ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::setSoapHeader()', $this->getModel()->getExtends(true)))) |
||
240 | ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $firstParameterType, $firstParameter->getName()))) |
||
|
|||
241 | ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('string $%s', self::PARAM_SET_HEADER_NAMESPACE))) |
||
242 | ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('bool $%s', self::PARAM_SET_HEADER_MUSTUNDERSTAND))) |
||
243 | ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('string $%s', self::PARAM_SET_HEADER_ACTOR))) |
||
244 | ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, 'bool')); |
||
245 | } |
||
246 | return $this; |
||
247 | } |
||
248 | /** |
||
249 | * @param PhpAnnotationBlock $annotationBlock |
||
250 | * @param PhpMethod $method |
||
251 | * @return Service |
||
252 | */ |
||
253 | protected function addAnnotationBlockForOperationMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method) |
||
254 | { |
||
255 | if (($model = $this->getModelFromMethod($method)) instanceof MethodModel) { |
||
256 | $operationAnnotationBlock = new OperationAnnotationBlock($model, $this->getGenerator()); |
||
257 | $operationAnnotationBlock->addAnnotationBlockForOperationMethod($annotationBlock); |
||
258 | } |
||
259 | return $this; |
||
260 | } |
||
261 | /** |
||
262 | * @param PhpAnnotationBlock $annotationBlock |
||
263 | * @return Service |
||
264 | */ |
||
265 | protected function addAnnotationBlockForgetResultMethod(PhpAnnotationBlock $annotationBlock) |
||
266 | { |
||
267 | $annotationBlock |
||
268 | ->addChild('Returns the result')->addChild(new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::getResult()', $this->getModel()->getExtends(true)))) |
||
269 | ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getServiceReturnTypes())); |
||
270 | return $this; |
||
271 | } |
||
272 | /** |
||
273 | * @return string |
||
274 | */ |
||
275 | protected function getServiceReturnTypes() |
||
283 | } |
||
284 | /** |
||
285 | * @param MethodModel $method |
||
286 | * @return string |
||
287 | */ |
||
288 | public static function getOperationMethodReturnType(MethodModel $method, Generator $generator) |
||
289 | { |
||
290 | $returnType = $method->getReturnType(); |
||
291 | if ((($struct = $generator->getStructByName($returnType)) instanceof StructModel) && !$struct->isRestriction()) { |
||
292 | if ($struct->isStruct()) { |
||
293 | $returnType = $struct->getPackagedName(true); |
||
294 | } elseif ($struct->isArray()) { |
||
295 | if (($structInheritance = $struct->getInheritanceStruct()) instanceof StructModel) { |
||
296 | $returnType = sprintf('%s[]', $structInheritance->getPackagedName(true)); |
||
297 | } else { |
||
298 | $returnType = $struct->getInheritance(); |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | return $returnType; |
||
303 | } |
||
304 | /** |
||
305 | * @param PhpMethod $method |
||
306 | * @return MethodModel|null |
||
307 | */ |
||
308 | protected function getModelFromMethod(PhpMethod $method) |
||
309 | { |
||
310 | $model = $this->getGenerator()->getServiceMethod($method->getName()); |
||
311 | if (!$model instanceof MethodModel) { |
||
312 | $model = array_key_exists($method->getName(), $this->methodNames) ? $this->methodNames[$method->getName()] : null; |
||
313 | } |
||
314 | return $model; |
||
315 | } |
||
316 | /** |
||
317 | * @param PhpMethod $phpMethod |
||
318 | * @param MethodModel $methodModel |
||
319 | * @return Service |
||
320 | */ |
||
321 | protected function setModelFromMethod(PhpMethod $phpMethod, MethodModel $methodModel) |
||
322 | { |
||
323 | $this->methodNames[$phpMethod->getName()] = $methodModel; |
||
324 | return $this; |
||
325 | } |
||
326 | /** |
||
327 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getModel() |
||
328 | * @return ServiceModel |
||
329 | */ |
||
330 | public function getModel() |
||
333 | } |
||
334 | /** |
||
335 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel() |
||
336 | * @throws \InvalidArgumentException |
||
337 | * @param AbstractModel $model |
||
338 | * @return Service |
||
339 | */ |
||
340 | public function setModel(AbstractModel $model) |
||
341 | { |
||
342 | if (!$model instanceof ServiceModel) { |
||
343 | throw new \InvalidArgumentException('Model must be an instance of a Service', __LINE__); |
||
344 | } |
||
345 | return parent::setModel($model); |
||
346 | } |
||
347 | } |
||
348 |