We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 11 |
Paths | 18 |
Total Lines | 28 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Tests | 13 |
CRAP Score | 11 |
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 |
||
20 | 11 | public function compactFakeFields($requestInput, $form = 'create', $id = false) |
|
21 | { |
||
22 | // get the right fields according to the form type (create/update) |
||
23 | 11 | $fields = $this->getFields($form, $id); |
|
|
|||
24 | |||
25 | 9 | $compactedFakeFields = []; |
|
26 | 9 | foreach ($fields as $field) { |
|
27 | // compact fake fields |
||
28 | 7 | if (isset($field['fake']) && $field['fake'] == true && array_key_exists($field['name'], $requestInput)) { |
|
29 | 2 | $fakeFieldKey = isset($field['store_in']) ? $field['store_in'] : 'extras'; |
|
30 | 2 | $this->addCompactedField($requestInput, $field['name'], $fakeFieldKey); |
|
31 | |||
32 | 2 | if (! in_array($fakeFieldKey, $compactedFakeFields)) { |
|
33 | 7 | $compactedFakeFields[] = $fakeFieldKey; |
|
34 | } |
||
35 | } |
||
36 | } |
||
37 | |||
38 | // json_encode all fake_value columns if applicable in the database, so they can be properly stored and interpreted |
||
39 | 9 | foreach ($compactedFakeFields as $value) { |
|
40 | 2 | if (! (property_exists($this->model, 'translatable') && in_array($value, $this->model->getTranslatableAttributes(), true)) && $this->model->shouldEncodeFake($value)) { |
|
41 | 2 | $requestInput[$value] = json_encode($requestInput[$value]); |
|
42 | } |
||
43 | } |
||
44 | |||
45 | // if there are no fake fields defined, return the original request input |
||
46 | 9 | return $requestInput; |
|
47 | } |
||
48 | |||
64 |
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.