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 | 132 | /** |
|
| 56 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassConstants() |
||
| 57 | 132 | */ |
|
| 58 | protected function getClassConstants(ConstantContainer $constants) |
||
| 59 | { |
||
| 60 | } |
||
| 61 | /** |
||
| 62 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getConstantAnnotationBlock() |
||
| 63 | */ |
||
| 64 | protected function getConstantAnnotationBlock(PhpConstant $constant) |
||
| 65 | { |
||
| 66 | } |
||
| 67 | 132 | /** |
|
| 68 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassProperties() |
||
| 69 | 132 | */ |
|
| 70 | protected function getClassProperties(PropertyContainer $properties) |
||
| 71 | { |
||
| 72 | } |
||
| 73 | /** |
||
| 74 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getPropertyAnnotationBlock() |
||
| 75 | */ |
||
| 76 | protected function getPropertyAnnotationBlock(PhpProperty $property) |
||
| 77 | { |
||
| 78 | } |
||
| 79 | 132 | /** |
|
| 80 | * @return string |
||
| 81 | 132 | */ |
|
| 82 | protected function getClassDeclarationLineText() |
||
| 83 | { |
||
| 84 | return $this->getGenerator()->getOptionGatherMethods() === GeneratorOptions::VALUE_NONE ? 'This class stands for all operations' : parent::getClassDeclarationLineText(); |
||
| 85 | } |
||
| 86 | 132 | /** |
|
| 87 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassMethods() |
||
| 88 | 132 | */ |
|
| 89 | 132 | protected function getClassMethods(MethodContainer $methods) |
|
| 90 | { |
||
| 91 | $this->addSoapHeaderMethods($methods)->addOperationsMethods($methods)->addGetResultMethod($methods); |
||
| 92 | } |
||
| 93 | /** |
||
| 94 | 132 | * @param MethodContainer $methods |
|
| 95 | * @return Service |
||
| 96 | 132 | */ |
|
| 97 | 132 | protected function addSoapHeaderMethods(MethodContainer $methods) |
|
| 98 | 88 | { |
|
| 99 | 132 | foreach ($this->getModel()->getMethods() as $method) { |
|
| 100 | $this->addSoapHeaderFromMethod($methods, $method); |
||
| 101 | } |
||
| 102 | return $this; |
||
| 103 | } |
||
| 104 | /** |
||
| 105 | * @param MethodContainer $methods |
||
| 106 | 132 | * @param MethodModel $method |
|
| 107 | * @return Service |
||
| 108 | 132 | */ |
|
| 109 | 132 | protected function addSoapHeaderFromMethod(MethodContainer $methods, MethodModel $method) |
|
| 110 | 132 | { |
|
| 111 | 132 | $soapHeaderNames = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMES, array()); |
|
| 112 | 30 | $soapHeaderNamespaces = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMESPACES, array()); |
|
| 113 | 30 | $soapHeaderTypes = $method->getMetaValue(TagHeader::META_SOAP_HEADER_TYPES, array()); |
|
| 114 | 30 | foreach ($soapHeaderNames as $index => $soapHeaderName) { |
|
| 115 | 30 | $methodName = $this->getSoapHeaderMethodName($soapHeaderName); |
|
| 116 | 30 | if ($methods->get($methodName) === null) { |
|
| 117 | 20 | $soapHeaderNamespace = array_key_exists($index, $soapHeaderNamespaces) ? $soapHeaderNamespaces[$index] : null; |
|
| 118 | 88 | $soapHeaderType = array_key_exists($index, $soapHeaderTypes) ? $soapHeaderTypes[$index] : null; |
|
| 119 | 132 | $methods->add($this->getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, $soapHeaderType)); |
|
| 120 | } |
||
| 121 | } |
||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | /** |
||
| 125 | * @param string $methodName |
||
| 126 | * @param string $soapHeaderName |
||
| 127 | * @param string $soapHeaderNamespace |
||
| 128 | 30 | * @param string $soapHeaderType |
|
| 129 | * @return PhpMethod |
||
| 130 | */ |
||
| 131 | 30 | protected function getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, $soapHeaderType) |
|
| 132 | 30 | { |
|
| 133 | 30 | try { |
|
| 134 | 30 | $method = new PhpMethod($methodName, array( |
|
| 135 | 30 | $firstParameter = new PhpFunctionParameter(lcfirst($soapHeaderName), PhpFunctionParameterBase::NO_VALUE, $this->getTypeFromName($soapHeaderType)), |
|
| 136 | 20 | new PhpFunctionParameterBase(self::PARAM_SET_HEADER_NAMESPACE, $soapHeaderNamespace), |
|
| 137 | 30 | new PhpFunctionParameterBase(self::PARAM_SET_HEADER_MUSTUNDERSTAND, false), |
|
| 138 | 20 | new PhpFunctionParameterBase(self::PARAM_SET_HEADER_ACTOR, null), |
|
| 139 | )); |
||
| 140 | $model = $this->getModelByName($soapHeaderType); |
||
| 141 | 30 | if ($model instanceof StructModel) { |
|
| 142 | $rules = new Rules($this, $method, new StructAttributeModel($model->getGenerator(), $soapHeaderType, $model->getName(), $model)); |
||
| 143 | $rules->applyRules(lcfirst($soapHeaderName)); |
||
| 144 | $firstParameter->setModel($model); |
||
| 145 | } |
||
| 146 | $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)); |
||
| 147 | } catch (\InvalidArgumentException $exception) { |
||
| 148 | 30 | 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); |
|
| 149 | } |
||
| 150 | 30 | return $method; |
|
| 151 | 30 | } |
|
| 152 | 30 | /** |
|
| 153 | 30 | * @param string $name |
|
| 154 | 20 | * @return string |
|
| 155 | 30 | */ |
|
| 156 | protected function getTypeFromName($name) |
||
| 157 | { |
||
| 158 | $type = $name; |
||
| 159 | $model = $this->getModelByName($name); |
||
| 160 | if ($model instanceof StructModel) { |
||
| 161 | 30 | if ($model->isRestriction() || $model->isUnion()) { |
|
| 162 | $type = self::TYPE_STRING; |
||
| 163 | 30 | } else { |
|
| 164 | $type = $model->getPackagedName(true); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | return self::getValidType($type); |
||
| 168 | } |
||
| 169 | 132 | /** |
|
| 170 | * @param string $soapHeaderName |
||
| 171 | 132 | * @return string |
|
| 172 | 132 | */ |
|
| 173 | 88 | protected function getSoapHeaderMethodName($soapHeaderName) |
|
| 177 | /** |
||
| 178 | * @param MethodContainer $methods |
||
| 179 | * @return Service |
||
| 180 | 132 | */ |
|
| 181 | protected function addOperationsMethods(MethodContainer $methods) |
||
| 182 | 132 | { |
|
| 183 | 132 | foreach ($this->getModel()->getMethods() as $method) { |
|
| 184 | 132 | $this->addMainMethod($methods, $method); |
|
| 185 | 132 | } |
|
| 186 | return $this; |
||
| 187 | } |
||
| 188 | /** |
||
| 189 | * @param MethodContainer $methods |
||
| 190 | * @return Service |
||
| 191 | */ |
||
| 192 | 132 | protected function addGetResultMethod(MethodContainer $methods) |
|
| 199 | /** |
||
| 200 | * @param MethodContainer $methods |
||
| 201 | * @param MethodModel $method |
||
| 202 | * @return Service |
||
| 203 | 132 | */ |
|
| 204 | protected function addMainMethod(MethodContainer $methods, MethodModel $method) |
||
| 212 | /** |
||
| 213 | 132 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getMethodAnnotationBlock() |
|
| 214 | */ |
||
| 215 | protected function getMethodAnnotationBlock(PhpMethod $method) |
||
| 227 | 30 | /** |
|
| 228 | 30 | * @param PhpAnnotationBlock $annotationBlock |
|
| 229 | 30 | * @param PhpMethod $method |
|
| 230 | 30 | * @return Service |
|
| 231 | 30 | */ |
|
| 232 | 20 | protected function addAnnotationBlockForSoapHeaderMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method) |
|
| 258 | /** |
||
| 259 | * @param PhpAnnotationBlock $annotationBlock |
||
| 260 | 132 | * @param PhpMethod $method |
|
| 261 | * @return Service |
||
| 262 | 132 | */ |
|
| 263 | 132 | protected function addAnnotationBlockForOperationMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method) |
|
| 271 | /** |
||
| 272 | * @param PhpAnnotationBlock $annotationBlock |
||
| 273 | 132 | * @return Service |
|
| 274 | */ |
||
| 275 | 132 | protected function addAnnnotationBlockForgetResultMethod(PhpAnnotationBlock $annotationBlock) |
|
| 282 | 12 | /** |
|
| 283 | 12 | * @return string |
|
| 284 | */ |
||
| 285 | 12 | protected function getServiceReturnTypes() |
|
| 294 | /** |
||
| 295 | 132 | * @param MethodModel $method |
|
| 296 | 132 | * @return string |
|
| 297 | 24 | */ |
|
| 298 | 16 | public static function getOperationMethodReturnType(MethodModel $method, Generator $generator) |
|
| 314 | /** |
||
| 315 | 138 | * @param PhpMethod $method |
|
| 316 | * @return MethodModel|null |
||
| 317 | 138 | */ |
|
| 318 | protected function getModelFromMethod(PhpMethod $method) |
||
| 326 | /** |
||
| 327 | 144 | * @param PhpMethod $phpMethod |
|
| 328 | 6 | * @param MethodModel $methodModel |
|
| 329 | * @return Service |
||
| 330 | 138 | */ |
|
| 331 | protected function setModelFromMethod(PhpMethod $phpMethod, MethodModel $methodModel) |
||
| 336 | /** |
||
| 337 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getModel() |
||
| 338 | * @return ServiceModel |
||
| 339 | */ |
||
| 340 | public function getModel() |
||
| 344 | /** |
||
| 345 | * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel() |
||
| 346 | * @throws \InvalidaArgumentException |
||
| 347 | * @param AbstractModel $model |
||
| 348 | * @return Service |
||
| 349 | */ |
||
| 350 | public function setModel(AbstractModel $model) |
||
| 357 | } |
||
| 358 |