Skip to content

Commit bf87a7f

Browse files
Add sorting and filtering documentation to README and introduce grouping results by class in Configuration
1 parent b037229 commit bf87a7f

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

docs/Configuration.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,17 @@ cognitive:
7979
showHalsteadComplexity: false
8080
showCyclomaticComplexity: false
8181
```
82+
83+
## Grouping Results by Class
84+
85+
You can control how the analysis results are displayed by setting the `groupByClass` option.
86+
87+
```yaml
88+
cognitive:
89+
groupByClass: true
90+
```
91+
92+
- **`true` (default)**: Results are grouped by class, showing all methods within each class together
93+
- **`false`**: Results are displayed as a flat list without grouping
94+
95+
When `groupByClass` is enabled, the output will show separate tables for each class, making it easier to understand the complexity within specific classes. When disabled, all methods are shown in a single table sorted by their complexity score.

docs/Sorting-and-Filtering.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
```

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ bin/phpcca churn <path-to-folder>
5151
* [Metrics Collected](./docs/Cognitive-Complexity-Analysis.md#metrics-collected)
5252
* [Result Interpretation](./docs/Cognitive-Complexity-Analysis.md#result-interpretation)
5353
* [Churn - Finding Hotspots](./docs/Churn-Finding-Hotspots.md)
54+
* [Sorting and Filtering](./docs/Sorting-and-Filtering.md)
5455
* [Configuration](./docs/Configuration.md#configuration)
5556
* [Tuning the calculation](./docs/Configuration.md#tuning-the-calculation)
5657
* [Examples](#examples)

0 commit comments

Comments
 (0)