Passed
Push — master ( 1426d4...f170c2 )
by Ashoka
03:32 queued 10s
created

InstallerPlugin::getPhpStormDefaultPath()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 16
rs 9.9666
ccs 0
cts 9
cp 0
crap 12
1
<?php declare(strict_types=1);
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 * @deprecated
6
 */
7
8
namespace Mediact\CodingStandard\PhpStorm;
9
10
use Composer\Composer;
11
use Composer\EventDispatcher\EventSubscriberInterface;
12
use Composer\IO\IOInterface;
13
use Composer\Plugin\PluginInterface;
14
use Composer\Script\Event;
15
use Composer\Script\ScriptEvents;
16
use Mediact\CodingStandard\PhpStorm\Patcher\ConfigPatcher;
17
use Mediact\CodingStandard\PhpStorm\Patcher\ConfigPatcherInterface;
18
19
class InstallerPlugin implements PluginInterface, EventSubscriberInterface
20
{
21
    /**
22
     * @var ConfigPatcherInterface
23
     */
24
    private $patcher;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param ConfigPatcherInterface $patcher
30
     */
31 1
    public function __construct(ConfigPatcherInterface $patcher = null)
32
    {
33 1
        $this->patcher = $patcher !== null
34 1
            ? $patcher
35
            : new ConfigPatcher();
36 1
    }
37
38
    /**
39
     * Apply plugin modifications to Composer
40
     *
41
     * @param Composer    $composer
42
     * @param IOInterface $inputOutput
43
     *
44
     * @return void
45
     */
46
    public function activate(Composer $composer, IOInterface $inputOutput): void
47
    {
48
    }
49
50
    /**
51
     * Get the subscribed events.
52
     *
53
     * @return array
54
     */
55 1
    public static function getSubscribedEvents(): array
56
    {
57
        return [
58 1
            ScriptEvents::POST_INSTALL_CMD => 'onNewCodeEvent',
59 1
            ScriptEvents::POST_UPDATE_CMD  => 'onNewCodeEvent'
60
        ];
61
    }
62
63
    /**
64
     * On new code.
65
     *
66
     * @param Event $event
67
     *
68
     * @return void
69
     */
70 1
    public function onNewCodeEvent(Event $event): void
71
    {
72 1
        $vendorDir   = $event->getComposer()->getConfig()->get('vendor-dir');
73 1
        $projectDir  = dirname($vendorDir);
74 1
        $phpStormDir = $projectDir . DIRECTORY_SEPARATOR . '.idea';
75 1
        $filesDir    = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'files';
76
77 1
        $phpStormDefaultPath = $this->getPhpStormDefaultPath();
78
79 1
        if (is_dir($phpStormDir) && is_dir($filesDir)) {
80 1
            $this->patcher->patch(
81 1
                new Environment(
82 1
                    new Filesystem($phpStormDir),
83 1
                    new Filesystem($phpStormDefaultPath),
84 1
                    new Filesystem($filesDir),
85 1
                    new Filesystem($projectDir),
86 1
                    $event->getIO(),
87 1
                    $event->getComposer()
88
                )
89
            );
90
91 1
            $output = $event->getIO();
92 1
            $output->write('Patched the PhpStorm config');
93
        }
94 1
    }
95
96
    /**
97
     * Get the latest version of clients phpstorm directory
98
     *
99
     * @return string
100
     */
101
    public function getPhpStormDefaultPath(): string
102
    {
103
        $phpStormDefaultPath = '';
104
105
        if (isset($_SERVER['HOME'])) {
106
            $home = $_SERVER['HOME'];
107
        } else {
108
            $home = getenv("HOME");
109
        }
110
111
        $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

111
        $phpStormDefaultPaths = array_reverse(/** @scrutinizer ignore-type */ glob("$home/.[pP]hp[sS]torm201*/config/"));
Loading history...
112
        if (! empty($phpStormDefaultPaths)) {
113
            $phpStormDefaultPath = reset($phpStormDefaultPaths);
114
        }
115
116
        return $phpStormDefaultPath;
117
    }
118
}
119