| Conditions | 22 |
| Paths | 3768 |
| Total Lines | 122 |
| Lines | 16 |
| Ratio | 13.11 % |
| 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 |
||
| 14 | public function view() |
||
| 15 | { |
||
| 16 | View Code Duplication | if (class_exists('FLang')) { |
|
| 17 | try { |
||
| 18 | FLang::setMainLang(Lang::get()); |
||
| 19 | FLang::setLangCode(Lang::get(), ''); |
||
| 20 | } catch (Exception $ex) { } |
||
|
|
|||
| 21 | } |
||
| 22 | |||
| 23 | $section = General::sanitize($this->_context[0]); |
||
| 24 | $sectionId = SectionManager::fetchIDFromHandle($section); |
||
| 25 | $sectionId = General::intval($sectionId); |
||
| 26 | $excludes = !isset($this->_context[1]) ? [] : explode(',', General::sanitize($this->_context[1])); |
||
| 27 | |||
| 28 | if ($sectionId < 1) { |
||
| 29 | $this->_Result['status'] = Page::HTTP_STATUS_BAD_REQUEST; |
||
| 30 | $this->_Result['error'] = __('No section id found'); |
||
| 31 | return; |
||
| 32 | } |
||
| 33 | |||
| 34 | $section = (new SectionManager) |
||
| 35 | ->select() |
||
| 36 | ->section($sectionId) |
||
| 37 | ->execute() |
||
| 38 | ->next(); |
||
| 39 | |||
| 40 | View Code Duplication | if (empty($section)) { |
|
| 41 | $this->_Result['status'] = Page::HTTP_STATUS_NOT_FOUND; |
||
| 42 | $this->_Result['error'] = __('Section not found'); |
||
| 43 | return; |
||
| 44 | } |
||
| 45 | |||
| 46 | $query = trim(General::sanitize($_GET['query'])); |
||
| 47 | $entries = array(); |
||
| 48 | $filterableFields = $section->fetchFilterableFields(); |
||
| 49 | View Code Duplication | if (empty($filterableFields)) { |
|
| 50 | $this->_Result['status'] = Page::HTTP_STATUS_BAD_REQUEST; |
||
| 51 | $this->_Result['error'] = __('Section not filterable'); |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | foreach ($filterableFields as $key => $field) { |
||
| 56 | if ($field instanceof FieldRelationship || $field instanceof FieldDate) { |
||
| 57 | unset($filterableFields[$key]); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | $filterableFields = array_values($filterableFields); |
||
| 61 | if (count($filterableFields) > self::MAX_FILTERABLE_FIELDS) { |
||
| 62 | $filterableFields = array_slice($filterableFields, 0, self::MAX_FILTERABLE_FIELDS); |
||
| 63 | } |
||
| 64 | |||
| 65 | $primaryField = $section->fetchVisibleColumns(); |
||
| 66 | |||
| 67 | $getId = function ($obj) { return $obj->get('id'); }; |
||
| 68 | $intersection = array_intersect(array_map($getId, $filterableFields), array_map($getId, $primaryField)); |
||
| 69 | |||
| 70 | if (empty($primaryField)) { |
||
| 71 | $primaryField = current($filterableFields); |
||
| 72 | reset($filterableFields); |
||
| 73 | } else { |
||
| 74 | $primaryField = current($primaryField); |
||
| 75 | } |
||
| 76 | |||
| 77 | foreach ($filterableFields as $field) { |
||
| 78 | if (!empty($intersection) && !in_array($field->get('id'), $intersection)) { |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | $q = (new EntryManager) |
||
| 82 | ->select() |
||
| 83 | ->sort('system:id', 'asc') |
||
| 84 | ->schema([$primaryField->get('element_name')]) |
||
| 85 | ->section($sectionId) |
||
| 86 | ->disableDefaultSort() |
||
| 87 | ->limit(self::MAX_RESULT); |
||
| 88 | |||
| 89 | if (!empty($query)) { |
||
| 90 | try { |
||
| 91 | $opt = array_map(function ($op) { |
||
| 92 | return trim($op['filter']); |
||
| 93 | }, $field->fetchFilterableOperators()); |
||
| 94 | if (in_array('contains:', $opt)) { |
||
| 95 | $q->filter($field, ['contains: ' . $query . '%']); |
||
| 96 | } elseif (in_array('regexp:', $opt)) { |
||
| 97 | $q->filter($field, ['regexp: ' . $query]); |
||
| 98 | } else { |
||
| 99 | $q->filter($field, [$query]); |
||
| 100 | } |
||
| 101 | } catch (DatabaseStatementException $ex) { |
||
| 102 | continue; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | if (!empty($excludes)) { |
||
| 107 | $q->filter('system:id', ['not:' . implode(',', $excludes)]); |
||
| 108 | } |
||
| 109 | |||
| 110 | $fEntries = $q |
||
| 111 | ->execute() |
||
| 112 | ->rows(); |
||
| 113 | |||
| 114 | if (!empty($fEntries)) { |
||
| 115 | $entries = array_merge($entries, $fEntries); |
||
| 116 | $excludes = array_merge($excludes, array_map($getId, $fEntries)); |
||
| 117 | } |
||
| 118 | |||
| 119 | if (count($entries) > self::MAX_RESULT) { |
||
| 120 | break; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | $entries = array_map(function ($entry) use ($primaryField) { |
||
| 125 | return array( |
||
| 126 | 'value' => $entry->get('id') . ':' . |
||
| 127 | $primaryField->prepareReadableValue( |
||
| 128 | $entry->getData($primaryField->get('id')), |
||
| 129 | $entry->get('id') |
||
| 130 | ), |
||
| 131 | ); |
||
| 132 | }, $entries); |
||
| 133 | |||
| 134 | $this->_Result['entries'] = $entries; |
||
| 135 | } |
||
| 136 | } |
||
| 137 |