Skip to content

Commit e29b789

Browse files
Update cache versioning in CognitiveMetricsCollector and enhance understandability output
- Introduced a new constant `CACHE_VERSION` in `CognitiveMetricsCollector` to manage cache versioning effectively. - Updated cache item versioning to use the new constant, ensuring stale entries are ignored when the analysis result shape changes. - Enhanced the understandability output in various test files by updating complexity ratings, improving clarity in metrics reporting.
1 parent 4c06062 commit e29b789

2 files changed

Lines changed: 37 additions & 24 deletions

File tree

src/Business/Cognitive/CognitiveMetricsCollector.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
*/
2424
class CognitiveMetricsCollector
2525
{
26+
/**
27+
* Bump when cached analysis_result shape changes so stale entries are ignored.
28+
*/
29+
private const CACHE_VERSION = '1.1';
30+
2631
/**
2732
* @var array<string, mixed>
2833
*/
@@ -322,7 +327,7 @@ private function cacheResult(
322327
string $configHash
323328
): void {
324329
$cacheItem->set([
325-
'version' => '1.0',
330+
'version' => self::CACHE_VERSION,
326331
'file_path' => $file->getRealPath(),
327332
'file_mtime' => $file->getMTime(),
328333
'config_hash' => $configHash,
@@ -363,6 +368,14 @@ private function getCachedMetrics(SplFileInfo $file, string $configHash, bool $u
363368
return ['metrics' => null, 'cacheItem' => $cacheItem];
364369
}
365370

371+
if (($cachedData['version'] ?? null) !== self::CACHE_VERSION) {
372+
return ['metrics' => null, 'cacheItem' => $cacheItem];
373+
}
374+
375+
if (($cachedData['config_hash'] ?? null) !== $configHash) {
376+
return ['metrics' => null, 'cacheItem' => $cacheItem];
377+
}
378+
366379
$ignoredItems = $cachedData['ignored_items'] ?? [];
367380
$this->ignoredItems = $this->normalizeIgnoredItems($ignoredItems);
368381
$this->messageBus->dispatch(new FileProcessed($file));

tests/Unit/Command/OutputWithUnderstandability.txt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ File: tests/TestCode/FileWithTwoClasses.php
77
│ Method Name │ Lines │ Arguments │ Returns │ Variables │ Property │ If │ If Nesting │ Else │ Cognitive │ Understandability │
88
│ │ │ │ │ │ Accesses │ │ Level │ │ Complexity │ │
99
├─────────────┼───────┼───────────┼─────────┼───────────┼──────────┼───────┼────────────┼───────┼────────────┼───────────────────┤
10-
│ add │ 4 (0) │ 2 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ -
10+
│ add │ 4 (0) │ 2 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ 0 (low)
1111
└─────────────┴───────┴───────────┴─────────┴───────────┴──────────┴───────┴────────────┴───────┴────────────┴───────────────────┘
1212

1313
Class: \ClassTwo
@@ -16,7 +16,7 @@ File: tests/TestCode/FileWithTwoClasses.php
1616
│ Method Name │ Lines │ Arguments │ Returns │ Variables │ Property │ If │ If Nesting │ Else │ Cognitive │ Understandability │
1717
│ │ │ │ │ │ Accesses │ │ Level │ │ Complexity │ │
1818
├─────────────┼───────┼───────────┼─────────┼───────────┼──────────┼───────┼────────────┼───────┼────────────┼───────────────────┤
19-
│ add │ 4 (0) │ 2 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ -
19+
│ add │ 4 (0) │ 2 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ 0 (low)
2020
└─────────────┴───────┴───────────┴─────────┴───────────┴──────────┴───────┴────────────┴───────┴────────────┴───────────────────┘
2121

2222
Class: \Doctrine\ORM\Tools\Pagination\Paginator
@@ -25,19 +25,19 @@ File: tests/TestCode/Paginator.php
2525
│ Method Name │ Lines │ Arguments │ Returns │ Variables │ Property │ If │ If Nesting │ Else │ Cognitive │ Understandability │
2626
│ │ │ │ │ │ Accesses │ │ Level │ │ Complexity │ │
2727
├───────────────────────────────────────────┼────────┼───────────┼─────────┼───────────┼──────────┼───────┼────────────┼───────────┼────────────┼───────────────────┤
28-
│ __construct │ 10 (0) │ 2 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ -
29-
│ getQuery │ 4 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ -
30-
│ getFetchJoinCollection │ 4 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ -
31-
│ getUseOutputWalkers │ 4 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ -
32-
│ setUseOutputWalkers │ 6 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ -
33-
│ count │ 12 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ -
34-
│ getIterator │ 46 (0) │ 0 (0) │ 2 (0) │ 9 (0.693) │ 2 (0) │ 3 (0) │ 2 (0.693) │ 2 (0.693) │ 2.079 │ -
35-
│ cloneQuery │ 13 (0) │ 1 (0) │ 1 (0) │ 3 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ -
36-
│ useOutputWalker │ 8 (0) │ 1 (0) │ 2 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ -
37-
│ appendTreeWalker │ 11 (0) │ 2 (0) │ 0 (0) │ 1 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ -
38-
│ getCountQuery │ 25 (0) │ 0 (0) │ 1 (0) │ 4 (0) │ 1 (0) │ 2 (0) │ 1 (0) │ 1 (0) │ 0 │ -
39-
│ unbindUnusedQueryParams │ 17 (0) │ 1 (0) │ 0 (0) │ 6 (0.336) │ 0 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0.336 │ -
40-
│ convertWhereInIdentifiersToDatabaseValues │ 11 (0) │ 1 (0) │ 1 (0) │ 5 (0.182) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0.182 │ -
28+
│ __construct │ 10 (0) │ 2 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ 1 (low)
29+
│ getQuery │ 4 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ 0 (low)
30+
│ getFetchJoinCollection │ 4 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ 0 (low)
31+
│ getUseOutputWalkers │ 4 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ 0 (low)
32+
│ setUseOutputWalkers │ 6 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ 0 (low)
33+
│ count │ 12 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ 3 (low)
34+
│ getIterator │ 46 (0) │ 0 (0) │ 2 (0) │ 9 (0.693) │ 2 (0) │ 3 (0) │ 2 (0.693) │ 2 (0.693) │ 2.079 │ 8 (medium)
35+
│ cloneQuery │ 13 (0) │ 1 (0) │ 1 (0) │ 3 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ 1 (low)
36+
│ useOutputWalker │ 8 (0) │ 1 (0) │ 2 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ 1 (low)
37+
│ appendTreeWalker │ 11 (0) │ 2 (0) │ 0 (0) │ 1 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ 1 (low)
38+
│ getCountQuery │ 25 (0) │ 0 (0) │ 1 (0) │ 4 (0) │ 1 (0) │ 2 (0) │ 1 (0) │ 1 (0) │ 0 │ 3 (low)
39+
│ unbindUnusedQueryParams │ 17 (0) │ 1 (0) │ 0 (0) │ 6 (0.336) │ 0 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0.336 │ 4 (low)
40+
│ convertWhereInIdentifiersToDatabaseValues │ 11 (0) │ 1 (0) │ 1 (0) │ 5 (0.182) │ 1 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0.182 │ 0 (low)
4141
└───────────────────────────────────────────┴────────┴───────────┴─────────┴───────────┴──────────┴───────┴────────────┴───────────┴────────────┴───────────────────┘
4242

4343
Class: \WP_Debug_Data
@@ -46,13 +46,13 @@ File: tests/TestCode/WpDebugData.php
4646
│ Method Name │ Lines │ Arguments │ Returns │ Variables │ Property │ If │ If Nesting │ Else │ Cognitive │ Understandability │
4747
│ │ │ │ │ │ Accesses │ │ Level │ │ Complexity │ │
4848
├───────────────────┼──────────────┼───────────┼─────────┼─────────────┼────────────┼────────────┼────────────┼────────────┼────────────┼───────────────────┤
49-
│ check_for_updates │ 6 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ -
50-
│ debug_data │ 1230 (3.867) │ 0 (0) │ 1 (0) │ 105 (3.054) │ 20 (0.726) │ 58 (4.025) │ 3 (1.099) │ 33 (3.497) │ 16.267 │ -
51-
│ get_wp_constants │ 144 (1.472) │ 0 (0) │ 1 (0) │ 9 (0.693) │ 0 (0) │ 5 (1.099) │ 1 (0) │ 5 (1.609) │ 4.874 │ -
52-
│ get_wp_filesystem │ 60 (0) │ 0 (0) │ 1 (0) │ 9 (0.693) │ 0 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0.693 │ -
53-
│ get_mysql_var │ 15 (0) │ 1 (0) │ 2 (0) │ 2 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ -
54-
│ format │ 60 (0) │ 2 (0) │ 1 (0) │ 11 (0.875) │ 0 (0) │ 5 (1.099) │ 1 (0) │ 5 (1.609) │ 3.584 │ -
55-
│ get_database_size │ 14 (0) │ 0 (0) │ 1 (0) │ 4 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ -
56-
│ get_sizes │ 125 (1.281) │ 0 (0) │ 1 (0) │ 14 (1.099) │ 0 (0) │ 9 (1.946) │ 2 (0.693) │ 5 (1.609) │ 6.628 │ -
49+
│ check_for_updates │ 6 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 (0) │ 0 │ 0 (low)
50+
│ debug_data │ 1230 (3.867) │ 0 (0) │ 1 (0) │ 105 (3.054) │ 20 (0.726) │ 58 (4.025) │ 3 (1.099) │ 33 (3.497) │ 16.267 │ 230 (very_high)
51+
│ get_wp_constants │ 144 (1.472) │ 0 (0) │ 1 (0) │ 9 (0.693) │ 0 (0) │ 5 (1.099) │ 1 (0) │ 5 (1.609) │ 4.874 │ 36 (very_high)
52+
│ get_wp_filesystem │ 60 (0) │ 0 (0) │ 1 (0) │ 9 (0.693) │ 0 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0.693 │ 18 (very_high)
53+
│ get_mysql_var │ 15 (0) │ 1 (0) │ 2 (0) │ 2 (0) │ 0 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ 2 (low)
54+
│ format │ 60 (0) │ 2 (0) │ 1 (0) │ 11 (0.875) │ 0 (0) │ 5 (1.099) │ 1 (0) │ 5 (1.609) │ 3.584 │ 41 (very_high)
55+
│ get_database_size │ 14 (0) │ 0 (0) │ 1 (0) │ 4 (0) │ 1 (0) │ 1 (0) │ 1 (0) │ 0 (0) │ 0 │ 3 (low)
56+
│ get_sizes │ 125 (1.281) │ 0 (0) │ 1 (0) │ 14 (1.099) │ 0 (0) │ 9 (1.946) │ 2 (0.693) │ 5 (1.609) │ 6.628 │ 23 (very_high)
5757
└───────────────────┴──────────────┴───────────┴─────────┴─────────────┴────────────┴────────────┴────────────┴────────────┴────────────┴───────────────────┘
5858

0 commit comments

Comments
 (0)