Passed
Push — master ( eee2b6...67ddf0 )
by Andrey
31:41 queued 29:01
created

Locale::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use function array_map;
6
use function array_push;
7
use Helldar\LaravelLangPublisher\Contracts\Locale as LocaleContract;
8
use Helldar\LaravelLangPublisher\Facades\Arr as ArrFacade;
9
10
use Helldar\LaravelLangPublisher\Facades\Config;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\LaravelLangPublisher\Support\Config. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use Illuminate\Support\Facades\File;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\LaravelLangPublisher\Support\File. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use function resource_path;
13
14
final class Locale implements LocaleContract
15
{
16
    /**
17
     * List of available locations.
18
     *
19
     * @return array
20
     */
21
    public function available(): array
22
    {
23
        return $this->get($this->getSourceDirectories());
24
    }
25
26
    /**
27
     * List of installed locations.
28
     *
29
     * @return array
30
     */
31
    public function installed(): array
32
    {
33
        return $this->get($this->getInstalledDirectories());
34
    }
35
36
    /**
37
     * Retrieving a list of protected locales.
38
     *
39
     * @return array
40
     */
41
    public function protects(): array
42
    {
43
        return ArrFacade::unique([
44
            $this->getDefault(),
45
            $this->getFallback(),
46
        ]);
47
    }
48
49
    /**
50
     * Checks if a language pack is installed.
51
     *
52
     * @param  string  $locale
53
     *
54
     * @return bool
55
     */
56
    public function isAvailable(string $locale): bool
57
    {
58
        return in_array($locale, $this->available(), true);
59
    }
60
61
    /**
62
     * Checks whether it is possible to install the language pack.
63
     *
64
     * @param  string  $locale
65
     *
66
     * @return bool
67
     */
68
    public function isInstalled(string $locale): bool
69
    {
70
        return in_array($locale, $this->installed(), true);
71
    }
72
73
    /**
74
     * The checked locale protecting.
75
     *
76
     * @param  string  $locale
77
     *
78
     * @return bool
79
     */
80 12
    public function isProtected(string $locale): bool
81
    {
82 12
        return $locale === $this->getDefault() || $locale === $this->getFallback();
83
    }
84
85
    /**
86
     * Getting the default localization name.
87
     *
88
     * @return string
89
     */
90 12
    public function getDefault(): string
91
    {
92 12
        return Config::getLocale();
93
    }
94
95
    /**
96
     * Getting the fallback localization name.
97
     *
98
     * @return string
99
     */
100 6
    public function getFallback(): string
101
    {
102 6
        return Config::getFallbackLocale();
103
    }
104
105
    protected function get(array $directories): array
106
    {
107
        $locales = $this->normalizeNames($directories);
108
109
        $this->addDefaultLocale($locales);
110
111
        return $this->filterLocales($locales);
112
    }
113
114
    protected function getSourceDirectories(): array
115
    {
116
        return File::directories(
117
            Config::getVendorPath()
118
        );
119
    }
120
121
    protected function getInstalledDirectories(): array
122
    {
123
        return File::directories(
124
            resource_path('lang')
125
        );
126
    }
127
128
    protected function normalizeNames(array $directories): array
129
    {
130
        return array_map(function ($dir) {
131
            return File::name($dir);
132
        }, $directories);
133
    }
134
135
    protected function addDefaultLocale(array &$locales): void
136
    {
137
        array_push($locales, Config::getLocale());
138
    }
139
140
    protected function filterLocales(array $locales): array
141
    {
142
        return ArrFacade::unique($locales);
143
    }
144
}
145