Passed
Pull Request — master (#18)
by Ashoka
09:26
created

Plugin::deactivate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
3
/**
4
 * Copyright MediaCT. All rights reserved.
5
 * https://www.mediact.nl
6
 */
7
8
declare(strict_types=1);
9
10
namespace Mediact\CodingStandard\PhpStorm;
11
12
use Composer\Composer;
13
use Composer\EventDispatcher\EventSubscriberInterface;
14
use Composer\IO\IOInterface;
15
use Composer\Plugin\PluginInterface;
16
use Composer\Script\Event;
17
use Composer\Script\ScriptEvents;
18
use Mediact\CodingStandard\PhpStorm\Patcher\ConfigPatcher;
19
use Mediact\CodingStandard\PhpStorm\Patcher\ConfigPatcherInterface;
20
21
/**
22
 * This class is deprecated because this class has an extra argument.
23
 *
24
 * @deprecated
25
 * Class Plugin
26
 * @package Mediact\CodingStandard\PhpStorm
27
 */
28
class Plugin implements PluginInterface, EventSubscriberInterface
29
{
30
    /**
31
     * @var ConfigPatcherInterface
32
     */
33
    private $patcher;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param ConfigPatcherInterface $patcher
39
     */
40
    public function __construct(ConfigPatcherInterface $patcher = null)
41
    {
42
        $this->patcher = $patcher !== null
43
            ? $patcher
44
            : new ConfigPatcher();
45
    }
46
47
    /**
48
     * Apply plugin modifications to Composer
49
     *
50
     * @param Composer    $composer
51
     * @param IOInterface $inputOutput
52
     *
53
     * @return void
54
     */
55
    public function activate(Composer $composer, IOInterface $inputOutput): void
56
    {
57
    }
58
59
    /**
60
     * Remove any hooks from Composer.
61
     *
62
     * @param Composer    $composer
63
     * @param IOInterface $io
64
     *
65
     * @return void
66
     */
67
    public function deactivate(Composer $composer, IOInterface $io): void
68
    {
69
    }
70
71
    /**
72
     * Prepare the plugin to be uninstalled
73
     *
74
     * @param Composer    $composer
75
     * @param IOInterface $io
76
     *
77
     * @return void
78
     */
79
    public function uninstall(Composer $composer, IOInterface $io): void
80
    {
81
    }
82
83
    /**
84
     * Get the subscribed events.
85
     *
86
     * @return array
87
     */
88
    public static function getSubscribedEvents(): array
89
    {
90
        return [
91
            ScriptEvents::POST_INSTALL_CMD => 'onNewCodeEvent',
92
            ScriptEvents::POST_UPDATE_CMD  => 'onNewCodeEvent'
93
        ];
94
    }
95
96
    /**
97
     * On new code.
98
     *
99
     * @param Event $event
100
     *
101
     * @return void
102
     */
103
    public function onNewCodeEvent(Event $event): void
104
    {
105
        $vendorDir   = $event->getComposer()->getConfig()->get('vendor-dir');
106
        $projectDir  = dirname($vendorDir);
107
        $phpStormDir = $projectDir . DIRECTORY_SEPARATOR . '.idea';
108
        $filesDir    = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'files';
109
110
        $phpStormDefaultPath = $this->getPhpStormDefaultPath();
111
112
        if (is_dir($phpStormDir) && is_dir($filesDir)) {
113
            $this->patcher->patch(
114
                new Environment(
115
                    new Filesystem($phpStormDir),
116
                    new Filesystem($phpStormDefaultPath),
117
                    new Filesystem($filesDir),
118
                    new Filesystem($projectDir),
119
                    $event->getIO(),
120
                    $event->getComposer()
121
                )
122
            );
123
124
            $output = $event->getIO();
125
            $output->write('Patched the PhpStorm config');
126
        }
127
    }
128
129
    /**
130
     * Get the latest version of clients phpstorm directory
131
     *
132
     * @return string
133
     */
134
    public function getPhpStormDefaultPath(): string
135
    {
136
        $phpStormDefaultPath = '';
137
138
        if (isset($_SERVER['HOME'])) {
139
            $home = $_SERVER['HOME'];
140
        } else {
141
            $home = getenv("HOME");
142
        }
143
144
        $phpStormDefaultPaths = array_reverse(glob("$home/.[pP]hp[sS]torm201*/config/"));
0 ignored issues
show
Bug introduced by
It seems like glob($home.'/.[pP]hp[sS]torm201*/config/') can also be of type false; however, parameter $array of array_reverse() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
        $phpStormDefaultPaths = array_reverse(/** @scrutinizer ignore-type */ glob("$home/.[pP]hp[sS]torm201*/config/"));
Loading history...
145
        if (! empty($phpStormDefaultPaths)) {
146
            $phpStormDefaultPath = reset($phpStormDefaultPaths);
147
        }
148
149
        return $phpStormDefaultPath;
150
    }
151
}
152