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 |
||
| 17 | class SoapClient extends \SoapClient implements SoapClientInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $options; |
||
| 23 | /** |
||
| 24 | * @var EventDispatcherInterface |
||
| 25 | */ |
||
| 26 | protected $dispatcher; |
||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $mockRequests = []; |
||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $mockResponses = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * SoapClient constructor. |
||
| 38 | * @param null $wsdl |
||
| 39 | * @param array|null $options |
||
| 40 | */ |
||
| 41 | 18 | public function __construct($wsdl = null, array $options = []) |
|
| 76 | |||
| 77 | 6 | public function getOptions(): array |
|
| 81 | |||
| 82 | 9 | public function __call($function_name, $arguments) |
|
| 83 | { |
||
| 84 | try { |
||
| 85 | 9 | $response = parent::__call($function_name, $arguments); |
|
| 86 | //works only with 'exceptions' => false, we always throw |
||
| 87 | 6 | if (is_soap_fault($response)) { |
|
| 88 | 6 | throw $response; |
|
| 89 | } |
||
| 90 | 3 | } catch (\Exception $e) { |
|
| 91 | 3 | $this->handleFault($function_name, $arguments, $e); |
|
| 92 | } |
||
| 93 | |||
| 94 | 6 | return $response; |
|
| 95 | } |
||
| 96 | |||
| 97 | public function __soapCall( |
||
| 98 | $function_name, |
||
| 99 | $arguments, |
||
| 100 | $options = null, |
||
| 101 | $input_headers = null, |
||
| 102 | &$output_headers = null |
||
| 103 | ) { |
||
| 104 | try { |
||
| 105 | $response = parent::__soapCall($function_name, $arguments, $options, $input_headers, $output_headers); |
||
| 106 | //works only with 'exceptions' => false, we always throw |
||
| 107 | if (is_soap_fault($response)) { |
||
| 108 | throw $response; |
||
| 109 | } |
||
| 110 | } catch (\Exception $e) { |
||
| 111 | $this->handleFault($function_name, $arguments, $e); |
||
| 112 | } |
||
| 113 | |||
| 114 | return $response; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $request |
||
| 119 | * @param string $location |
||
| 120 | * @param string $action |
||
| 121 | * @param int $version |
||
| 122 | * @param null $one_way |
||
| 123 | * @return bool|string |
||
| 124 | */ |
||
| 125 | 6 | public function __doRequest($request, $location, $action, $version, $one_way = null) |
|
| 126 | { |
||
| 127 | 6 | $id = Uuid::uuid1(); |
|
| 128 | |||
| 129 | 6 | foreach ($this->mockRequests as $key => $mockRequest) { |
|
| 130 | 6 | if (is_string($key)) { |
|
| 131 | 6 | if (strrpos($action, $key) !== false) { |
|
| 132 | 6 | $request = file_get_contents($mockRequest); |
|
| 133 | 6 | break; |
|
| 134 | } |
||
| 135 | } else { |
||
| 136 | if (is_callable($mockRequest)) { |
||
| 137 | if ($requestFilePath = $mockRequest($request, $location, $action, $version, $one_way)) { |
||
| 138 | $request = file_get_contents($requestFilePath); |
||
| 139 | break; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | 6 | $this->preCall($id->toString(), (string)$action, $request); |
|
| 146 | |||
| 147 | 6 | foreach ($this->mockResponses as $key => $mockResponse) { |
|
| 148 | 6 | if (is_string($key)) { |
|
| 149 | 3 | if (strrpos($action, $key) !== false) { |
|
| 150 | 3 | $response = file_get_contents($mockResponse); |
|
| 151 | |||
| 152 | 3 | $this->postCall($id->toString(), $action, $response); |
|
| 153 | |||
| 154 | 3 | return $response; |
|
| 155 | } |
||
| 156 | } else { |
||
| 157 | 3 | if (is_callable($mockResponse)) { |
|
| 158 | 3 | if ($responseFilePath = $mockResponse($request, $location, $action, $version, $one_way)) { |
|
| 159 | 3 | $response = file_get_contents($responseFilePath); |
|
| 160 | |||
| 161 | 3 | $this->postCall($id->toString(), $action, $response); |
|
| 162 | |||
| 163 | 3 | return $response; |
|
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /* workaround for working timeout */ |
||
| 170 | $socketTimeout = false; |
||
| 171 | if (isset($this->options['connection_timeout']) |
||
| 172 | && (int)$this->options['connection_timeout'] > (int)ini_get('default_socket_timeout') |
||
| 173 | ) { |
||
| 174 | $socketTimeout = ini_set('default_socket_timeout', $this->options['connection_timeout']); |
||
| 175 | } |
||
| 176 | |||
| 177 | $response = parent::__doRequest($request, $location, $action, $version, $one_way); |
||
| 178 | |||
| 179 | $this->postCall($id->toString(), (string)$action, $response); |
||
| 180 | |||
| 181 | if ($socketTimeout !== false) { |
||
| 182 | ini_set('default_socket_timeout', $socketTimeout); |
||
| 183 | } |
||
| 184 | |||
| 185 | return $response; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Triggered before a request is executed |
||
| 190 | * |
||
| 191 | * @param string $id |
||
| 192 | * @param string $resource |
||
| 193 | * @param string $requestContent |
||
| 194 | */ |
||
| 195 | 6 | protected function preCall(string $id, string $resource, string $requestContent = null) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $id |
||
| 202 | * @param string $resource |
||
| 203 | * @param string $response |
||
| 204 | */ |
||
| 205 | 6 | protected function postCall(string $id, string $resource, string $response = null) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $id |
||
| 220 | * @param string $resource |
||
| 221 | * @param string $requestContent |
||
| 222 | * @param \Exception $exception |
||
| 223 | */ |
||
| 224 | 3 | protected function faultCall(string $id, string $resource, string $requestContent, \Exception $exception) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $wsdl |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | 18 | protected function resolveLocation($wsdl) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param array $mockRequests |
||
| 250 | */ |
||
| 251 | 12 | public function setMockRequests(array $mockRequests) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param array $mockResponses |
||
| 258 | */ |
||
| 259 | 12 | public function setMockResponses(array $mockResponses) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param EventDispatcherInterface $dispatcher |
||
| 266 | * @required |
||
| 267 | */ |
||
| 268 | 18 | public function setDispatcher(EventDispatcherInterface $dispatcher) |
|
| 269 | { |
||
| 270 | 18 | if (class_exists(LegacyEventDispatcherProxy::class)) { |
|
| 271 | 12 | $this->dispatcher = LegacyEventDispatcherProxy::decorate($dispatcher); |
|
| 272 | } else { |
||
| 273 | 6 | $this->dispatcher = $dispatcher; |
|
| 274 | } |
||
| 275 | 18 | } |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param $function_name |
||
| 279 | * @param $arguments |
||
| 280 | * @param $e |
||
| 281 | */ |
||
| 282 | 3 | protected function handleFault($function_name, $arguments, $e): void |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @param Event $event |
||
| 296 | * @param string $eventName |
||
| 297 | */ |
||
| 298 | 9 | private function dispatch(Event $event, $eventName) |
|
| 313 | } |
||
| 314 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: