Conditions | 29 |
Paths | 269 |
Total Lines | 140 |
Code Lines | 47 |
Lines | 11 |
Ratio | 7.86 % |
Changes | 6 | ||
Bugs | 3 | Features | 1 |
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 namespace Anomaly\Streams\Platform\Ui\Table\Component\Header\Guesser; |
||
71 | public function guess(TableBuilder $builder) |
||
72 | { |
||
73 | $columns = $builder->getColumns(); |
||
74 | $stream = $builder->getTableStream(); |
||
75 | |||
76 | $module = $this->modules->active(); |
||
77 | |||
78 | foreach ($columns as &$column) { |
||
|
|||
79 | |||
80 | /** |
||
81 | * If the heading is already set then |
||
82 | * we don't have anything to do. |
||
83 | */ |
||
84 | if (isset($column['heading'])) { |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * If the heading is false, then no |
||
90 | * header is desired at all. |
||
91 | */ |
||
92 | if (isset($column['heading']) && $column['heading'] === false) { |
||
93 | continue; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * No stream means we can't |
||
98 | * really do much here. |
||
99 | */ |
||
100 | if (!$stream instanceof StreamInterface) { |
||
101 | continue; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * No module means we can't |
||
106 | * really do much here. |
||
107 | */ |
||
108 | if (!$module instanceof Module) { |
||
109 | continue; |
||
110 | } |
||
111 | |||
112 | if (!isset($column['field']) && is_string($column['value'])) { |
||
113 | $column['field'] = $column['value']; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * If the heading matches a field |
||
118 | * with dot format then reduce it. |
||
119 | */ |
||
120 | if (isset($column['field']) && preg_match("/^entry.([a-zA-Z\\_]+)/", $column['field'], $match)) { |
||
121 | $column['field'] = $match[1]; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Detect some built in columns. |
||
126 | */ |
||
127 | if (in_array($column['field'], ['id', 'created_at', 'created_by', 'updated_at', 'updated_by'])) { |
||
128 | |||
129 | $column['heading'] = 'streams::entry.' . $column['field']; |
||
130 | |||
131 | continue; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Detect entry title. |
||
136 | */ |
||
137 | if (in_array($column['field'], ['view_link', 'edit_link']) && $field = $stream->getTitleField()) { |
||
138 | |||
139 | $column['heading'] = $field->getName(); |
||
140 | |||
141 | continue; |
||
142 | } |
||
143 | |||
144 | $field = $stream->getField($column['value']); |
||
145 | |||
146 | /** |
||
147 | * Detect the title column. |
||
148 | */ |
||
149 | $title = $stream->getTitleField(); |
||
150 | |||
151 | if ( |
||
152 | $title && |
||
153 | !$field && |
||
154 | $column['field'] == 'title' && |
||
155 | $this->translator->has($heading = $title->getName()) |
||
156 | ) { |
||
157 | $column['heading'] = $heading; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Use the name from the field. |
||
162 | */ |
||
163 | if ($field && $heading = $field->getName()) { |
||
164 | $column['heading'] = $heading; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * If no field look for |
||
169 | * a name anyways. |
||
170 | */ |
||
171 | if (!$field && $this->translator->has( |
||
172 | $heading = $module->getNamespace('field.' . $column['field'] . '.name') |
||
173 | ) |
||
174 | ) { |
||
175 | $column['heading'] = $heading; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * If no translatable heading yet and |
||
180 | * the heading matches the value (default) |
||
181 | * then humanize the heading value. |
||
182 | */ |
||
183 | View Code Duplication | if (!isset($column['heading']) && $this->config->get('streams::locales.lazy')) { |
|
184 | $column['heading'] = $this->string->humanize($column['field']); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * If we have a translatable heading and |
||
189 | * the heading does not have a translation |
||
190 | * then humanize the heading value. |
||
191 | */ |
||
192 | View Code Duplication | if ( |
|
193 | isset($column['heading']) && |
||
194 | str_is('*.*.*::*', $column['heading']) && |
||
195 | !$this->translator->has($column['heading']) && |
||
196 | $this->config->get('streams::locales.lazy') |
||
197 | ) { |
||
198 | $column['heading'] = $this->string->humanize($column['field']); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Last resort. |
||
203 | */ |
||
204 | if (!isset($column['heading'])) { |
||
205 | $column['heading'] = $module->getNamespace('field.' . $column['field'] . '.name'); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | $builder->setColumns($columns); |
||
210 | } |
||
211 | } |
||
212 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.