Conditions | 13 |
Paths | 19 |
Total Lines | 114 |
Code Lines | 77 |
Lines | 0 |
Ratio | 0 % |
Tests | 59 |
CRAP Score | 13.2018 |
Changes | 10 | ||
Bugs | 0 | Features | 2 |
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 |
||
134 | 45 | public function __construct( |
|
135 | TypeServiceInterface $typeService, |
||
136 | $year = self::WILDCARD, |
||
137 | $month = self::WILDCARD, |
||
138 | $day = self::WILDCARD, |
||
139 | $hour = self::WILDCARD, |
||
140 | $minute = self::WILDCARD, |
||
141 | $second = self::WILDCARD, |
||
142 | $uid = self::WILDCARD, |
||
143 | $type = self::WILDCARD, |
||
144 | $revision = Revision::CURRENT, |
||
145 | $visibility = SelectorInterface::VISIBLE, |
||
146 | $draft = SelectorInterface::PUBLISHED |
||
147 | ) { |
||
148 | 45 | $datePrecision = intval(getenv('OBJECT_DATE_PRECISION')); |
|
149 | |||
150 | // Validate the creation date and ID components |
||
151 | $dateComponents = [ |
||
152 | 45 | 'year' => $year, |
|
153 | 45 | 'month' => $month, |
|
154 | 45 | 'day' => $day, |
|
155 | 45 | 'hour' => $hour, |
|
156 | 45 | 'minute' => $minute, |
|
157 | 'second' => $second |
||
158 | 45 | ]; |
|
159 | 45 | foreach (array_slice($dateComponents, 0, $datePrecision) as $label => $component) { |
|
160 | // If the component isn't valid |
||
161 | 45 | if (!is_int($component) && ($component !== self::WILDCARD)) { |
|
162 | 1 | throw new InvalidArgumentException( |
|
163 | 1 | sprintf( |
|
164 | 1 | 'Invalid repository selector '.$label.' component "%s"', |
|
165 | $component |
||
166 | 1 | ), |
|
167 | 1 | InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT, |
|
168 | 1 | null, |
|
169 | $label |
||
170 | 1 | ); |
|
171 | } |
||
172 | |||
173 | // Set the component value |
||
174 | 44 | $this->$label = |
|
175 | 44 | ($component === self::WILDCARD) ? self::WILDCARD : str_pad($component, 2, '0', STR_PAD_LEFT); |
|
176 | 44 | } |
|
177 | |||
178 | // If the ID component isn't valid |
||
179 | 44 | if (!is_int($uid) && ($uid !== self::WILDCARD)) { |
|
180 | 1 | throw new InvalidArgumentException( |
|
181 | 1 | sprintf( |
|
182 | 1 | 'Invalid repository selector ID component "%s"', |
|
183 | $uid |
||
184 | 1 | ), |
|
185 | 1 | InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT, |
|
186 | 1 | null, |
|
187 | 'id' |
||
188 | 1 | ); |
|
189 | } |
||
190 | 43 | $this->uid = $uid; |
|
191 | |||
192 | // If the type component isn't valid |
||
193 | 43 | if (!$typeService->supportsType($type) && ($type !== self::WILDCARD)) { |
|
194 | 1 | throw new InvalidArgumentException( |
|
195 | 1 | sprintf( |
|
196 | 1 | 'Invalid repository selector type component "%s"', |
|
197 | $type |
||
198 | 1 | ), |
|
199 | 1 | InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT, |
|
200 | 1 | null, |
|
201 | 'type' |
||
202 | 1 | ); |
|
203 | } |
||
204 | 42 | $this->type = $type; |
|
205 | |||
206 | // If the revision component isn't valid |
||
207 | 42 | if (!Revision::isValidRevision($revision) && ($revision !== self::WILDCARD)) { |
|
208 | 1 | throw new InvalidArgumentException( |
|
209 | 1 | sprintf( |
|
210 | 1 | 'Invalid repository selector revision component "%s"', |
|
211 | $revision |
||
212 | 1 | ), |
|
213 | 1 | InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT, |
|
214 | 1 | null, |
|
215 | 'revision' |
||
216 | 1 | ); |
|
217 | } |
||
218 | 41 | $this->revision = $revision; |
|
219 | |||
220 | // If the object visibility isn't valid |
||
221 | 41 | if (!self::isValidVisibility($visibility)) { |
|
222 | 1 | throw new InvalidArgumentException( |
|
223 | 1 | sprintf( |
|
224 | 1 | 'Invalid repository selector visibility "%s"', |
|
225 | $visibility |
||
226 | 1 | ), |
|
227 | 1 | InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT, |
|
228 | 1 | null, |
|
229 | 'visibility' |
||
230 | 1 | ); |
|
231 | } |
||
232 | 40 | $this->visibility = $visibility; |
|
233 | |||
234 | // If the object draft state isn't valid |
||
235 | 40 | if (!self::isValidDraftState($draft)) { |
|
236 | throw new InvalidArgumentException( |
||
237 | sprintf( |
||
238 | 'Invalid repository selector draft state "%s"', |
||
239 | $visibility |
||
240 | ), |
||
241 | InvalidArgumentException::INVALID_REPOSITORY_SELECTOR_COMPONENT, |
||
242 | null, |
||
243 | 'draft' |
||
244 | ); |
||
245 | } |
||
246 | 40 | $this->draft = $draft; |
|
247 | 40 | } |
|
248 | |||
385 |