|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | + |
| 9 | +/** |
| 10 | + * Service class for sorting CognitiveMetricsCollection |
| 11 | + */ |
| 12 | +class CognitiveMetricsSorter |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Available sortable fields |
| 16 | + */ |
| 17 | + private const SORTABLE_FIELDS = [ |
| 18 | + 'score', |
| 19 | + 'halstead', |
| 20 | + 'cyclomatic', |
| 21 | + 'class', |
| 22 | + 'method', |
| 23 | + 'file', |
| 24 | + 'lineCount', |
| 25 | + 'argCount', |
| 26 | + 'returnCount', |
| 27 | + 'variableCount', |
| 28 | + 'propertyCallCount', |
| 29 | + 'ifCount', |
| 30 | + 'ifNestingLevel', |
| 31 | + 'elseCount', |
| 32 | + 'lineCountWeight', |
| 33 | + 'argCountWeight', |
| 34 | + 'returnCountWeight', |
| 35 | + 'variableCountWeight', |
| 36 | + 'propertyCallCountWeight', |
| 37 | + 'ifCountWeight', |
| 38 | + 'ifNestingLevelWeight', |
| 39 | + 'elseCountWeight' |
| 40 | + ]; |
| 41 | + |
| 42 | + /** |
| 43 | + * Sort the metrics collection by the specified field and order |
| 44 | + * |
| 45 | + * @param CognitiveMetricsCollection $collection |
| 46 | + * @param string $sortBy |
| 47 | + * @param string $sortOrder |
| 48 | + * @return CognitiveMetricsCollection |
| 49 | + * @throws InvalidArgumentException |
| 50 | + */ |
| 51 | + public function sort( |
| 52 | + CognitiveMetricsCollection $collection, |
| 53 | + string $sortBy, |
| 54 | + string $sortOrder = 'asc' |
| 55 | + ): CognitiveMetricsCollection { |
| 56 | + if (!in_array($sortBy, self::SORTABLE_FIELDS, true)) { |
| 57 | + throw new InvalidArgumentException( |
| 58 | + sprintf( |
| 59 | + 'Invalid sort field "%s". Available fields: %s', |
| 60 | + $sortBy, |
| 61 | + implode(', ', self::SORTABLE_FIELDS) |
| 62 | + ) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + if (!in_array(strtolower($sortOrder), ['asc', 'desc'], true)) { |
| 67 | + throw new InvalidArgumentException('Sort order must be "asc" or "desc"'); |
| 68 | + } |
| 69 | + |
| 70 | + $metrics = iterator_to_array($collection, true); |
| 71 | + |
| 72 | + // Convert to indexed array for sorting |
| 73 | + $metricsArray = array_values($metrics); |
| 74 | + |
| 75 | + // Sort by values |
| 76 | + usort($metricsArray, function (CognitiveMetrics $alpha, CognitiveMetrics $beta) use ($sortBy, $sortOrder) { |
| 77 | + $valueA = $this->getFieldValue($alpha, $sortBy); |
| 78 | + $valueB = $this->getFieldValue($beta, $sortBy); |
| 79 | + |
| 80 | + $comparison = $this->compareValues($valueA, $valueB); |
| 81 | + |
| 82 | + return strtolower($sortOrder) === 'desc' ? -$comparison : $comparison; |
| 83 | + }); |
| 84 | + |
| 85 | + $sortedCollection = new CognitiveMetricsCollection(); |
| 86 | + foreach ($metricsArray as $metric) { |
| 87 | + $sortedCollection->add($metric); |
| 88 | + } |
| 89 | + |
| 90 | + return $sortedCollection; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get the value of a field from a CognitiveMetrics object |
| 95 | + * |
| 96 | + * @param CognitiveMetrics $metrics |
| 97 | + * @param string $field |
| 98 | + * @return mixed |
| 99 | + */ |
| 100 | + private function getFieldValue(CognitiveMetrics $metrics, string $field): mixed |
| 101 | + { |
| 102 | + return match ($field) { |
| 103 | + 'score' => $metrics->getScore(), |
| 104 | + 'halstead' => $metrics->getHalstead()?->getVolume() ?? 0.0, |
| 105 | + 'cyclomatic' => $metrics->getCyclomatic()?->complexity ?? 0, // @phpstan-ignore-line |
| 106 | + 'class' => $metrics->getClass(), |
| 107 | + 'method' => $metrics->getMethod(), |
| 108 | + 'file' => $metrics->getFileName(), |
| 109 | + 'lineCount' => $metrics->getLineCount(), |
| 110 | + 'argCount' => $metrics->getArgCount(), |
| 111 | + 'returnCount' => $metrics->getReturnCount(), |
| 112 | + 'variableCount' => $metrics->getVariableCount(), |
| 113 | + 'propertyCallCount' => $metrics->getPropertyCallCount(), |
| 114 | + 'ifCount' => $metrics->getIfCount(), |
| 115 | + 'ifNestingLevel' => $metrics->getIfNestingLevel(), |
| 116 | + 'elseCount' => $metrics->getElseCount(), |
| 117 | + 'lineCountWeight' => $metrics->getLineCountWeight(), |
| 118 | + 'argCountWeight' => $metrics->getArgCountWeight(), |
| 119 | + 'returnCountWeight' => $metrics->getReturnCountWeight(), |
| 120 | + 'variableCountWeight' => $metrics->getVariableCountWeight(), |
| 121 | + 'propertyCallCountWeight' => $metrics->getPropertyCallCountWeight(), |
| 122 | + 'ifCountWeight' => $metrics->getIfCountWeight(), |
| 123 | + 'ifNestingLevelWeight' => $metrics->getIfNestingLevelWeight(), |
| 124 | + 'elseCountWeight' => $metrics->getElseCountWeight(), |
| 125 | + default => throw new InvalidArgumentException("Unknown field: $field") |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Compare two values for sorting |
| 131 | + * |
| 132 | + * @param mixed $alpha |
| 133 | + * @param mixed $beta |
| 134 | + * @return int |
| 135 | + */ |
| 136 | + private function compareValues(mixed $alpha, mixed $beta): int |
| 137 | + { |
| 138 | + if (is_numeric($alpha) && is_numeric($beta)) { |
| 139 | + return $alpha <=> $beta; |
| 140 | + } |
| 141 | + |
| 142 | + if (is_string($alpha) && is_string($beta)) { |
| 143 | + return strcasecmp($alpha, $beta); |
| 144 | + } |
| 145 | + |
| 146 | + // Handle mixed types by converting to string |
| 147 | + return strcasecmp((string) $alpha, (string) $beta); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Get all available sortable fields |
| 152 | + * |
| 153 | + * @return array<string> |
| 154 | + */ |
| 155 | + public function getSortableFields(): array |
| 156 | + { |
| 157 | + return self::SORTABLE_FIELDS; |
| 158 | + } |
| 159 | +} |
0 commit comments