Passed
Push — master ( fe8484...90b95a )
by butschster
17:12 queued 18s
created

CatalogueLoader::loadCatalogue()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 6.0007

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 51
ccs 35
cts 36
cp 0.9722
rs 8.8337
c 0
b 0
f 0
cc 6
nc 10
nop 1
crap 6.0007

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Translator\Catalogue;
6
7
use Spiral\Logger\Traits\LoggerTrait;
8
use Spiral\Translator\Catalogue;
9
use Spiral\Translator\CatalogueInterface;
10
use Spiral\Translator\Config\TranslatorConfig;
11
use Symfony\Component\Finder\Finder;
12
13
final class CatalogueLoader implements LoaderInterface
14
{
15
    use LoggerTrait;
16
17 51
    public function __construct(
18
        private readonly TranslatorConfig $config
19
    ) {
20 51
    }
21
22 44
    public function hasLocale(string $locale): bool
23
    {
24 44
        $locale = \preg_replace('/[^a-zA-Z_]/', '', \mb_strtolower($locale));
25
26 44
        foreach ($this->getDirectories() as $directory) {
27 44
            if (\is_dir($this->config->getLocaleDirectory($locale, $directory))) {
28 44
                return true;
29
            }
30
        }
31
32 2
        return false;
33
    }
34
35 29
    public function getLocales(): array
36
    {
37 29
        $directories = $this->getDirectories();
38 29
        if ($directories === []) {
39
            return [];
40
        }
41
42 29
        $finder = new Finder();
43 29
        $locales = [];
44 29
        foreach ($finder->in($directories)->directories() as $directory) {
45 29
            $locales[] = $directory->getFilename();
46
        }
47
48 29
        return \array_unique($locales);
49
    }
50
51 43
    public function loadCatalogue(string $locale): CatalogueInterface
52
    {
53 43
        $locale = \preg_replace('/[^a-zA-Z_]/', '', \mb_strtolower($locale));
54 43
        $catalogue = new Catalogue($locale);
55
56 43
        if (!$this->hasLocale($locale)) {
57
            return $catalogue;
58
        }
59
60 43
        $directories = [];
61 43
        foreach ($this->getDirectories() as $directory) {
62 43
            if (\is_dir($this->config->getLocaleDirectory($locale, $directory))) {
63 43
                $directories[] = $this->config->getLocaleDirectory($locale, $directory);
64
            }
65
        }
66
67 43
        $finder = new Finder();
68 43
        foreach ($finder->in($directories)->files() as $file) {
69 40
            $this->getLogger()->info(
70 40
                \sprintf(
71 40
                    "found locale domain file '%s'",
72 40
                    $file->getFilename()
73 40
                ),
74 40
                ['file' => $file->getFilename()]
75 40
            );
76
77
            //Per application agreement domain name must present in filename
78 40
            $domain = \strstr($file->getFilename(), '.', true);
79
80 40
            if (!$this->config->hasLoader($file->getExtension())) {
81 1
                $this->getLogger()->warning(
82 1
                    \sprintf(
83 1
                        "unable to load domain file '%s', no loader found",
84 1
                        $file->getFilename()
85 1
                    ),
86 1
                    ['file' => $file->getFilename()]
87 1
                );
88
89 1
                continue;
90
            }
91
92 40
            $catalogue->mergeFrom(
93 40
                $this->config->getLoader($file->getExtension())->load(
94 40
                    (string)$file,
95 40
                    $locale,
96 40
                    $domain
97 40
                )
98 40
            );
99
        }
100
101 43
        return $catalogue;
102
    }
103
104
    /**
105
     * @return array<array-key, non-empty-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, non-empty-string> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, non-empty-string>.
Loading history...
106
     */
107 47
    private function getDirectories(): array
108
    {
109 47
        $directories = [];
110 47
        if (\is_dir($this->config->getLocalesDirectory())) {
111 47
            $directories[] = $this->config->getLocalesDirectory();
112
        }
113
114 47
        foreach ($this->config->getDirectories() as $directory) {
115 4
            if (\is_dir($directory)) {
116 4
                $directories[] = $directory;
117
            }
118
        }
119
120 47
        return $directories;
121
    }
122
}
123