Conditions | 10 |
Paths | 59 |
Total Lines | 53 |
Lines | 0 |
Ratio | 0 % |
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 |
||
80 | public function passes($attribute, $value) |
||
81 | { |
||
82 | $items = collect(explode($this->separatedBy, $value)); |
||
83 | |||
84 | if (!is_null($this->minimum)) { |
||
85 | if ($items->count() < $this->minimum) { |
||
86 | $this->message = __('validation.delimited.min', [ |
||
|
|||
87 | 'minimum' => $this->minimum, |
||
88 | 'actual' => $items->count(), |
||
89 | 'item' => Str::plural($this->validationMessageWord, $items->count()) |
||
90 | ]); |
||
91 | |||
92 | return false; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | if (!is_null($this->maximum)) { |
||
97 | if ($items->count() > $this->maximum) { |
||
98 | $this->message = __('validation.delimited.max', [ |
||
99 | 'maximum' => $this->maximum, |
||
100 | 'actual' => $items->count(), |
||
101 | 'item' => Str::plural($this->validationMessageWord, $items->count()) |
||
102 | ]); |
||
103 | |||
104 | return false; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | if ($this->trimItems) { |
||
109 | $items = $items->map(function ($item) { |
||
110 | return trim($item); |
||
111 | }); |
||
112 | } |
||
113 | |||
114 | foreach ($items as $item) { |
||
115 | [$isValid, $validationMessage] = $this->validate($attribute, $item); |
||
116 | |||
117 | if (! $isValid) { |
||
118 | $this->message = $validationMessage; |
||
119 | |||
120 | return false; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | if (!$this->allowDuplicates) { |
||
125 | if ($items->unique()->count() !== $items->count()) { |
||
126 | $this->message = __('validation.delimited.unique'); |
||
127 | return false; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | return true; |
||
132 | } |
||
133 | |||
150 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.