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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 25 | class Service extends AbstractModelFile |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | const METHOD_SET_HEADER_PREFIX = 'setSoapHeader'; |
||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | const PARAM_SET_HEADER_NAMESPACE = 'nameSpace'; |
||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const PARAM_SET_HEADER_MUSTUNDERSTAND = 'mustUnderstand'; |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | const PARAM_SET_HEADER_ACTOR = 'actor'; |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | const METHOD_GET_RESULT = 'getResult'; |
||
| 47 | /** |
||
| 48 | * Method model can't be found in case the original method's name is unclean: |
||
| 49 | * - ex: my.operation.name becomes my_operation_name |
||
| 50 | * thus the Model from Model\Service::getMethod() can't be found |
||
| 51 | * So we store the generated name associated to the original method object |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $methods = array(); |
||
| 55 | /** |
||
| 56 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassConstants() |
||
| 57 | */ |
||
| 58 | 115 | protected function getClassConstants(ConstantContainer $constants) |
|
| 59 | { |
||
| 60 | 115 | } |
|
| 61 | /** |
||
| 62 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getConstantAnnotationBlock() |
||
| 63 | */ |
||
| 64 | protected function getConstantAnnotationBlock(PhpConstant $constant) |
||
| 65 | { |
||
| 66 | } |
||
| 67 | /** |
||
| 68 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassProperties() |
||
| 69 | */ |
||
| 70 | 115 | protected function getClassProperties(PropertyContainer $properties) |
|
| 71 | { |
||
| 72 | 115 | } |
|
| 73 | /** |
||
| 74 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getPropertyAnnotationBlock() |
||
| 75 | */ |
||
| 76 | protected function getPropertyAnnotationBlock(PhpProperty $property) |
||
| 77 | { |
||
| 78 | } |
||
| 79 | /** |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | 115 | protected function getClassDeclarationLineText() |
|
| 83 | { |
||
| 84 | 115 | return $this->getGenerator()->getOptionGatherMethods() === GeneratorOptions::VALUE_NONE ? 'This class stands for all operations' : parent::getClassDeclarationLineText(); |
|
| 85 | } |
||
| 86 | /** |
||
| 87 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassMethods() |
||
| 88 | */ |
||
| 89 | 115 | protected function getClassMethods(MethodContainer $methods) |
|
| 90 | { |
||
| 91 | 115 | $this->addSoapHeaderMethods($methods)->addOperationsMethods($methods)->addGetResultMethod($methods); |
|
| 92 | 115 | } |
|
| 93 | /** |
||
| 94 | * @param MethodContainer $methods |
||
| 95 | * @return Service |
||
| 96 | */ |
||
| 97 | 115 | protected function addSoapHeaderMethods(MethodContainer $methods) |
|
| 104 | /** |
||
| 105 | * @param MethodContainer $methods |
||
| 106 | * @param MethodModel $method |
||
| 107 | * @return Service |
||
| 108 | */ |
||
| 109 | 115 | protected function addSoapHeaderFromMethod(MethodContainer $methods, MethodModel $method) |
|
| 124 | /** |
||
| 125 | * @param string $methodName |
||
| 126 | * @param string $soapHeaderName |
||
| 127 | * @param string $soapHeaderNamespace |
||
| 128 | * @param string $soapHeaderType |
||
| 129 | * @return PhpMethod |
||
| 130 | */ |
||
| 131 | 30 | protected function getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, $soapHeaderType) |
|
| 152 | /** |
||
| 153 | * @param string $name |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | 30 | protected function getTypeFromName($name) |
|
| 169 | /** |
||
| 170 | * @param string $soapHeaderName |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | 30 | protected function getSoapHeaderMethodName($soapHeaderName) |
|
| 177 | /** |
||
| 178 | * @param MethodContainer $methods |
||
| 179 | * @return Service |
||
| 180 | */ |
||
| 181 | 115 | protected function addOperationsMethods(MethodContainer $methods) |
|
| 188 | /** |
||
| 189 | * @param MethodContainer $methods |
||
| 190 | * @return Service |
||
| 191 | */ |
||
| 192 | 115 | protected function addGetResultMethod(MethodContainer $methods) |
|
| 199 | /** |
||
| 200 | * @param MethodContainer $methods |
||
| 201 | * @param MethodModel $method |
||
| 202 | * @return Service |
||
| 203 | */ |
||
| 204 | 115 | protected function addMainMethod(MethodContainer $methods, MethodModel $method) |
|
| 212 | /** |
||
| 213 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getMethodAnnotationBlock() |
||
| 214 | */ |
||
| 215 | 115 | protected function getMethodAnnotationBlock(PhpMethod $method) |
|
| 227 | /** |
||
| 228 | * @param PhpAnnotationBlock $annotationBlock |
||
| 229 | * @param PhpMethod $method |
||
| 230 | * @return Service |
||
| 231 | */ |
||
| 232 | 30 | protected function addAnnotationBlockForSoapHeaderMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method) |
|
| 258 | /** |
||
| 259 | * @param PhpAnnotationBlock $annotationBlock |
||
| 260 | * @param PhpMethod $method |
||
| 261 | * @return Service |
||
| 262 | */ |
||
| 263 | 115 | protected function addAnnotationBlockForOperationMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method) |
|
| 271 | /** |
||
| 272 | * @param PhpAnnotationBlock $annotationBlock |
||
| 273 | * @return Service |
||
| 274 | */ |
||
| 275 | 115 | protected function addAnnnotationBlockForgetResultMethod(PhpAnnotationBlock $annotationBlock) |
|
| 282 | /** |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | 115 | protected function getServiceReturnTypes() |
|
| 294 | /** |
||
| 295 | * @param MethodModel $method |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | 115 | public static function getOperationMethodReturnType(MethodModel $method, Generator $generator) |
|
| 314 | /** |
||
| 315 | * @param PhpMethod $method |
||
| 316 | * @return MethodModel|null |
||
| 317 | */ |
||
| 318 | 115 | protected function getModelFromMethod(PhpMethod $method) |
|
| 326 | /** |
||
| 327 | * @param PhpMethod $phpMethod |
||
| 328 | * @param MethodModel $methodModel |
||
| 329 | * @return Service |
||
| 330 | */ |
||
| 331 | 115 | protected function setModelFromMethod(PhpMethod $phpMethod, MethodModel $methodModel) |
|
| 336 | /** |
||
| 337 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getModel() |
||
| 338 | * @return ServiceModel |
||
| 339 | */ |
||
| 340 | 120 | public function getModel() |
|
| 344 | /** |
||
| 345 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel() |
||
| 346 | * @throws \InvalidaArgumentException |
||
| 347 | * @param AbstractModel $model |
||
| 348 | * @return Service |
||
| 349 | */ |
||
| 350 | 125 | public function setModel(AbstractModel $model) |
|
| 357 | } |
||
| 358 |