Skip to content

Commit 35e4a12

Browse files
committed
introduce plugin interface
1 parent 17dfacd commit 35e4a12

File tree

6 files changed

+86
-13
lines changed

6 files changed

+86
-13
lines changed

comments_density.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
declare(strict_types=1);
44

55
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
6-
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\ConsoleOutputDTO;
7-
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\MissingDocblockConfigDTO;
86

97
return new Config(
10-
output: ConsoleOutputDTO::create(),
118
directories: [
129
'src',
1310
],

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"composer/composer": "^2.8",
2222
"composer/xdebug-handler": "^3.0.5",
2323
"nikic/php-parser": "^5.1",
24+
"php-defer/php-defer": "^5.0",
2425
"savinmikhail/primitive_wrappers": "^1.2",
2526
"symfony/cache": "^7.2",
2627
"symfony/cache-contracts": "^3.5",

composer.lock

Lines changed: 50 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AnalyzeComments/Analyzer/Analyzer.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
final readonly class Analyzer
2424
{
2525
public function __construct(
26-
private Config $configDTO,
26+
private Config $config,
2727
private CommentTypeFactory $commentFactory,
2828
private MetricsFacade $metrics,
2929
private BaselineStorageInterface $baselineStorage,
@@ -41,11 +41,11 @@ public function analyze(): Report
4141
$comments = [];
4242
$filesAnalyzed = 0;
4343
$totalLinesOfCode = 0;
44-
$fileFinder = new FileFinder($this->configDTO);
44+
$fileFinder = new FileFinder($this->config);
4545
foreach ($fileFinder() as $file) {
4646
$commentFinder = new CommentFinder(
4747
$this->commentFactory,
48-
$this->configDTO,
48+
$this->config,
4949
);
5050
$contentExtractor = new FileContentExtractor($file);
5151

@@ -61,13 +61,21 @@ public function analyze(): Report
6161
++$filesAnalyzed;
6262
}
6363

64-
if ($this->configDTO->useBaseline) {
64+
if ($this->config->useBaseline) {
6565
$comments = $this->baselineStorage->filterComments($comments);
6666
}
6767

6868
$commentStatistics = $this->statisticsAggregator->calculateCommentStatistics($comments);
69+
$report = $this->createReport($comments, $commentStatistics, $filesAnalyzed, $totalLinesOfCode);
6970

70-
return $this->createReport($comments, $commentStatistics, $filesAnalyzed, $totalLinesOfCode);
71+
$config = $this->config;
72+
defer($_, static function () use ($report, $config): void { // todo figure out how much sense it makes
73+
foreach ($config->plugins as $plugin) {
74+
$plugin->handle($report, $config);
75+
}
76+
});
77+
78+
return $report;
7179
}
7280

7381
private function getCacheKey(SplFileInfo $file): string

src/AnalyzeComments/Config/DTO/Config.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@
1010
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\MissingDocBlock;
1111
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\RegularComment;
1212
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\TodoComment;
13+
use SavinMikhail\CommentsDensity\Plugin\PluginInterface;
1314

1415
final readonly class Config
1516
{
17+
public OutputDTO $output;
18+
1619
/**
1720
* @param array<string, float> $thresholds
21+
* @param PluginInterface[] $plugins
1822
* @param string[] $exclude
1923
* @param string[] $directories
2024
* @param string[] $disable
2125
*/
2226
public function __construct(
23-
public OutputDTO $output,
2427
/** Directories to be scanned for comments */
2528
public array $directories,
29+
?OutputDTO $output = null,
2630
public MissingDocblockConfigDTO $docblockConfigDTO = new MissingDocblockConfigDTO(),
2731
/** Limit occurrences of each comment type */
2832
public array $thresholds = [],
@@ -33,7 +37,10 @@ public function __construct(
3337
public string $cacheDir = 'var/cache/comments-density',
3438
/** Disable certain types; set to empty array for full statistics */
3539
public array $disable = [],
36-
) {}
40+
public array $plugins = [],
41+
) {
42+
$this->output = $output ?? ConsoleOutputDTO::create();
43+
}
3744

3845
/**
3946
* @return non-empty-string[]

src/Plugin/PluginInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SavinMikhail\CommentsDensity\Plugin;
6+
7+
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\Report;
8+
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
9+
10+
interface PluginInterface
11+
{
12+
public function handle(Report $report, Config $config): void;
13+
}

0 commit comments

Comments
 (0)