DirectoryResolver::getConfigurationDirectory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Paysera\PhpStormHelper\Service;
5
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\Finder\SplFileInfo;
8
use RuntimeException;
9
10
class DirectoryResolver
11
{
12 3
    public function getConfigurationDirectory(): string
13
    {
14 3
        return $this->getNeededDirectory('Preferences', '/config');
15
    }
16
17 3
    public function getPluginDirectory()
18
    {
19 3
        return $this->getNeededDirectory('Application Support', '/plugins');
20
    }
21
22 6
    private function getNeededDirectory(string $macOsFolderName, string $linuxPostfix): string
23
    {
24 6
        $homeDir = $_SERVER['HOME'] ?? $_SERVER['USERPROFILE'] ?? null;
25 6
        if ($homeDir === null) {
26
            throw new RuntimeException('Cannot resolve home folder to search for PhpStorm configuration');
27
        }
28
29 6
        $possibleDirectories[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$possibleDirectories was never initialized. Although not strictly required by PHP, it is generally a good practice to add $possibleDirectories = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
30 6
            'parent' => $homeDir,
31 6
            'pattern' => '#^\.PhpStorm20[0-9.]+$#',
32 6
            'postfix' => $linuxPostfix,
33
        ];
34
35 6
        if (file_exists($homeDir . '/Library/' . $macOsFolderName)) {
36 2
            $possibleDirectories[] = [
37 2
                'parent' => $homeDir . '/Library/' . $macOsFolderName,
38 2
                'pattern' => '#^PhpStorm20[0-9.]+$#',
39 2
                'postfix' => '',
40
            ];
41
        }
42
43 6
        foreach ($possibleDirectories as $directoryInfo) {
44 6
            $directory = $this->tryFindPhpStormDirectory($directoryInfo['parent'], $directoryInfo['pattern']);
45 6
            if ($directory !== null) {
46 6
                return $directory . $directoryInfo['postfix'];
47
            }
48
        }
49
50
        throw new RuntimeException(sprintf(
51
            '%s. %s',
52
            'PhpStorm configuration was not found',
53
            'You need to run PhpStorm at least once before running this script'
54
        ));
55
    }
56
57 6
    private function tryFindPhpStormDirectory(string $parentDirectory, string $pattern)
58
    {
59 6
        $directoryIterator = (new Finder())
60 6
            ->in($parentDirectory)
61 6
            ->directories()
62 6
            ->depth(0)
63 6
            ->path($pattern)
64 6
            ->sortByName()
65 6
            ->ignoreDotFiles(false)
66 6
            ->getIterator()
67
        ;
68 6
        $directories = array_reverse(iterator_to_array($directoryIterator));
69 6
        if (count($directories) > 0) {
70
            /** @var SplFileInfo $fileInfo */
71 6
            $fileInfo = reset($directories);
72 6
            return $fileInfo->getPathname();
73
        }
74
75 2
        return null;
76
    }
77
}
78