Passed
Push — master ( 0c3b18...0300eb )
by Andrey
02:51 queued 11s
created

Locale   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 58.14%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
eloc 30
c 3
b 0
f 0
dl 0
loc 138
ccs 25
cts 43
cp 0.5814
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A protects() 0 5 1
A isInstalled() 0 3 1
A getFallback() 0 3 1
A getDefault() 0 3 1
A isProtected() 0 3 2
A isAvailable() 0 3 1
A get() 0 7 1
A getSourceDirectories() 0 7 2
A installed() 0 7 1
A getInstalledDirectories() 0 7 2
A addDefaultLocale() 0 3 1
A filterLocales() 0 3 1
A normalizeNames() 0 5 1
A available() 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
     * @return array
15
     */
16 12
    public function available(bool $is_json = false): array
17
    {
18 12
        return $this->get($this->getSourceDirectories($is_json));
19
    }
20
21
    /**
22
     * List of installed locations.
23
     *
24
     * @return array
25
     */
26
    public function installed(bool $is_json = false): array
27
    {
28
        $locales   = $this->get($this->getInstalledDirectories($is_json));
29
        $available = $this->available();
30
31
        return array_filter($locales, function ($locale) use ($available) {
32
            return in_array($locale, $available);
33
        });
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 18
    public function isProtected(string $locale): bool
81
    {
82 18
        return $locale === $this->getDefault() || $locale === $this->getFallback();
83
    }
84
85
    /**
86
     * Getting the default localization name.
87
     *
88
     * @return string
89
     */
90 18
    public function getDefault(): string
91
    {
92 18
        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 12
    protected function get(array $directories): array
106
    {
107 12
        $locales = $this->normalizeNames($directories);
108
109 12
        $this->addDefaultLocale($locales);
110
111 12
        return $this->filterLocales($locales);
112
    }
113
114 12
    protected function getSourceDirectories(bool $is_json = false): array
115
    {
116 12
        $vendor = Config::getVendorPath();
117
118 12
        return $is_json
119 6
            ? File::files($vendor . '/json')
120 12
            : File::directories($vendor . '/src');
121
    }
122
123
    protected function getInstalledDirectories(bool $is_json = false): array
124
    {
125
        $resources = resource_path('lang');
126
127
        return $is_json
128
            ? File::files($resources)
129
            : File::directories($resources);
130
    }
131
132 12
    protected function normalizeNames(array $directories): array
133
    {
134
        return array_map(function ($dir) {
135 12
            return File::name($dir);
136 12
        }, $directories);
137
    }
138
139 12
    protected function addDefaultLocale(array &$locales): void
140
    {
141 12
        array_push($locales, Config::getLocale());
142 12
    }
143
144 12
    protected function filterLocales(array $locales): array
145
    {
146 12
        return ArrFacade::unique($locales);
147
    }
148
}
149