Skip to content

Commit f62a2ee

Browse files
committed
transform MissingDocBlockAnalyzer into MissingDocBlockComment
1 parent fe31268 commit f62a2ee

14 files changed

+37
-55
lines changed

comments_density.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
directories: [
2424
'src',
2525
],
26-
docblockConfigDTO: new MissingDocblockConfigDTO(),
26+
docblockConfigDTO: new MissingDocblockConfigDTO(class: true),
2727
disable: []
2828
);

src/AnalyzeComments/Analyzer/Analyzer.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\ConfigDTO;
1313
use SavinMikhail\CommentsDensity\AnalyzeComments\Exception\CommentsDensityException;
1414
use SavinMikhail\CommentsDensity\AnalyzeComments\Metrics\MetricsFacade;
15-
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\MissingDocBlockAnalyzer;
1615
use SavinMikhail\CommentsDensity\Baseline\Storage\BaselineStorageInterface;
1716
use SplFileInfo;
1817
use Symfony\Contracts\Cache\CacheInterface;
@@ -27,7 +26,6 @@ public function __construct(
2726
private readonly ConfigDTO $configDTO,
2827
private readonly CommentTypeFactory $commentFactory,
2928
private readonly MetricsFacade $metrics,
30-
private readonly MissingDocBlockAnalyzer $missingDocBlockAnalyzer,
3129
private readonly BaselineStorageInterface $baselineStorage,
3230
private readonly CacheInterface $cache,
3331
private readonly CommentStatisticsAggregator $statisticsAggregator,
@@ -51,7 +49,6 @@ public function analyze(iterable $files): Report
5149
$task = new CommentFinder(
5250
$this->commentFactory,
5351
$this->configDTO,
54-
$this->missingDocBlockAnalyzer,
5552
);
5653

5754
$fileComments = $this->cache->get(
@@ -88,9 +85,6 @@ private function checkThresholdsExceeded(): bool
8885
if ($this->metrics->hasExceededThreshold()) {
8986
return true;
9087
}
91-
if ($this->missingDocBlockAnalyzer->hasExceededThreshold()) {
92-
return true;
93-
}
9488
foreach ($this->commentFactory->getCommentTypes() as $commentType) {
9589
if ($commentType->hasExceededThreshold()) {
9690
return true;

src/AnalyzeComments/Analyzer/AnalyzerFactory.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use SavinMikhail\CommentsDensity\AnalyzeComments\Metrics\ComToLoc;
1111
use SavinMikhail\CommentsDensity\AnalyzeComments\Metrics\MetricsFacade;
1212
use SavinMikhail\CommentsDensity\AnalyzeComments\Metrics\ResourceUtilization;
13-
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\MissingDocBlockAnalyzer;
1413
use SavinMikhail\CommentsDensity\Baseline\Storage\BaselineStorageInterface;
1514
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
1615

@@ -21,7 +20,6 @@ public function getAnalyzer(
2120
BaselineStorageInterface $baselineStorage,
2221
): Analyzer {
2322
$commentFactory = new CommentTypeFactory($configDto->getAllowedTypes());
24-
$missingDocBlock = new MissingDocBlockAnalyzer($configDto->docblockConfigDTO);
2523
$cds = new CDS($configDto->thresholds, $commentFactory);
2624

2725
$metrics = new MetricsFacade(
@@ -34,10 +32,9 @@ public function getAnalyzer(
3432
$configDto,
3533
$commentFactory,
3634
$metrics,
37-
$missingDocBlock,
3835
$baselineStorage,
3936
new FilesystemAdapter(directory: $configDto->cacheDir),
40-
new CommentStatisticsAggregator($configDto, $commentFactory, $missingDocBlock),
37+
new CommentStatisticsAggregator($configDto, $commentFactory),
4138
);
4239
}
4340
}

src/AnalyzeComments/Analyzer/CommentFinder.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\Visitors\MissingDocBlockVisitor;
1414
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory;
1515
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\ConfigDTO;
16-
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\MissingDocBlockAnalyzer;
16+
1717
use function in_array;
1818

1919
final readonly class CommentFinder
@@ -23,7 +23,6 @@
2323
public function __construct(
2424
private CommentTypeFactory $commentFactory,
2525
private ConfigDTO $configDTO,
26-
private MissingDocBlockAnalyzer $missingDocBlockAnalyzer,
2726
?Parser $parser = null,
2827
) {
2928
$this->parser = $parser ?? (new ParserFactory())->createForHostVersion();
@@ -36,14 +35,11 @@ public function run(string $content, string $filename): array
3635
{
3736
$traverser = new NodeTraverser();
3837

39-
// $nameResolverVisitor = new NameResolver();
40-
// $traverser->addVisitor($nameResolverVisitor);
41-
4238
$missingDocBlockVisitor = new MissingDocBlockVisitor(
4339
$filename,
4440
new NodeNeedsDocblockChecker($this->configDTO->docblockConfigDTO),
4541
);
46-
if ($this->shouldAnalyzeMissingDocBlocks()) {
42+
if (in_array('missingDocBlock', $this->configDTO->getAllowedTypes(), true)) {
4743
$traverser->addVisitor($missingDocBlockVisitor);
4844
}
4945

@@ -57,15 +53,4 @@ public function run(string $content, string $filename): array
5753

5854
return [...$missingDocBlockVisitor->missingDocBlocks, ...$commentVisitor->comments];
5955
}
60-
61-
private function shouldAnalyzeMissingDocBlocks(): bool
62-
{
63-
return
64-
$this->configDTO->getAllowedTypes() === []
65-
|| in_array(
66-
$this->missingDocBlockAnalyzer->getName(),
67-
$this->configDTO->getAllowedTypes(),
68-
true,
69-
);
70-
}
7156
}

src/AnalyzeComments/Analyzer/CommentStatisticsAggregator.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory;
1010
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\ConfigDTO;
1111
use SavinMikhail\CommentsDensity\AnalyzeComments\Exception\CommentsDensityException;
12-
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\MissingDocBlockAnalyzer;
1312

1413
use function substr_count;
1514

@@ -20,7 +19,6 @@
2019
public function __construct(
2120
private ConfigDTO $configDTO,
2221
private CommentTypeFactory $commentFactory,
23-
private MissingDocBlockAnalyzer $missingDocBlock,
2422
) {}
2523

2624
/**
@@ -67,16 +65,6 @@ private function countCommentOccurrences(array $comments): array
6765
*/
6866
private function prepareCommentStatistic(string $type, array $stat): CommentStatisticsDTO
6967
{
70-
if ($type === $this->missingDocBlock->getName()) {
71-
return new CommentStatisticsDTO(
72-
$this->missingDocBlock->getColor(),
73-
$this->missingDocBlock->getName(),
74-
$stat['lines'],
75-
$this->missingDocBlock->getStatColor($stat['count'], $this->configDTO->thresholds),
76-
$stat['count'],
77-
);
78-
}
79-
8068
$commentType = $this->commentFactory->getCommentType($type);
8169
if ($commentType) {
8270
return new CommentStatisticsDTO(

src/AnalyzeComments/Analyzer/Visitors/CommentVisitor.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\Visitors;
46

57
use PhpParser\Node;
68
use PhpParser\NodeVisitorAbstract;
79
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO;
810
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\CommentTypeFactory;
9-
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\MissingDocBlockAnalyzer;
1011

1112
final class CommentVisitor extends NodeVisitorAbstract
1213
{
@@ -38,4 +39,4 @@ public function enterNode(Node $node): null
3839

3940
return null;
4041
}
41-
}
42+
}

src/AnalyzeComments/Analyzer/Visitors/MissingDocBlockVisitor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PhpParser\NodeVisitorAbstract;
99
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO;
1010
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\Visitors\Checkers\NodeNeedsDocblockChecker;
11-
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\MissingDocBlockAnalyzer;
11+
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\MissingDocBlock;
1212

1313
final class MissingDocBlockVisitor extends NodeVisitorAbstract
1414
{
@@ -32,8 +32,8 @@ public function enterNode(Node $node): null
3232

3333
$this->missingDocBlocks[] =
3434
new CommentDTO(
35-
MissingDocBlockAnalyzer::NAME,
36-
MissingDocBlockAnalyzer::COLOR,
35+
MissingDocBlock::NAME,
36+
MissingDocBlock::COLOR,
3737
$this->filename,
3838
$node->getLine(),
3939
'',

src/AnalyzeComments/Comments/CommentTypeFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function __construct(private array $allowedTypes = [])
2222
new RegularComment(),
2323
new LicenseComment(),
2424
new DocBlockComment(),
25+
new MissingDocBlock(),
2526
];
2627
}
2728

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Comments;
6+
7+
final class MissingDocBlock extends Comment
8+
{
9+
public const NAME = 'missingDocblock';
10+
public const COLOR = 'red';
11+
public const PATTERN = '/.*/';
12+
public const WEIGHT = -1;
13+
public const COMPARISON_TYPE = '<=';
14+
}

src/AnalyzeComments/Comments/RegularComment.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
final class RegularComment extends Comment
88
{
99
// phpcs:ignore Generic.Files.LineLength.TooLong
10-
public const PATTERN = '/(#(?!.*\b(?:todo|fixme)\b:?).*?$)|(\/\/(?!.*\b(?:todo|fixme)\b:?).*?$)|\/\*(?!\*)(?!.*\b(?:todo|fixme)\b:?).*?\*\//ms';
10+
public const PATTERN =
11+
'/(#(?!.*\b(?:todo|fixme)\b:?).*?$)|(\/\/(?!.*\b(?:todo|fixme)\b:?).*?$)|\/\*(?!\*)(?!.*\b(?:todo|fixme)\b:?).*?\*\//ms';
1112
public const COLOR = 'red';
1213
public const WEIGHT = -1;
1314
public const COMPARISON_TYPE = '<=';

0 commit comments

Comments
 (0)