| Conditions | 33 |
| Paths | 262 |
| Total Lines | 142 |
| Code Lines | 54 |
| 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\Form\Component\Field\Guesser; |
||
| 50 | public function guess(FormBuilder $builder) |
||
| 51 | { |
||
| 52 | $fields = $builder->getFields(); |
||
| 53 | $stream = $builder->getFormStream(); |
||
| 54 | |||
| 55 | foreach ($fields as &$field) { |
||
|
|
|||
| 56 | $locale = array_get($field, 'locale'); |
||
| 57 | |||
| 58 | /* |
||
| 59 | * If the label are already set then use it. |
||
| 60 | */ |
||
| 61 | if (isset($field['label'])) { |
||
| 62 | if (str_is('*::*', $field['label'])) { |
||
| 63 | $field['label'] = trans($field['label'], [], null, $locale); |
||
| 64 | } |
||
| 65 | |||
| 66 | continue; |
||
| 67 | } |
||
| 68 | |||
| 69 | /* |
||
| 70 | * If we don't have a field then we |
||
| 71 | * can not really guess anything here. |
||
| 72 | */ |
||
| 73 | if (!isset($field['field'])) { |
||
| 74 | continue; |
||
| 75 | } |
||
| 76 | |||
| 77 | /* |
||
| 78 | * No stream means we can't |
||
| 79 | * really do much here. |
||
| 80 | */ |
||
| 81 | if (!$stream instanceof StreamInterface) { |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | |||
| 85 | $assignment = $stream->getAssignment($field['field']); |
||
| 86 | $object = $stream->getField($field['field']); |
||
| 87 | |||
| 88 | /* |
||
| 89 | * No assignment means we still do |
||
| 90 | * not have anything to do here. |
||
| 91 | */ |
||
| 92 | if (!$assignment instanceof AssignmentInterface) { |
||
| 93 | continue; |
||
| 94 | } |
||
| 95 | |||
| 96 | /* |
||
| 97 | * Next try using the fallback assignment |
||
| 98 | * label system as generated verbatim. |
||
| 99 | */ |
||
| 100 | $label = $assignment->getLabel() . '.default'; |
||
| 101 | |||
| 102 | if (!isset($field['label']) && str_is('*::*', $label) && trans()->has( |
||
| 103 | $label, |
||
| 104 | $locale |
||
| 105 | ) |
||
| 106 | ) { |
||
| 107 | $field['label'] = trans($label, [], null, $locale); |
||
| 108 | } |
||
| 109 | |||
| 110 | /* |
||
| 111 | * Next try using the default assignment |
||
| 112 | * label system as generated verbatim. |
||
| 113 | */ |
||
| 114 | $label = $assignment->getLabel(); |
||
| 115 | |||
| 116 | if ( |
||
| 117 | !isset($field['label']) |
||
| 118 | && str_is('*::*', $label) |
||
| 119 | && trans()->has($label, $locale) |
||
| 120 | && is_string($translated = trans($label, [], null, $locale)) |
||
| 121 | ) { |
||
| 122 | $field['label'] = $translated; |
||
| 123 | } |
||
| 124 | |||
| 125 | /* |
||
| 126 | * Check if it's just a standard string. |
||
| 127 | */ |
||
| 128 | if (!isset($field['label']) && $label && !str_is('*::*', $label)) { |
||
| 129 | $field['label'] = $label; |
||
| 130 | } |
||
| 131 | |||
| 132 | /* |
||
| 133 | * Next try using the generic assignment |
||
| 134 | * label system without the stream identifier. |
||
| 135 | */ |
||
| 136 | $label = explode('.', $assignment->getLabel()); |
||
| 137 | |||
| 138 | array_pop($label); |
||
| 139 | |||
| 140 | $label = implode('.', $label); |
||
| 141 | |||
| 142 | if ( |
||
| 143 | !isset($field['label']) |
||
| 144 | && str_is('*::*', $label) |
||
| 145 | && trans()->has($label, $locale) |
||
| 146 | && is_string($translated = trans($label, [], null, $locale)) |
||
| 147 | ) { |
||
| 148 | $field['label'] = $translated; |
||
| 149 | } |
||
| 150 | |||
| 151 | /* |
||
| 152 | * Check if it's just a standard string. |
||
| 153 | */ |
||
| 154 | if (!isset($field['label']) && $label && !str_is('*::*', $label)) { |
||
| 155 | $field['label'] = $label; |
||
| 156 | } |
||
| 157 | |||
| 158 | /* |
||
| 159 | * Next try using the default field |
||
| 160 | * label system as generated verbatim. |
||
| 161 | */ |
||
| 162 | $label = $object->getName(); |
||
| 163 | |||
| 164 | if ( |
||
| 165 | !isset($field['label']) |
||
| 166 | && str_is('*::*', $label) |
||
| 167 | && trans()->has($label, $locale) |
||
| 168 | && is_string($translated = trans($label, [], null, $locale)) |
||
| 169 | ) { |
||
| 170 | $field['label'] = $translated; |
||
| 171 | } |
||
| 172 | |||
| 173 | /* |
||
| 174 | * Check if it's just a standard string. |
||
| 175 | */ |
||
| 176 | if (!isset($field['label']) && $label && !str_is('*::*', $label)) { |
||
| 177 | $field['label'] = $label; |
||
| 178 | } |
||
| 179 | |||
| 180 | /* |
||
| 181 | * If the field is still untranslated and |
||
| 182 | * we're not debugging then humanize the slug |
||
| 183 | * in leu of displaying an untranslated key. |
||
| 184 | */ |
||
| 185 | if (!isset($field['label']) && $this->config->get('streams::system.lazy_translations')) { |
||
| 186 | $field['label'] = ucwords($this->string->humanize($field['field'])); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $builder->setFields($fields); |
||
| 191 | } |
||
| 192 | } |
||
| 193 |
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.