|
| 1 | +# Sorting and Filtering |
| 2 | + |
| 3 | +The cognitive code analysis tool provides powerful sorting and filtering capabilities to help you organize and focus on the most relevant results from your code analysis. |
| 4 | + |
| 5 | +## Sorting Results |
| 6 | + |
| 7 | +You can sort analysis results by various metrics to identify the most complex or problematic code areas. |
| 8 | + |
| 9 | +### Command Line Options |
| 10 | + |
| 11 | +```bash |
| 12 | +bin/phpcca analyse <path-to-folder> --sort-by=<field> --sort-order=<order> |
| 13 | +``` |
| 14 | + |
| 15 | +#### Available Options |
| 16 | + |
| 17 | +- `--sort-by, -s`: Field to sort by (optional) |
| 18 | +- `--sort-order`: Sort order - `asc` (ascending) or `desc` (descending), default: `asc` |
| 19 | + |
| 20 | +### Sortable Fields |
| 21 | + |
| 22 | +The following fields are available for sorting: |
| 23 | + |
| 24 | +| Field | Description | |
| 25 | +|-------|-------------| |
| 26 | +| `score` | Cognitive complexity score | |
| 27 | +| `halstead` | Halstead complexity metrics | |
| 28 | +| `cyclomatic` | Cyclomatic complexity | |
| 29 | +| `class` | Class name (alphabetical) | |
| 30 | +| `method` | Method name (alphabetical) | |
| 31 | +| `lineCount` | Number of lines of code | |
| 32 | +| `argCount` | Number of method arguments | |
| 33 | +| `returnCount` | Number of return statements | |
| 34 | +| `variableCount` | Number of variables used | |
| 35 | +| `propertyCallCount` | Number of property accesses | |
| 36 | +| `ifCount` | Number of if statements | |
| 37 | +| `ifNestingLevel` | Maximum nesting level of if statements | |
| 38 | +| `elseCount` | Number of else statements | |
| 39 | + |
| 40 | +### Examples |
| 41 | + |
| 42 | +Sort by cognitive complexity score (highest first): |
| 43 | +```bash |
| 44 | +bin/phpcca analyse src/ --sort-by=score --sort-order=desc |
| 45 | +``` |
| 46 | + |
| 47 | +Sort by method name alphabetically: |
| 48 | +```bash |
| 49 | +bin/phpcca analyse src/ --sort-by=method --sort-order=asc |
| 50 | +``` |
| 51 | + |
| 52 | +Sort by cyclomatic complexity: |
| 53 | +```bash |
| 54 | +bin/phpcca analyse src/ --sort-by=cyclomatic --sort-order=desc |
| 55 | +``` |
| 56 | + |
| 57 | +## Filtering and Grouping |
| 58 | + |
| 59 | +### Grouping by Class |
| 60 | + |
| 61 | +By default, results are grouped by class to make it easier to understand complexity within specific classes. This behavior can be controlled via configuration: |
| 62 | + |
| 63 | +```yaml |
| 64 | +cognitive: |
| 65 | + groupByClass: true # Default: true |
| 66 | +``` |
| 67 | +
|
| 68 | +- **`true`**: Results are grouped by class, showing separate tables for each class |
| 69 | +- **`false`**: Results are displayed as a flat list without grouping |
| 70 | + |
| 71 | +### Excluding Classes and Methods |
| 72 | + |
| 73 | +You can exclude specific classes and methods from analysis using regex patterns in your configuration file: |
| 74 | + |
| 75 | +```yaml |
| 76 | +cognitive: |
| 77 | + excludePatterns: |
| 78 | + - '(.*)::__construct' # Exclude all constructors |
| 79 | + - '(.*)::toArray' # Exclude all toArray methods |
| 80 | + - '(.*)Transformer::(.*)' # Exclude all methods in Transformer classes |
| 81 | +``` |
| 82 | + |
| 83 | +### Excluding Files |
| 84 | + |
| 85 | +You can exclude entire files from analysis: |
| 86 | + |
| 87 | +```yaml |
| 88 | +cognitive: |
| 89 | + excludeFilePatterns: |
| 90 | + - '.*Cognitive.*' # Exclude files with "Cognitive" in the name |
| 91 | + - '(.*)Test.php' # Exclude all test files |
| 92 | +``` |
| 93 | + |
| 94 | +## Error Handling |
| 95 | + |
| 96 | +If you specify an invalid sort field, the tool will display an error message with the list of available fields: |
| 97 | + |
| 98 | +```bash |
| 99 | +bin/phpcca analyse src/ --sort-by=invalidField |
| 100 | +# Output: Sorting error: Invalid sort field "invalidField". Available fields: score, halstead, cyclomatic, class, method, lineCount, argCount, returnCount, variableCount, propertyCallCount, ifCount, ifNestingLevel, elseCount |
| 101 | +``` |
0 commit comments