|
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[] = [ |
|
|
|
|
|
|
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
|
|
|
|
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:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.