Passed
Push — main ( 311549...7ac0b4 )
by mikhail
03:56
created

CommentsDensityPlugin::uninstall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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::promptForPreCommitHook($interface);
78 1
        self::promptForConfigFile($interface);
79
    }
80
81 2
    public static function promptForPreCommitHook(IOInterface $ioHelper): void
82
    {
83 2
        $ioHelper->write('Run pre-commit installation');
84 2
        $shouldInstallHook = $ioHelper->askConfirmation('Do you want to install the pre-commit hook? [y/N] ');
85
86 2
        if ($shouldInstallHook) {
87 1
            $ioHelper->write('Installing pre-commit hook...');
88
89 1
            $source = __DIR__ . '/../../pre-commit.sh';
90 1
            $destination = '.git/hooks/pre-commit';
91
92 1
            if (!file_exists($source)) {
93
                $ioHelper->writeError("Error: Source file {$source} does not exist.");
94
95
                return;
96
            }
97
98 1
            copy($source, $destination);
99 1
            chmod($destination, 0o755);
100
101 1
            $ioHelper->write('Pre-commit hook installed.');
102
103 1
            return;
104
        }
105
106 1
        $ioHelper->write('Pre-commit hook installation skipped.');
107
    }
108
109 2
    private static function promptForConfigFile(IOInterface $interface): void
110
    {
111 2
        $interface->write('Run configuration file setup');
112 2
        $shouldCreateConfig = $interface->askConfirmation('Do you want to create a default configuration file? [y/N] ');
113
114 2
        if ($shouldCreateConfig) {
115 1
            $interface->write('Creating default configuration file...');
116
117 1
            file_put_contents('comments_density.php', self::CONFIG);
118
119 1
            $interface->write('Default configuration file created.');
120
121 1
            return;
122
        }
123
124 1
        $interface->write('Configuration file setup skipped.');
125
    }
126
127
    /**
128
     * @SuppressWarnings(PHPMD.ShortVariable)
129
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
130
     */
131 1
    public function activate(Composer $composer, IOInterface $io): void
132
    {
133
        // No activation logic needed
134 1
    }
135
136
    /**
137
     * @SuppressWarnings(PHPMD.ShortVariable)
138
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
139
     */
140 1
    public function deactivate(Composer $composer, IOInterface $io): void
141
    {
142
        // No deactivation logic needed
143 1
    }
144
145
    /**
146
     * @SuppressWarnings(PHPMD.ShortVariable)
147
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
148
     */
149 1
    public function uninstall(Composer $composer, IOInterface $io): void
150
    {
151
        // No uninstallation logic needed
152 1
    }
153
}
154