1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SavinMikhail\CommentsDensity\Composer; |
6
|
|
|
|
7
|
|
|
use Composer\Composer; |
8
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
9
|
|
|
use Composer\IO\IOInterface; |
10
|
|
|
use Composer\Plugin\PluginInterface; |
11
|
|
|
use Composer\Script\Event; |
12
|
|
|
use Composer\Script\ScriptEvents; |
13
|
|
|
|
14
|
|
|
use function chmod; |
15
|
|
|
use function copy; |
16
|
|
|
use function file_exists; |
17
|
|
|
use function file_put_contents; |
18
|
|
|
|
19
|
|
|
final class CommentsDensityPlugin implements PluginInterface, EventSubscriberInterface |
20
|
|
|
{ |
21
|
|
|
private const CONFIG = <<<'PHP' |
22
|
|
|
<?php |
23
|
|
|
|
24
|
|
|
return [ |
25
|
|
|
'directories' => [ |
26
|
|
|
'src', // Directories to be scanned for comments |
27
|
|
|
], |
28
|
|
|
'exclude' => [ |
29
|
|
|
'src/DTO', // Directories to be ignored during scanning |
30
|
|
|
], |
31
|
|
|
'thresholds' => [ |
32
|
|
|
// Limit occurrences of each comment type |
33
|
|
|
'docBlock' => 90, |
34
|
|
|
'regular' => 5, |
35
|
|
|
'todo' => 5, |
36
|
|
|
'fixme' => 5, |
37
|
|
|
'missingDocBlock' => 10, |
38
|
|
|
// Additional metrics thresholds |
39
|
|
|
'Com/LoC' => 0.1, // Comments per Lines of Code |
40
|
|
|
'CDS' => 0.1, // Comment Density Score |
41
|
|
|
], |
42
|
|
|
'only' => [ |
43
|
|
|
'missingDocblock', // Only this type will be analyzed; set to empty array for full statistics |
44
|
|
|
], |
45
|
|
|
'output' => [ |
46
|
|
|
'type' => 'console', // Supported values: 'console', 'html' |
47
|
|
|
'file' => 'output.html', // File path for HTML output (only used if type is 'html') |
48
|
|
|
], |
49
|
|
|
'missingDocblock' => [ |
50
|
|
|
'class' => true, // Check for missing docblocks in classes |
51
|
|
|
'interface' => true, // Check for missing docblocks in interfaces |
52
|
|
|
'trait' => true, // Check for missing docblocks in traits |
53
|
|
|
'enum' => true, // Check for missing docblocks in enums |
54
|
|
|
'property' => true, // Check for missing docblocks in properties |
55
|
|
|
'constant' => true, // Check for missing docblocks in constants |
56
|
|
|
'function' => true, // Check for missing docblocks in functions |
57
|
|
|
// If false, only methods where @throws tag or generic can be applied will be checked |
58
|
|
|
'requireForAllMethods' => true, |
59
|
|
|
], |
60
|
|
|
'use_baseline' => true, // Filter collected comments against the baseline stored in baseline.php |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
PHP; |
64
|
|
|
|
65
|
1 |
|
public static function getSubscribedEvents(): array |
66
|
|
|
{ |
67
|
1 |
|
return [ |
68
|
1 |
|
ScriptEvents::POST_INSTALL_CMD => 'promptForSetup', |
69
|
1 |
|
ScriptEvents::POST_UPDATE_CMD => 'promptForSetup', |
70
|
1 |
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public static function promptForSetup(Event $event): void |
74
|
|
|
{ |
75
|
1 |
|
$interface = $event->getIO(); |
76
|
|
|
|
77
|
1 |
|
self::promptForConfigFile($interface); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
private static function promptForConfigFile(IOInterface $interface): void |
81
|
2 |
|
{ |
82
|
|
|
$interface->write('Run configuration file setup'); |
83
|
2 |
|
$shouldCreateConfig = $interface |
84
|
2 |
|
->askConfirmation('Do you want to create a default configuration file? [y/n]'); |
85
|
|
|
|
86
|
2 |
|
if (!$shouldCreateConfig) { |
87
|
1 |
|
$interface->write('Configuration file setup skipped.'); |
88
|
|
|
return; |
89
|
1 |
|
} |
90
|
1 |
|
|
91
|
|
|
file_put_contents('comments_density.php', self::CONFIG); |
92
|
1 |
|
|
93
|
|
|
$interface->write('Default configuration file created.'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
98
|
1 |
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
99
|
1 |
|
*/ |
100
|
|
|
public function activate(Composer $composer, IOInterface $io): void |
101
|
1 |
|
{ |
102
|
|
|
// No activation logic needed |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
1 |
|
* @SuppressWarnings(PHPMD.ShortVariable) |
107
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
108
|
|
|
*/ |
109
|
2 |
|
public function deactivate(Composer $composer, IOInterface $io): void |
110
|
|
|
{ |
111
|
2 |
|
// No deactivation logic needed |
112
|
2 |
|
} |
113
|
|
|
|
114
|
2 |
|
/** |
115
|
1 |
|
* @SuppressWarnings(PHPMD.ShortVariable) |
116
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
117
|
1 |
|
*/ |
118
|
|
|
public function uninstall(Composer $composer, IOInterface $io): void |
119
|
1 |
|
{ |
120
|
|
|
// No uninstallation logic needed |
121
|
1 |
|
} |
122
|
|
|
} |
123
|
|
|
|