Test Failed
Push — main ( 27d416...4ff5eb )
by mikhail
03:21
created

CommentsDensityPlugin::promptForSetup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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