We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 13 |
Paths | 40 |
Total Lines | 49 |
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 |
||
46 | public function getUpdateFields($id = false) |
||
47 | { |
||
48 | $fields = $this->fields(); |
||
49 | $entry = ($id != false) ? $this->getEntry($id) : $this->getCurrentEntry(); |
||
50 | |||
51 | foreach ($fields as &$field) { |
||
52 | // set the value |
||
53 | if (! isset($field['value'])) { |
||
54 | if (isset($field['subfields'])) { |
||
55 | $field['value'] = []; |
||
56 | foreach ($field['subfields'] as $subfield) { |
||
57 | $field['value'][] = $entry->{$subfield['name']}; |
||
58 | } |
||
59 | |||
60 | // handle fake fields |
||
61 | } elseif (! empty($field['fake'])) { |
||
62 | // determine the stored-in attribute |
||
63 | $fakeStoredInAttribute = $field['store_in'] ?? 'extras'; |
||
64 | // check if the fake stored-in attribute exists |
||
65 | if (! empty($entry->{$fakeStoredInAttribute})) { |
||
66 | $fakeStoredInArray = $entry->{$fakeStoredInAttribute}; |
||
67 | // check if it's a string, decode it |
||
68 | // otherwise, it should be an array |
||
69 | if (is_string($fakeStoredInArray)) { |
||
70 | // decode it |
||
71 | $fakeStoredInArray = json_decode($fakeStoredInArray, true); |
||
72 | } |
||
73 | |||
74 | if (! empty($fakeStoredInArray) && is_array($fakeStoredInArray) && isset($fakeStoredInArray[$field['name']])) { |
||
75 | $field['value'] = $fakeStoredInArray[$field['name']]; |
||
76 | } |
||
77 | } |
||
78 | } else { |
||
79 | $field['value'] = $this->getModelAttributeValue($entry, $field); |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | // always have a hidden input for the entry id |
||
85 | if (! array_key_exists('id', $fields)) { |
||
86 | $fields['id'] = [ |
||
87 | 'name' => $entry->getKeyName(), |
||
88 | 'value' => $entry->getKey(), |
||
89 | 'type' => 'hidden', |
||
90 | ]; |
||
91 | } |
||
92 | |||
93 | return $fields; |
||
94 | } |
||
95 | |||
134 |
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.