Passed
Push — master ( a7279d...feb425 )
by Andrey
11:45
created

Locale   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 16
c 1
b 0
f 0
dl 0
loc 80
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSourceDirectories() 0 4 1
A get() 0 7 1
A getFallback() 0 3 1
A getDefault() 0 3 1
A installed() 0 3 1
A getInstalledDirectories() 0 4 1
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\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
     * Getting the default localization name.
38
     *
39
     * @return string
40
     */
41
    public function getDefault(): string
42
    {
43
        return Config::getLocale();
44
    }
45
46
    /**
47
     * Getting the fallback localization name.
48
     *
49
     * @return string
50
     */
51
    public function getFallback(): string
52
    {
53
        return Config::getFallbackLocale();
54
    }
55
56
    protected function get(array $directories): array
57
    {
58
        $locales = $this->normalizeNames($directories);
59
60
        $this->addDefaultLocale($locales);
61
62
        return $this->filterLocales($locales);
63
    }
64
65
    protected function getSourceDirectories(): array
66
    {
67
        return File::directories(
68
            Config::getVendorPath()
69
        );
70
    }
71
72
    protected function getInstalledDirectories(): array
73
    {
74
        return File::directories(
75
            resource_path('lang')
76
        );
77
    }
78
79
    protected function normalizeNames(array $directories): array
80
    {
81
        return array_map(function ($dir) {
82
            return File::name($dir);
83
        }, $directories);
84
    }
85
86
    protected function addDefaultLocale(array &$locales): void
87
    {
88
        array_push($locales, Config::getLocale());
89
    }
90
91
    protected function filterLocales(array $locales): array
92
    {
93
        return ArrFacade::unique($locales);
94
    }
95
}
96