| Conditions | 30 |
| Paths | 268 |
| Total Lines | 130 |
| Code Lines | 45 |
| 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 namespace Anomaly\Streams\Platform\Ui\Table\Component\Header\Guesser; |
||
| 69 | public function guess(TableBuilder $builder) |
||
| 70 | { |
||
| 71 | $columns = $builder->getColumns(); |
||
| 72 | $stream = $builder->getTableStream(); |
||
| 73 | |||
| 74 | $module = $this->modules->active(); |
||
| 75 | |||
| 76 | foreach ($columns as &$column) { |
||
|
|
|||
| 77 | |||
| 78 | /* |
||
| 79 | * If the heading is already set then |
||
| 80 | * we don't have anything to do. |
||
| 81 | */ |
||
| 82 | if (isset($column['heading'])) { |
||
| 83 | continue; |
||
| 84 | } |
||
| 85 | |||
| 86 | /* |
||
| 87 | * If the heading is false, then no |
||
| 88 | * header is desired at all. |
||
| 89 | */ |
||
| 90 | if (isset($column['heading']) && $column['heading'] === false) { |
||
| 91 | continue; |
||
| 92 | } |
||
| 93 | |||
| 94 | /* |
||
| 95 | * No stream means we can't |
||
| 96 | * really do much here. |
||
| 97 | */ |
||
| 98 | if (!$stream instanceof StreamInterface) { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | if (!isset($column['field']) && is_string($column['value'])) { |
||
| 103 | $column['field'] = $column['value']; |
||
| 104 | } |
||
| 105 | |||
| 106 | /* |
||
| 107 | * If the heading matches a field |
||
| 108 | * with dot format then reduce it. |
||
| 109 | */ |
||
| 110 | if (isset($column['field']) && preg_match("/^entry.([a-zA-Z\\_]+)/", $column['field'], $match)) { |
||
| 111 | $column['field'] = $match[1]; |
||
| 112 | } |
||
| 113 | |||
| 114 | /* |
||
| 115 | * Detect some built in columns. |
||
| 116 | */ |
||
| 117 | if (in_array($column['field'], ['id', 'created_at', 'created_by', 'updated_at', 'updated_by'])) { |
||
| 118 | $column['heading'] = 'streams::entry.' . $column['field']; |
||
| 119 | |||
| 120 | continue; |
||
| 121 | } |
||
| 122 | |||
| 123 | /* |
||
| 124 | * Detect entry title. |
||
| 125 | */ |
||
| 126 | if (in_array($column['field'], ['view_link', 'edit_link']) && $field = $stream->getTitleField()) { |
||
| 127 | $column['heading'] = $field->getName(); |
||
| 128 | |||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | $field = $stream->getField(array_get($column, 'field')); |
||
| 133 | |||
| 134 | /* |
||
| 135 | * Detect the title column. |
||
| 136 | */ |
||
| 137 | $title = $stream->getTitleField(); |
||
| 138 | |||
| 139 | if ( |
||
| 140 | $title && |
||
| 141 | !$field && |
||
| 142 | $column['field'] == 'title' && |
||
| 143 | $this->translator->has($heading = $title->getName()) |
||
| 144 | ) { |
||
| 145 | $column['heading'] = $heading; |
||
| 146 | } |
||
| 147 | |||
| 148 | /* |
||
| 149 | * Use the name from the field. |
||
| 150 | */ |
||
| 151 | if ($field && $heading = $field->getName()) { |
||
| 152 | $column['heading'] = $heading; |
||
| 153 | } |
||
| 154 | |||
| 155 | /* |
||
| 156 | * If no field look for |
||
| 157 | * a name anyways. |
||
| 158 | */ |
||
| 159 | if ($module && !$field && $this->translator->has( |
||
| 160 | $heading = $module->getNamespace('field.' . $column['field'] . '.name') |
||
| 161 | ) |
||
| 162 | ) { |
||
| 163 | $column['heading'] = $heading; |
||
| 164 | } |
||
| 165 | |||
| 166 | /* |
||
| 167 | * If no translatable heading yet and |
||
| 168 | * the heading matches the value (default) |
||
| 169 | * then humanize the heading value. |
||
| 170 | */ |
||
| 171 | if (!isset($column['heading']) && $this->config->get('streams::system.lazy_translations')) { |
||
| 172 | $column['heading'] = ucwords($this->string->humanize($column['field'])); |
||
| 173 | } |
||
| 174 | |||
| 175 | /* |
||
| 176 | * If we have a translatable heading and |
||
| 177 | * the heading does not have a translation |
||
| 178 | * then humanize the heading value. |
||
| 179 | */ |
||
| 180 | if ( |
||
| 181 | isset($column['heading']) && |
||
| 182 | str_is('*.*.*::*', $column['heading']) && |
||
| 183 | !$this->translator->has($column['heading']) && |
||
| 184 | $this->config->get('streams::system.lazy_translations') |
||
| 185 | ) { |
||
| 186 | $column['heading'] = ucwords($this->string->humanize($column['field'])); |
||
| 187 | } |
||
| 188 | |||
| 189 | /* |
||
| 190 | * Last resort. |
||
| 191 | */ |
||
| 192 | if ($module && !isset($column['heading'])) { |
||
| 193 | $column['heading'] = $module->getNamespace('field.' . $column['field'] . '.name'); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | $builder->setColumns($columns); |
||
| 198 | } |
||
| 199 | } |
||
| 200 |
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.