Conditions | 14 |
Paths | 3 |
Total Lines | 67 |
Code Lines | 41 |
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 | break; |
||
116 | |||
117 | case 'check': |
||
118 | |||
119 | $default = collect(array_map('trans', array_map('trim', explode("\n", $this->default))))->map(function ($item) use ($entity) { |
||
120 | $details = [ |
||
121 | 'label' => '', |
||
122 | 'status' => false, |
||
123 | ]; |
||
124 | |||
125 | if (mb_strpos($item, '=')) { |
||
126 | $details['label'] = mb_strstr($item, '=', true); |
||
127 | $item = str_replace_first('=', '', mb_strstr($item, '=')); |
||
128 | |||
129 | // Check for SELECTED itmes (marked by asterisk) |
||
130 | ! str_contains($item, '*') || $details['status'] = true; |
||
131 | ! str_contains($item, '*') || $item = str_replace_last('*', '', $item); |
||
132 | |||
133 | ! $entity->exists || $details['status'] = $entity->{$this->slug}->search($item) !== false; |
||
134 | } else { |
||
135 | $details['label'] = $item; |
||
136 | |||
137 | // Check for SELECTED itmes (marked by asterisk) |
||
138 | ! str_contains($item, '*') || $details['status'] = true; |
||
139 | ! str_contains($item, '*') || $details['label'] = $item = str_replace_last('*', '', $item); |
||
140 | |||
141 | ! $entity->exists || $details['status'] = $entity->{$this->slug}->search($item) !== false; |
||
142 | } |
||
143 | |||
144 | return [$item => $details]; |
||
145 | })->collapse(); |
||
146 | |||
147 | return view("cortex/attributes::$accessArea.types.".$this->type, ['attribute' => $this, 'entity' => $entity, 'default' => $default])->render(); |
||
148 | break; |
||
149 | |||
150 | default: |
||
151 | return view("cortex/attributes::$accessArea.types.".$this->type, ['attribute' => $this, 'entity' => $entity, 'default' => $default])->render(); |
||
152 | break; |
||
153 | } |
||
154 | } |
||
155 | |||
166 |
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.