Passed
Push — main ( fcc13f...628042 )
by mikhail
02:55
created

CommentsDensityPlugin   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 95.12%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 10
eloc 48
c 5
b 1
f 0
dl 0
loc 106
ccs 39
cts 41
cp 0.9512
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A uninstall() 0 2 1
A getSubscribedEvents() 0 5 1
A activate() 0 2 1
A promptForPreCommitHook() 0 24 3
A promptForSetup() 0 6 1
A promptForConfigFile() 0 35 2
A deactivate() 0 2 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
final class CommentsDensityPlugin implements PluginInterface, EventSubscriberInterface
15
{
16
    /**
17
     * @SuppressWarnings(PHPMD.ShortVariable)
18
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
19
     */
20 1
    public function activate(Composer $composer, IOInterface $io): void
21
    {
22
        // No activation logic needed
23 1
    }
24
25
    /**
26
     * @SuppressWarnings(PHPMD.ShortVariable)
27
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
28
     */
29 1
    public function deactivate(Composer $composer, IOInterface $io): void
30
    {
31
        // No deactivation logic needed
32 1
    }
33
34
    /**
35
     * @SuppressWarnings(PHPMD.ShortVariable)
36
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
37
     */
38 1
    public function uninstall(Composer $composer, IOInterface $io): void
39
    {
40
        // No uninstallation logic needed
41 1
    }
42
43 1
    public static function getSubscribedEvents(): array
44
    {
45 1
        return [
46 1
            ScriptEvents::POST_INSTALL_CMD => 'promptForSetup',
47 1
            ScriptEvents::POST_UPDATE_CMD => 'promptForSetup',
48 1
        ];
49
    }
50
51 1
    public static function promptForSetup(Event $event): void
52
    {
53 1
        $interface = $event->getIO();
54
55 1
        self::promptForPreCommitHook($interface);
56 1
        self::promptForConfigFile($interface);
57
    }
58
59 2
    private static function promptForConfigFile(IOInterface $interface): void
60
    {
61 2
        $interface->write('Run configuration file setup');
62 2
        $shouldCreateConfig = $interface->askConfirmation('Do you want to create a default configuration file? [y/N] ');
63
64 2
        if ($shouldCreateConfig) {
65 1
            $interface->write('Creating default configuration file...');
66
67 1
            $defaultConfig = <<<YAML
68
directories:
69
  - "src"
70
  - "tests"
71
exclude:
72
  - "src/bin"
73
thresholds:
74
  docBlock: 0
75
  regular: 0
76
  todo: 0
77
  fixme: 0
78
  missingDocBlock: 0
79
  Com/LoC: 0.1
80
  CDS: 0.5
81
output:
82
  type: "html" #  "console" or 'html'
83
  file: "output.html" # file path for HTML output
84
85 1
YAML;
86
87 1
            file_put_contents('comments_density.yaml', $defaultConfig);
88
89 1
            $interface->write('Default configuration file created.');
90 1
            return;
91
        }
92
93 1
        $interface->write('Configuration file setup skipped.');
94
    }
95
96 2
    public static function promptForPreCommitHook(IOInterface $ioHelper): void
97
    {
98 2
        $ioHelper->write('Run pre-commit installation');
99 2
        $shouldInstallHook = $ioHelper->askConfirmation('Do you want to install the pre-commit hook? [y/N] ');
100
101 2
        if ($shouldInstallHook) {
102 1
            $ioHelper->write('Installing pre-commit hook...');
103
104 1
            $source = __DIR__ . '/../../pre-commit.sh';
105 1
            $destination = '.git/hooks/pre-commit';
106
107 1
            if (!file_exists($source)) {
108
                $ioHelper->writeError("Error: Source file $source does not exist.");
109
                return;
110
            }
111
112 1
            copy($source, $destination);
113 1
            chmod($destination, 0755);
114
115 1
            $ioHelper->write('Pre-commit hook installed.');
116 1
            return;
117
        }
118
119 1
        $ioHelper->write('Pre-commit hook installation skipped.');
120
    }
121
}
122