Conditions | 14 |
Paths | 3 |
Total Lines | 64 |
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 |
||
88 | public function render(Model $entity, string $accessArea): string |
||
89 | { |
||
90 | $default = ''; |
||
91 | $selected = ''; |
||
92 | |||
93 | switch ($this->type) { |
||
94 | case 'select': |
||
95 | |||
96 | $default = collect(array_map('trans', array_map('trim', explode("\n", $this->default))))->map(function ($item) use (&$selected) { |
||
97 | if (mb_strpos($item, '=')) { |
||
98 | $key = mb_strstr($item, '=', true); |
||
99 | $value = str_replace_first('=', '', mb_strstr($item, '=')); |
||
100 | |||
101 | // Check for SELECTED itmes (marked by asterisk) |
||
102 | ! str_contains($value, '*') || $selected = $key; |
||
103 | ! str_contains($value, '*') || $value = str_replace_last('*', '', $value); |
||
104 | } else { |
||
105 | $key = $value = $item; |
||
106 | |||
107 | // Check for SELECTED itmes (marked by asterisk) |
||
108 | ! str_contains($value, '*') || $key = $value = $selected = str_replace_last('*', '', $value); |
||
109 | } |
||
110 | |||
111 | return [$key => $value]; |
||
112 | })->collapse(); |
||
113 | |||
114 | return view("cortex/attributes::{$accessArea}.types.".$this->type, ['attribute' => $this, 'entity' => $entity, 'default' => $default, 'selected' => $selected])->render(); |
||
115 | |||
116 | case 'check': |
||
117 | |||
118 | $default = collect(array_map('trans', array_map('trim', explode("\n", $this->default))))->map(function ($item) use ($entity) { |
||
119 | $details = [ |
||
120 | 'label' => '', |
||
121 | 'status' => false, |
||
122 | ]; |
||
123 | |||
124 | if (mb_strpos($item, '=')) { |
||
125 | $details['label'] = mb_strstr($item, '=', true); |
||
126 | $item = str_replace_first('=', '', mb_strstr($item, '=')); |
||
127 | |||
128 | // Check for SELECTED itmes (marked by asterisk) |
||
129 | ! str_contains($item, '*') || $details['status'] = true; |
||
130 | ! str_contains($item, '*') || $item = str_replace_last('*', '', $item); |
||
131 | |||
132 | ! $entity->exists || $details['status'] = $entity->{$this->slug}->search($item) !== false; |
||
133 | } else { |
||
134 | $details['label'] = $item; |
||
135 | |||
136 | // Check for SELECTED itmes (marked by asterisk) |
||
137 | ! str_contains($item, '*') || $details['status'] = true; |
||
138 | ! str_contains($item, '*') || $details['label'] = $item = str_replace_last('*', '', $item); |
||
139 | |||
140 | ! $entity->exists || $details['status'] = $entity->{$this->slug}->search($item) !== false; |
||
141 | } |
||
142 | |||
143 | return [$item => $details]; |
||
144 | })->collapse(); |
||
145 | |||
146 | return view("cortex/attributes::{$accessArea}.types.".$this->type, ['attribute' => $this, 'entity' => $entity, 'default' => $default])->render(); |
||
147 | |||
148 | default: |
||
149 | return view("cortex/attributes::{$accessArea}.types.".$this->type, ['attribute' => $this, 'entity' => $entity, 'default' => $default])->render(); |
||
150 | } |
||
151 | } |
||
152 | |||
163 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.