Passed
Push — master ( dc765b...d220c6 )
by Andrey
130:03 queued 111:34
created

Locale::isProtected()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use Helldar\LaravelLangPublisher\Contracts\Locale as LocaleContract;
6
use Helldar\LaravelLangPublisher\Facades\Arr as ArrFacade;
7
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...
8
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...
9
10
use function array_map;
11
use function array_push;
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 secure 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
     * The checked locale protecting.
51
     *
52
     * @param string $locale
53
     *
54
     * @return bool
55
     */
56 12
    public function isProtected(string $locale): bool
57
    {
58 12
        return $locale === $this->getDefault() || $locale === $this->getFallback();
59
    }
60
61
    /**
62
     * Getting the default localization name.
63
     *
64
     * @return string
65
     */
66 12
    public function getDefault(): string
67
    {
68 12
        return Config::getLocale();
69
    }
70
71
    /**
72
     * Getting the fallback localization name.
73
     *
74
     * @return string
75
     */
76 6
    public function getFallback(): string
77
    {
78 6
        return Config::getFallbackLocale();
79
    }
80
81
    protected function get(array $directories): array
82
    {
83
        $locales = $this->normalizeNames($directories);
84
85
        $this->addDefaultLocale($locales);
86
87
        return $this->filterLocales($locales);
88
    }
89
90
    protected function getSourceDirectories(): array
91
    {
92
        return File::directories(
93
            Config::getVendorPath()
94
        );
95
    }
96
97
    protected function getInstalledDirectories(): array
98
    {
99
        return File::directories(
100
            resource_path('lang')
101
        );
102
    }
103
104
    protected function normalizeNames(array $directories): array
105
    {
106
        return array_map(function ($dir) {
107
            return File::name($dir);
108
        }, $directories);
109
    }
110
111
    protected function addDefaultLocale(array &$locales): void
112
    {
113
        array_push($locales, Config::getLocale());
114
    }
115
116
    protected function filterLocales(array $locales): array
117
    {
118
        return ArrFacade::unique($locales);
119
    }
120
}
121