Total Complexity | 50 |
Total Lines | 502 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AddLineItem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AddLineItem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class AddLineItem implements GridField_ActionProvider, GridField_HTMLProvider, GridField_URLHandler |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * HTML Fragment to render the field. |
||
37 | * |
||
38 | * @var string |
||
39 | **/ |
||
40 | protected $targetFragment; |
||
41 | |||
42 | /** |
||
43 | * Default field to create the dataobject by should be Title. |
||
44 | * |
||
45 | * @var string |
||
46 | **/ |
||
47 | protected $dataObjectField = "Title"; |
||
48 | |||
49 | /** |
||
50 | * @var string SSViewer template to render the results presentation |
||
51 | */ |
||
52 | protected $results_format = '$Title'; |
||
53 | |||
54 | /** |
||
55 | * Default field to create the dataobject from. |
||
56 | * |
||
57 | * @var string |
||
58 | **/ |
||
59 | protected $source_class = "Product"; |
||
60 | |||
61 | public function getSourceClass() |
||
62 | { |
||
63 | return $this->source_class; |
||
64 | } |
||
65 | |||
66 | public function setSourceClass($class) |
||
67 | { |
||
68 | $this->source_class = $class; |
||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * When we check for existing items, should we check based on all |
||
74 | * filters or any of the chosen (setting this to true uses |
||
75 | * $list->filter() where as false uses $list->filterAny()) |
||
76 | * |
||
77 | * @var boolean |
||
78 | */ |
||
79 | protected $strict_filter = true; |
||
80 | |||
81 | /** |
||
82 | * Getter for strict_filter param |
||
83 | * |
||
84 | * @return boolean |
||
85 | */ |
||
86 | public function getStrictFilter() |
||
87 | { |
||
88 | return $this->strict_filter; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Setter for strict_filter param |
||
93 | * |
||
94 | * @param boolean $bool |
||
95 | * @return void |
||
96 | */ |
||
97 | public function setStrictFilter($bool) |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Fields that we try and find our source object based on |
||
105 | * |
||
106 | * @var array |
||
107 | **/ |
||
108 | protected $filter_fields = [ |
||
109 | "Title", |
||
110 | "StockID" |
||
111 | ]; |
||
112 | |||
113 | public function getFilterFields() |
||
114 | { |
||
115 | return $this->filter_fields; |
||
116 | } |
||
117 | |||
118 | public function setFilterFields($fields) |
||
119 | { |
||
120 | $this->filter_fields = $fields; |
||
121 | return $this; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Fields that we use to filter items for our autocomplete |
||
126 | * |
||
127 | * @var array |
||
128 | **/ |
||
129 | protected $autocomplete_fields = [ |
||
130 | "Title", |
||
131 | "StockID" |
||
132 | ]; |
||
133 | |||
134 | public function getAutocompleteFields() |
||
135 | { |
||
136 | return $this->autocomplete_fields; |
||
137 | } |
||
138 | |||
139 | public function setAutocompleteFields($fields) |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * If filter fails, set this field when creating |
||
147 | * |
||
148 | * @var String |
||
149 | **/ |
||
150 | protected $create_field = "Title"; |
||
151 | |||
152 | public function getCreateField() |
||
153 | { |
||
154 | return $this->create_field; |
||
155 | } |
||
156 | |||
157 | public function setCreateField($field) |
||
158 | { |
||
159 | $this->create_field = $field; |
||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Fields that we are mapping from the source object to our item |
||
165 | * |
||
166 | * @var array |
||
167 | **/ |
||
168 | protected $source_fields = [ |
||
169 | "Title" => "Title", |
||
170 | "StockID" => "StockID", |
||
171 | "Price" => "BasePrice" |
||
172 | ]; |
||
173 | |||
174 | /** |
||
175 | * Get list of source fields |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | public function getSourceFields() |
||
180 | { |
||
181 | return $this->source_fields; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Set the list of source fields |
||
186 | * |
||
187 | * @param $fields |
||
188 | * @return AddLineItem |
||
189 | */ |
||
190 | public function setSourceFields($fields) |
||
191 | { |
||
192 | $this->source_fields = $fields; |
||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * This is the field that we attempt to match a TAX rate to |
||
198 | * when setting up an order item |
||
199 | * |
||
200 | * @var string |
||
201 | **/ |
||
202 | protected $source_tax_field = "TaxRate"; |
||
203 | |||
204 | /** |
||
205 | * @return array |
||
206 | */ |
||
207 | public function getSourceTaxField() |
||
208 | { |
||
209 | return $this->source_tax_field; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param $field |
||
214 | * @return AddLineItem |
||
215 | */ |
||
216 | public function setSourceTaxField($field) |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Number of results to appear in autocomplete |
||
224 | * |
||
225 | * @var int |
||
226 | */ |
||
227 | protected $results_limit = 20; |
||
228 | |||
229 | public function getResultsLimit() |
||
230 | { |
||
231 | return $this->results_limit; |
||
232 | } |
||
233 | |||
234 | public function setResultsLimit($fields) |
||
235 | { |
||
236 | $this->results_limit = $fields; |
||
237 | return $this; |
||
238 | } |
||
239 | |||
240 | public function __construct($targetFragment = 'before', $dataObjectField = "Title") |
||
241 | { |
||
242 | $this->targetFragment = $targetFragment; |
||
243 | $this->dataObjectField = (string) $dataObjectField; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * |
||
248 | * @param GridField $gridField |
||
249 | * @return array |
||
250 | */ |
||
251 | public function getURLHandlers($gridField) |
||
252 | { |
||
253 | return [ |
||
254 | 'search' => 'doSearch', |
||
255 | ]; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Provide actions to this component. |
||
260 | * |
||
261 | * @param $gridField GridField |
||
262 | * |
||
263 | * @return array |
||
264 | **/ |
||
265 | public function getActions($gridField) |
||
268 | } |
||
269 | |||
270 | |||
271 | /** |
||
272 | * Handles the add action for the given DataObject |
||
273 | * |
||
274 | * @param $grid GridFIeld |
||
275 | * @param $actionName string |
||
276 | * @param $arguments mixed |
||
277 | * @param $data array |
||
278 | **/ |
||
279 | public function handleAction(GridField $grid, $actionName, $arguments, $data) |
||
389 | } |
||
390 | } |
||
391 | |||
392 | |||
393 | |||
394 | /** |
||
395 | * Renders the TextField and add button to the GridField. |
||
396 | * |
||
397 | * @param $girdField GridField |
||
398 | * |
||
399 | * @return string HTML |
||
400 | **/ |
||
401 | public function getHTMLFragments($grid) |
||
456 | ]; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Returns a json array of a search results that can be used by for |
||
461 | * example Jquery.ui.autosuggestion |
||
462 | * |
||
463 | * @param GridField $gridField |
||
464 | * @param SS_HTTPRequest $request |
||
465 | */ |
||
466 | public function doSearch($gridField, $request) |
||
467 | { |
||
468 | $product_class = $this->getSourceClass(); |
||
469 | $params = array(); |
||
470 | |||
471 | // Do we have filter fields setup? |
||
472 | if ($this->getAutocompleteFields()) { |
||
473 | $search_fields = $this->getAutocompleteFields(); |
||
474 | } else { |
||
475 | $search_fields = $this->scaffoldSearchFields($product_class); |
||
476 | } |
||
477 | |||
478 | if (!$search_fields) { |
||
479 | throw new LogicException( |
||
480 | sprintf('GridFieldAddExistingAutocompleter: No searchable fields could be found for class "%s"', |
||
481 | $product_class) |
||
482 | ); |
||
483 | } |
||
484 | |||
485 | foreach ($search_fields as $search_field) { |
||
486 | $name = (strpos($search_field, ':') !== false) ? $search_field : "$search_field:StartsWith"; |
||
487 | $params[$name] = $request->getVar('gridfieldaddbydbfield'); |
||
488 | } |
||
489 | |||
490 | $json = array(); |
||
491 | |||
492 | if (class_exists($product_class)) { |
||
493 | $results = DataList::create($product_class) |
||
494 | ->filterAny($params) |
||
495 | ->sort(strtok($search_fields[0], ':'), 'ASC') |
||
496 | ->limit($this->getResultsLimit()); |
||
497 | |||
498 | $originalSourceFileComments = SSViewer::config()->get('source_file_comments'); |
||
499 | |||
500 | SSViewer::config()->update('source_file_comments', false); |
||
501 | |||
502 | foreach ($results as $result) { |
||
503 | $json[$result->ID] = html_entity_decode(SSViewer::fromString($this->results_format)->process($result)); |
||
504 | } |
||
505 | |||
506 | SSViewer::config()->update('source_file_comments', $originalSourceFileComments); |
||
507 | } |
||
508 | |||
509 | return Convert::array2json($json); |
||
510 | } |
||
511 | |||
512 | |||
513 | |||
514 | /** |
||
515 | * Returns the database field for which we'll add the new data object. |
||
516 | * |
||
517 | * @return string |
||
518 | **/ |
||
519 | public function getDataObjectField() |
||
522 | } |
||
523 | |||
524 | |||
525 | |||
526 | /** |
||
527 | * Set the database field. |
||
528 | * |
||
529 | * @param $field string |
||
530 | **/ |
||
531 | public function setDataObjectField($field) |
||
534 | } |
||
535 | } |
||
536 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths