Issues (22)

src/Support/Locale.php (3 issues)

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