Conditions | 12 |
Paths | 20 |
Total Lines | 34 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Tests | 20 |
CRAP Score | 12.0155 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
22 | 31 | public function dispatch(string $job, array $params = [], string $service = null) |
|
23 | { |
||
24 | 31 | if (array_key_exists($job, $this->test->mockInstances)) { |
|
25 | 6 | $mocks = $this->test->mockInstances[$job]; |
|
26 | 6 | $valid = null; |
|
27 | 6 | foreach ($mocks as $mock) { |
|
28 | 6 | if ($mock->params == $params || (!$mock->params && !$valid)) { |
|
29 | 6 | $valid = $mock; |
|
30 | } |
||
31 | } |
||
32 | 6 | if ($valid) { |
|
33 | 6 | $result = $valid->result; |
|
34 | 6 | if (is_callable($result)) { |
|
35 | 2 | $result = $result($params); |
|
36 | } |
||
37 | 6 | $valid->calls++; |
|
38 | 6 | return $this->get(Converter::class)->toObject($result); |
|
39 | } |
||
40 | } |
||
41 | 31 | if ($this->test->disableRemote) { |
|
42 | 30 | if (!$this->get(Runner::class)->hasJob($job)) { |
|
43 | 1 | throw new Exception("Remote calls ($job) are disabled for tests"); |
|
44 | } |
||
45 | } |
||
46 | |||
47 | 31 | $converter = $this->get(Converter::class); |
|
48 | |||
49 | 31 | $global = $this->test->params ?: []; |
|
50 | 31 | if (!is_array($global)) { |
|
51 | $global = get_object_vars($converter->toObject($this->test->params)); |
||
52 | } |
||
53 | |||
54 | 31 | return parent::dispatch($job, array_merge($params, $global), $service); |
|
55 | } |
||
56 | } |
||
57 |