Complex classes like SoapClient 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 SoapClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class SoapClient extends \SoapClient |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $options; |
||
| 21 | /** |
||
| 22 | * @var EventDispatcherInterface |
||
| 23 | */ |
||
| 24 | protected $dispatcher; |
||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private $mockRequests = []; |
||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $mockResponses = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * SoapClient constructor. |
||
| 36 | * @param null $wsdl |
||
| 37 | * @param array|null $options |
||
| 38 | */ |
||
| 39 | 9 | public function __construct($wsdl = null, array $options = []) |
|
| 74 | |||
| 75 | 9 | public function __call($function_name, $arguments) |
|
| 89 | |||
| 90 | public function __soapCall( |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $request |
||
| 112 | * @param string $location |
||
| 113 | * @param string $action |
||
| 114 | * @param int $version |
||
| 115 | * @param null $one_way |
||
| 116 | * @return bool|string |
||
| 117 | */ |
||
| 118 | 6 | public function __doRequest($request, $location, $action, $version, $one_way = null) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Triggered before a request is executed |
||
| 184 | * |
||
| 185 | * @param string $id |
||
| 186 | * @param string $resource |
||
| 187 | * @param string $requestContent |
||
| 188 | */ |
||
| 189 | 6 | protected function preCall(string $id, string $resource, string $requestContent = null) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $id |
||
| 198 | * @param string $resource |
||
| 199 | * @param string $response |
||
| 200 | */ |
||
| 201 | 6 | protected function postCall(string $id, string $resource, string $response = null) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $id |
||
| 218 | * @param string $resource |
||
| 219 | * @param string $requestContent |
||
| 220 | * @param \Exception $exception |
||
| 221 | */ |
||
| 222 | 3 | protected function faultCall(string $id, string $resource, string $requestContent, \Exception $exception) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $wsdl |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | 9 | protected function resolveLocation($wsdl) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param array $mockRequests |
||
| 250 | */ |
||
| 251 | 9 | public function setMockRequests(array $mockRequests) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param array $mockResponses |
||
| 258 | */ |
||
| 259 | 9 | public function setMockResponses(array $mockResponses) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param EventDispatcherInterface $dispatcher |
||
| 266 | */ |
||
| 267 | 9 | public function setDispatcher(EventDispatcherInterface $dispatcher) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param $function_name |
||
| 274 | * @param $arguments |
||
| 275 | * @param $e |
||
| 276 | */ |
||
| 277 | 3 | protected function handleFault($function_name, $arguments, $e): void |
|
| 288 | } |
||
| 289 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.