Conditions | 28 |
Paths | 182 |
Total Lines | 104 |
Code Lines | 68 |
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 |
||
112 | public function getHTMLFragments($gridField) |
||
113 | { |
||
114 | $list = $gridField->getList(); |
||
115 | if (!$this->checkDataType($list)) { |
||
116 | return null; |
||
117 | } |
||
118 | /** @var Sortable $list */ |
||
119 | $forTemplate = new ArrayData([]); |
||
120 | $forTemplate->Fields = new ArrayList; |
||
|
|||
121 | |||
122 | $state = $this->getState($gridField); |
||
123 | $columns = $gridField->getColumns(); |
||
124 | $currentColumn = 0; |
||
125 | |||
126 | $schema = DataObject::getSchema(); |
||
127 | foreach ($columns as $columnField) { |
||
128 | $currentColumn++; |
||
129 | $metadata = $gridField->getColumnMetadata($columnField); |
||
130 | $fieldName = str_replace('.', '-', $columnField ?: ''); |
||
131 | $title = $metadata['title']; |
||
132 | |||
133 | if (isset($this->fieldSorting[$columnField]) && $this->fieldSorting[$columnField]) { |
||
134 | $columnField = $this->fieldSorting[$columnField]; |
||
135 | } |
||
136 | |||
137 | $allowSort = ($title && $list->canSortBy($columnField)); |
||
138 | |||
139 | if (!$allowSort && strpos((string) $columnField, '.') !== false) { |
||
140 | // we have a relation column with dot notation |
||
141 | // @see DataObject::relField for approximation |
||
142 | $parts = explode('.', (string) $columnField); |
||
143 | $tmpItem = singleton($list->dataClass()); |
||
144 | for ($idx = 0; $idx < sizeof($parts ?: []); $idx++) { |
||
145 | $methodName = $parts[$idx]; |
||
146 | if ($tmpItem instanceof SS_List) { |
||
147 | // It's impossible to sort on a HasManyList/ManyManyList |
||
148 | break; |
||
149 | } elseif ($tmpItem && method_exists($tmpItem, 'hasMethod') && $tmpItem->hasMethod($methodName)) { |
||
150 | // The part is a relation name, so get the object/list from it |
||
151 | $tmpItem = $tmpItem->$methodName(); |
||
152 | } elseif ($tmpItem instanceof DataObject |
||
153 | && $schema->fieldSpec($tmpItem, $methodName, DataObjectSchema::DB_ONLY) |
||
154 | ) { |
||
155 | // Else, if we've found a database field at the end of the chain, we can sort on it. |
||
156 | // If a method is applied further to this field (E.g. 'Cost.Currency') then don't try to sort. |
||
157 | $allowSort = $idx === sizeof($parts ?: []) - 1; |
||
158 | break; |
||
159 | } else { |
||
160 | // If neither method nor field, then unable to sort |
||
161 | break; |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | if ($allowSort) { |
||
167 | $dir = 'asc'; |
||
168 | if ($state->SortColumn(null) == $columnField && $state->SortDirection('asc') == 'asc') { |
||
169 | $dir = 'desc'; |
||
170 | } |
||
171 | |||
172 | $field = GridField_FormAction::create( |
||
173 | $gridField, |
||
174 | 'SetOrder' . $fieldName, |
||
175 | $title, |
||
176 | "sort$dir", |
||
177 | ['SortColumn' => $columnField] |
||
178 | )->addExtraClass('grid-field__sort'); |
||
179 | |||
180 | if ($state->SortColumn(null) == $columnField) { |
||
181 | $field->addExtraClass('ss-gridfield-sorted'); |
||
182 | |||
183 | if ($state->SortDirection('asc') == 'asc') { |
||
184 | $field->addExtraClass('ss-gridfield-sorted-asc'); |
||
185 | } else { |
||
186 | $field->addExtraClass('ss-gridfield-sorted-desc'); |
||
187 | } |
||
188 | } |
||
189 | } else { |
||
190 | if ($currentColumn == count($columns ?: [])) { |
||
191 | $filter = $gridField->getConfig()->getComponentByType(GridFieldFilterHeader::class); |
||
192 | |||
193 | if ($filter && $filter->useLegacyFilterHeader && $filter->canFilterAnyColumns($gridField)) { |
||
194 | $field = new LiteralField( |
||
195 | $fieldName, |
||
196 | sprintf( |
||
197 | '<button type="button" name="showFilter" aria-label="%s" title="%s"' . |
||
198 | ' class="btn btn-secondary font-icon-search btn--no-text btn--icon-large grid-field__filter-open"></button>', |
||
199 | _t('SilverStripe\\Forms\\GridField\\GridField.OpenFilter', "Open search and filter"), |
||
200 | _t('SilverStripe\\Forms\\GridField\\GridField.OpenFilter', "Open search and filter") |
||
201 | ) |
||
202 | ); |
||
203 | } else { |
||
204 | $field = new LiteralField($fieldName, '<span class="non-sortable">' . $title . '</span>'); |
||
205 | } |
||
206 | } else { |
||
207 | $field = new LiteralField($fieldName, '<span class="non-sortable">' . $title . '</span>'); |
||
208 | } |
||
209 | } |
||
210 | $forTemplate->Fields->push($field); |
||
211 | } |
||
212 | |||
213 | $template = SSViewer::get_templates_by_class($this, '_Row', __CLASS__); |
||
214 | return [ |
||
215 | 'header' => $forTemplate->renderWith($template), |
||
216 | ]; |
||
292 |