Passed
Push — master ( f30f7e...f7518f )
by Andrey
37:08 queued 22:06
created

Locale   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 58.14%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 18
eloc 30
c 6
b 0
f 0
dl 0
loc 147
ccs 25
cts 43
cp 0.5814
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A installed() 0 7 1
A addDefaultLocale() 0 3 1
A protects() 0 5 1
A filterLocales() 0 3 1
A normalizeNames() 0 5 1
A available() 0 3 1
A isInstalled() 0 3 1
A getSourceDirectories() 0 7 2
A getFallback() 0 3 1
A get() 0 7 1
A getResourcePath() 0 3 1
A getDefault() 0 3 1
A isProtected() 0 3 2
A getInstalledDirectories() 0 5 2
A isAvailable() 0 3 1
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use Helldar\LaravelLangPublisher\Facades\Arr as ArrFacade;
6
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...
7
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...
8
9
final class Locale
10
{
11
    /**
12
     * List of available locations.
13
     *
14
     * @param  bool  $is_json
15
     *
16 12
     * @return array
17
     */
18 12
    public function available(bool $is_json = false): array
19
    {
20
        return $this->get($this->getSourceDirectories($is_json));
21
    }
22
23
    /**
24
     * List of installed locations.
25
     *
26
     * @param  bool  $is_json
27
     *
28
     * @return array
29
     */
30
    public function installed(bool $is_json = false): array
31
    {
32
        $locales   = $this->get($this->getInstalledDirectories($is_json));
33
        $available = $this->available($is_json);
34
35
        return array_values(array_filter($locales, function ($locale) use ($available) {
36
            return in_array($locale, $available);
37
        }));
38
    }
39
40
    /**
41
     * Retrieving a list of protected locales.
42
     *
43
     * @return array
44
     */
45
    public function protects(): array
46
    {
47
        return ArrFacade::unique([
48
            $this->getDefault(),
49
            $this->getFallback(),
50
        ]);
51
    }
52
53
    /**
54
     * The checked locale protecting.
55
     *
56
     * @param  string  $locale
57
     *
58
     * @return bool
59
     */
60
    public function isProtected(string $locale): bool
61
    {
62
        return $locale === $this->getDefault() || $locale === $this->getFallback();
63
    }
64
65
    /**
66
     * Checks if a language pack is installed.
67
     *
68
     * @param  string  $locale
69
     * @param  bool  $is_json
70
     *
71
     * @return bool
72
     */
73
    public function isAvailable(string $locale, bool $is_json = false): bool
74
    {
75
        return in_array($locale, $this->available($is_json), true);
76
    }
77
78
    /**
79
     * Checks whether it is possible to install the language pack.
80 18
     *
81
     * @param  string  $locale
82 18
     * @param  bool  $is_json
83
     *
84
     * @return bool
85
     */
86
    public function isInstalled(string $locale, bool $is_json = false): bool
87
    {
88
        return in_array($locale, $this->installed($is_json), true);
89
    }
90 18
91
    /**
92 18
     * Getting the default localization name.
93
     *
94
     * @return string
95
     */
96
    public function getDefault(): string
97
    {
98
        return Config::getLocale();
99
    }
100 6
101
    /**
102 6
     * Getting the fallback localization name.
103
     *
104
     * @return string
105 12
     */
106
    public function getFallback(): string
107 12
    {
108
        return Config::getFallbackLocale();
109 12
    }
110
111 12
    protected function get(array $directories): array
112
    {
113
        $locales = $this->normalizeNames($directories);
114 12
115
        $this->addDefaultLocale($locales);
116 12
117
        return $this->filterLocales($locales);
118 12
    }
119 6
120 12
    protected function getSourceDirectories(bool $is_json = false): array
121
    {
122
        $vendor = Config::getVendorPath();
123
124
        return $is_json
125
            ? File::files($vendor . '/json')
126
            : File::directories($vendor . '/src');
127
    }
128
129
    protected function getInstalledDirectories(bool $is_json = false): array
130
    {
131
        return $is_json
132 12
            ? File::files($this->getResourcePath())
133
            : File::directories($this->getResourcePath());
134
    }
135 12
136 12
    protected function normalizeNames(array $directories): array
137
    {
138
        return array_map(function ($dir) {
139 12
            return File::name($dir);
140
        }, $directories);
141 12
    }
142 12
143
    protected function addDefaultLocale(array &$locales): void
144 12
    {
145
        array_push($locales, Config::getLocale());
146 12
    }
147
148
    protected function filterLocales(array $locales): array
149
    {
150
        return ArrFacade::unique($locales);
151
    }
152
153
    protected function getResourcePath(): string
154
    {
155
        return resource_path('lang');
156
    }
157
}
158