Conditions | 13 |
Paths | 1 |
Total Lines | 43 |
Code Lines | 25 |
Lines | 18 |
Ratio | 41.86 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
59 | private function ifThenElse($conditions) |
||
60 | { |
||
61 | return $this->add(function ($value, $nameKey) use ($conditions) { |
||
62 | |||
63 | foreach ($conditions as $condition) { |
||
64 | if (!isset($condition['is'])) { |
||
65 | return $this->createError('alternatives.missing_is', $value, $nameKey); |
||
66 | } |
||
67 | |||
68 | if (!$condition['is'] instanceof AbstractValidator) { |
||
69 | return $this->createError('alternatives.invalid_is', $value, $nameKey); |
||
70 | } |
||
71 | |||
72 | /** @var AbstractValidator $is */ |
||
73 | $is = $condition['is']; |
||
74 | $is->toBool(true); |
||
75 | |||
76 | if (!isset($condition['then'])) { |
||
77 | return $this->createError('alternatives.missing_then', $value, $nameKey); |
||
78 | } |
||
79 | |||
80 | if ($is($value)) { |
||
81 | View Code Duplication | if ($condition['then'] instanceof AbstractValidator) { |
|
82 | $validationStack = $condition['then']->validationStack; |
||
83 | foreach ($validationStack as $validator) { |
||
84 | $this->validationStack[] = $validator; |
||
85 | } |
||
86 | } elseif (!is_null($condition['then'])) { |
||
87 | return $condition['then']; |
||
88 | } |
||
89 | View Code Duplication | } elseif (isset($condition['else'])) { |
|
90 | if ($condition['else'] instanceof AbstractValidator) { |
||
91 | $validationStack = $condition['else']->validationStack; |
||
92 | foreach ($validationStack as $validator) { |
||
93 | $this->validationStack[] = $validator; |
||
94 | } |
||
95 | } elseif (!is_null($condition['else'])) { |
||
96 | return $condition['else']; |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | }); |
||
101 | } |
||
102 | } |
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.