Passed
Push — main ( 30dfbd...b8dd52 )
by mikhail
03:24
created

CommentsDensityPlugin::promptForPreCommitHook()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0213

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 14
c 4
b 1
f 0
dl 0
loc 24
ccs 13
cts 15
cp 0.8667
rs 9.7998
cc 3
nc 3
nop 1
crap 3.0213
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
    'missingDocblock' => [
48
        'class' => true,
49
        'interface' => true,
50
        'trait' => true,
51
        'enum' => true,
52
        'property' => true,
53
        'constant' => true,
54
        'function' => true,
55
        'requireForAllMethods' => true
56
    ]
57
];
58
59
PHP;
60
    /**
61
     * @SuppressWarnings(PHPMD.ShortVariable)
62
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
63
     */
64 1
    public function activate(Composer $composer, IOInterface $io): void
65
    {
66
        // No activation logic needed
67 1
    }
68
69
    /**
70
     * @SuppressWarnings(PHPMD.ShortVariable)
71
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
72
     */
73 1
    public function deactivate(Composer $composer, IOInterface $io): void
74
    {
75
        // No deactivation logic needed
76 1
    }
77
78
    /**
79
     * @SuppressWarnings(PHPMD.ShortVariable)
80
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
81
     */
82 1
    public function uninstall(Composer $composer, IOInterface $io): void
83
    {
84
        // No uninstallation logic needed
85 1
    }
86
87 1
    public static function getSubscribedEvents(): array
88
    {
89 1
        return [
90 1
            ScriptEvents::POST_INSTALL_CMD => 'promptForSetup',
91 1
            ScriptEvents::POST_UPDATE_CMD => 'promptForSetup',
92 1
        ];
93
    }
94
95 1
    public static function promptForSetup(Event $event): void
96
    {
97 1
        $interface = $event->getIO();
98
99 1
        self::promptForPreCommitHook($interface);
100 1
        self::promptForConfigFile($interface);
101
    }
102
103 2
    private static function promptForConfigFile(IOInterface $interface): void
104
    {
105 2
        $interface->write('Run configuration file setup');
106 2
        $shouldCreateConfig = $interface->askConfirmation('Do you want to create a default configuration file? [y/N] ');
107
108 2
        if ($shouldCreateConfig) {
109 1
            $interface->write('Creating default configuration file...');
110
111 1
            file_put_contents('comments_density.php', self::CONFIG);
112
113 1
            $interface->write('Default configuration file created.');
114 1
            return;
115
        }
116
117 1
        $interface->write('Configuration file setup skipped.');
118
    }
119
120 2
    public static function promptForPreCommitHook(IOInterface $ioHelper): void
121
    {
122 2
        $ioHelper->write('Run pre-commit installation');
123 2
        $shouldInstallHook = $ioHelper->askConfirmation('Do you want to install the pre-commit hook? [y/N] ');
124
125 2
        if ($shouldInstallHook) {
126 1
            $ioHelper->write('Installing pre-commit hook...');
127
128 1
            $source = __DIR__ . '/../../pre-commit.sh';
129 1
            $destination = '.git/hooks/pre-commit';
130
131 1
            if (!file_exists($source)) {
132
                $ioHelper->writeError("Error: Source file $source does not exist.");
133
                return;
134
            }
135
136 1
            copy($source, $destination);
137 1
            chmod($destination, 0755);
138
139 1
            $ioHelper->write('Pre-commit hook installed.');
140 1
            return;
141
        }
142
143 1
        $ioHelper->write('Pre-commit hook installation skipped.');
144
    }
145
}
146