| Conditions | 14 |
| Paths | 85 |
| Total Lines | 58 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 93 | private function performSort($keyNumber, $dataToSort) |
||
| 94 | { |
||
| 95 | if (count($dataToSort) < 2) { |
||
| 96 | return $dataToSort; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** @var Key $key */ |
||
| 100 | $key = $this->sortingKeys->get($keyNumber); |
||
| 101 | $variable = $key->getVariable(); |
||
| 102 | $groupedItems = []; |
||
| 103 | |||
| 104 | if ($key->isDateVariable()) { |
||
| 105 | if (DateHelper::hasDateRanges($dataToSort, $variable, "all")) { |
||
| 106 | $newKey = clone $key; |
||
| 107 | $newKey->setRangePart(2); |
||
| 108 | $key->setRangePart(1); |
||
| 109 | $this->sortingKeys->add($newKey); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | //grouping by value |
||
| 114 | foreach ($dataToSort as $citationNumber => $dataItem) { |
||
| 115 | if ($key->isNameVariable()) { |
||
| 116 | $sortKey = Variables::nameHash($dataItem, $variable); |
||
| 117 | } elseif ($key->isNumberVariable()) { |
||
| 118 | $sortKey = $dataItem->{$variable}; |
||
| 119 | } elseif ($key->isDateVariable()) { |
||
| 120 | $sortKey = DateHelper::getSortKeyDate($dataItem, $key); |
||
| 121 | } elseif ($key->isMacro()) { |
||
| 122 | $sortKey = mb_strtolower(strip_tags(CiteProc::getContext()->getMacro( |
||
| 123 | $key->getMacro() |
||
| 124 | )->render($dataItem, $citationNumber))); |
||
| 125 | } elseif ($variable === "citation-number") { |
||
| 126 | $sortKey = $citationNumber + 1; |
||
| 127 | } else { |
||
| 128 | $sortKey = mb_strtolower(strip_tags($dataItem->{$variable})); |
||
| 129 | } |
||
| 130 | $groupedItems[$sortKey][] = $dataItem; |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($this->sortingKeys->count() > ++$keyNumber) { |
||
| 134 | foreach ($groupedItems as $group => &$array) { |
||
| 135 | if (count($array) > 1) { |
||
| 136 | $array = $this->performSort($keyNumber, $array); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | //sorting by array keys |
||
| 142 | if ($key->getSort() === "ascending") { |
||
| 143 | ksort($groupedItems); //ascending |
||
| 144 | } else { |
||
| 145 | krsort($groupedItems); //reverse |
||
| 146 | } |
||
| 147 | |||
| 148 | //the flattened array is the result |
||
| 149 | $sortedDataGroups = array_values($groupedItems); |
||
| 150 | return $this->flatten($sortedDataGroups); |
||
| 151 | } |
||
| 170 |