Conditions | 11 |
Paths | 8 |
Total Lines | 36 |
Code Lines | 21 |
Lines | 8 |
Ratio | 22.22 % |
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 |
||
16 | public function matches(Message $message) |
||
17 | { |
||
18 | if ($message->isHandled()) { |
||
19 | $this->debug($this->name.': message already handled'); |
||
|
|||
20 | return false; |
||
21 | } |
||
22 | |||
23 | if ($this->isBot !== null && $this->isBot !== $message->isBot()) { |
||
24 | $this->debug($this->name.': isBot match failed'); |
||
25 | return false; |
||
26 | } |
||
27 | |||
28 | View Code Duplication | if (!empty($this->channels) && !in_array($message->getChannelName(), $this->channels)) { |
|
29 | $this->debug($this->name.': channels match failed '.json_encode($this->channels)); |
||
30 | return false; |
||
31 | } |
||
32 | |||
33 | View Code Duplication | if (!empty($this->users) && !in_array($message->getUsername(), $this->users)) { |
|
34 | $this->debug($this->name.': users match failed '.json_encode($this->users)); |
||
35 | return false; |
||
36 | } |
||
37 | |||
38 | if (empty($this->patterns)) { |
||
39 | return true; |
||
40 | } |
||
41 | |||
42 | $matches = []; |
||
43 | $text = $message->getPluginText(); |
||
44 | foreach ($this->patterns as $pattern) { |
||
45 | if (preg_match($pattern, $text, $matches)) { |
||
46 | break; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | return $matches; |
||
51 | } |
||
52 | |||
77 | } |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.