Conditions | 4 |
Paths | 1 |
Total Lines | 59 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Tests | 38 |
CRAP Score | 4 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php declare(strict_types=1); |
||
44 | 4 | public function consume( |
|
45 | string $queue = '', |
||
46 | array $qos = [], |
||
47 | string $consumerTag = '', |
||
48 | bool $noLocal = false, |
||
49 | bool $noAck = false, |
||
50 | bool $exclusive = false, |
||
51 | bool $nowait = false, |
||
52 | array $arguments = [] |
||
53 | ): Subject { |
||
54 | 4 | $subject = new Subject(); |
|
55 | 4 | $consumeArgs = [$queue, $consumerTag, $noLocal, $noAck, $exclusive, $nowait, $arguments]; |
|
56 | |||
57 | 4 | $channel = $this->bunny->channel(); |
|
58 | 3 | $channel->then(function (Channel $channel) use ($qos) { |
|
|
|||
59 | 3 | if (count($qos) === 0) { |
|
60 | 2 | return $channel; |
|
61 | } |
||
62 | |||
63 | 1 | return $channel->qos(...$qos)->then(function () use ($channel) { |
|
64 | 1 | return $channel; |
|
65 | 1 | }); |
|
66 | })->then(function (Channel $channel) use ($subject, $consumeArgs) { |
||
67 | /** @var string $consumerTag */ |
||
68 | 3 | $consumerTag = null; |
|
69 | 3 | $timer = $this->loop->addPeriodicTimer($this->checkInterval, function () use ($channel, $subject, &$timer, &$consumerTag) { |
|
70 | 1 | if (!$subject->isDisposed()) { |
|
71 | 1 | return; |
|
72 | } |
||
73 | |||
74 | 1 | $this->cancelSubscription( |
|
75 | 1 | $timer, |
|
76 | 1 | $channel, |
|
77 | 1 | $consumerTag |
|
78 | 1 | )->done([$subject, 'onComplete'], $this->onError($subject, $timer)); |
|
79 | 3 | }); |
|
80 | 3 | $channel->consume( |
|
81 | 3 | function (BunnyMessage $message, Channel $channel) use ($subject, &$timer, &$consumerTag) { |
|
82 | 2 | if ($subject->isDisposed()) { |
|
83 | 1 | $channel->nack($message); |
|
84 | 1 | $this->cancelSubscription( |
|
85 | 1 | $timer, |
|
86 | 1 | $channel, |
|
87 | 1 | $consumerTag |
|
88 | 1 | )->done([$subject, 'onComplete'], $this->onError($subject, $timer)); |
|
89 | |||
90 | 1 | return; |
|
91 | } |
||
92 | |||
93 | 1 | $subject->onNext(new Message($message, $channel)); |
|
94 | 3 | }, |
|
95 | 3 | ...$consumeArgs |
|
96 | 3 | )->then(function (MethodBasicConsumeOkFrame $response) use (&$consumerTag) { |
|
97 | 2 | $consumerTag = $response->consumerTag; |
|
98 | 3 | })->done(null, $this->onError($subject, $timer)); |
|
99 | 4 | })->done(null, [$subject, 'onError']); |
|
100 | |||
101 | 4 | return $subject; |
|
102 | } |
||
103 | |||
121 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: