Passed
Push — main ( 41d632...dfc5c9 )
by mikhail
03:00
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',
27
    ],
28
    'exclude' => [
29
        'src/DTO',
30
    ],
31
    'thresholds' => [
32
        'docBlock' => 90,
33
        'regular' => 5,
34
        'todo' => 5,
35
        'fixme' => 5,
36
        'missingDocBlock' => 10,
37
        'Com/LoC' => 0.1,
38
        'CDS' => 0.1,
39
    ],
40
    'only' => [
41
        'missingDocblock'
42
    ],
43
    'output' => [
44
        'type' => 'console', // "console" or 'html'
45
        'file' => 'output.html', // file path for HTML output
46
    ],
47
];
48
49
PHP;
50
    /**
51
     * @SuppressWarnings(PHPMD.ShortVariable)
52
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
53
     */
54 1
    public function activate(Composer $composer, IOInterface $io): void
55
    {
56
        // No activation logic needed
57 1
    }
58
59
    /**
60
     * @SuppressWarnings(PHPMD.ShortVariable)
61
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
62
     */
63 1
    public function deactivate(Composer $composer, IOInterface $io): void
64
    {
65
        // No deactivation logic needed
66 1
    }
67
68
    /**
69
     * @SuppressWarnings(PHPMD.ShortVariable)
70
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
71
     */
72 1
    public function uninstall(Composer $composer, IOInterface $io): void
73
    {
74
        // No uninstallation logic needed
75 1
    }
76
77 1
    public static function getSubscribedEvents(): array
78
    {
79 1
        return [
80 1
            ScriptEvents::POST_INSTALL_CMD => 'promptForSetup',
81 1
            ScriptEvents::POST_UPDATE_CMD => 'promptForSetup',
82 1
        ];
83
    }
84
85 1
    public static function promptForSetup(Event $event): void
86
    {
87 1
        $interface = $event->getIO();
88
89 1
        self::promptForPreCommitHook($interface);
90 1
        self::promptForConfigFile($interface);
91
    }
92
93 2
    private static function promptForConfigFile(IOInterface $interface): void
94
    {
95 2
        $interface->write('Run configuration file setup');
96 2
        $shouldCreateConfig = $interface->askConfirmation('Do you want to create a default configuration file? [y/N] ');
97
98 2
        if ($shouldCreateConfig) {
99 1
            $interface->write('Creating default configuration file...');
100
101 1
            file_put_contents('comments_density.php', self::CONFIG);
102
103 1
            $interface->write('Default configuration file created.');
104 1
            return;
105
        }
106
107 1
        $interface->write('Configuration file setup skipped.');
108
    }
109
110 2
    public static function promptForPreCommitHook(IOInterface $ioHelper): void
111
    {
112 2
        $ioHelper->write('Run pre-commit installation');
113 2
        $shouldInstallHook = $ioHelper->askConfirmation('Do you want to install the pre-commit hook? [y/N] ');
114
115 2
        if ($shouldInstallHook) {
116 1
            $ioHelper->write('Installing pre-commit hook...');
117
118 1
            $source = __DIR__ . '/../../pre-commit.sh';
119 1
            $destination = '.git/hooks/pre-commit';
120
121 1
            if (!file_exists($source)) {
122
                $ioHelper->writeError("Error: Source file $source does not exist.");
123
                return;
124
            }
125
126 1
            copy($source, $destination);
127 1
            chmod($destination, 0755);
128
129 1
            $ioHelper->write('Pre-commit hook installed.');
130 1
            return;
131
        }
132
133 1
        $ioHelper->write('Pre-commit hook installation skipped.');
134
    }
135
}
136