Passed
Push — master ( 27afec...e8b4e9 )
by Klaas
01:42 queued 14s
created

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

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