CommentsDensityPlugin   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 8
eloc 37
c 4
b 2
f 0
dl 0
loc 85
ccs 21
cts 21
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A deactivate() 0 2 1
A getSubscribedEvents() 0 5 1
A uninstall() 0 2 1
A activate() 0 2 1
A promptForConfigFile() 0 19 3
A promptForSetup() 0 5 1
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
        declare(strict_types=1);
22
        
23
        use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\FixMeComment;
24
        use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\RegularComment;
25
        use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\TodoComment;
26
        use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
27
        
28
        return new Config(
29
            directories: [
30
                'src',
31
            ],
32
            thresholds: [
33
                RegularComment::NAME => 0,
34
                TodoComment::NAME => 0,
35
                FixMeComment::NAME => 0,
36
            ],
37
        );
38
39
        PHP;
40
41
    public static function getSubscribedEvents(): array
42
    {
43
        return [
44
            ScriptEvents::POST_INSTALL_CMD => 'promptForSetup',
45
            ScriptEvents::POST_UPDATE_CMD => 'promptForSetup',
46
        ];
47
    }
48
49
    public static function promptForSetup(Event $event): void
50 1
    {
51
        $interface = $event->getIO();
52 1
53 1
        self::promptForConfigFile($interface);
54 1
    }
55 1
56
    private static function promptForConfigFile(IOInterface $interface): void
57
    {
58 1
        $interface->write('Run configuration file setup');
59
        $shouldCreateConfig = $interface
60 1
            ->askConfirmation('Do you want to create a default configuration file? [y/n]');
61
62 1
        if (!$shouldCreateConfig) {
63
            $interface->write('Configuration file setup skipped.');
64
65 2
            return;
66
        }
67 2
68 2
        $res = file_put_contents('../../comments_density.php', self::CONFIG);
69 2
70
        if ($res === false) {
71 2
            $interface->error('Configuration file setup failed.');
72 1
        }
73
74 1
        $interface->write('Default configuration file created.');
75
    }
76
77 1
    /**
78
     * @SuppressWarnings(PHPMD.ShortVariable)
79 1
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
80
     */
81
    public function activate(Composer $composer, IOInterface $io): void
82
    {
83
        // No activation logic needed
84
    }
85
86 1
    /**
87
     * @SuppressWarnings(PHPMD.ShortVariable)
88
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
89 1
     */
90
    public function deactivate(Composer $composer, IOInterface $io): void
91
    {
92
        // No deactivation logic needed
93
    }
94
95 1
    /**
96
     * @SuppressWarnings(PHPMD.ShortVariable)
97
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
98 1
     */
99
    public function uninstall(Composer $composer, IOInterface $io): void
100
    {
101
        // No uninstallation logic needed
102
    }
103
}
104