Passed
Push — 9.x ( d63f62...9358ea )
by Andrey
20:32 queued 05:32
created

Path::sourceFull()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use Helldar\LaravelLangPublisher\Concerns\Logger;
6
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList;
7
use Helldar\LaravelLangPublisher\Facades\Config as ConfigFacade;
8
9
final class Path
10
{
11
    use Logger;
0 ignored issues
show
Bug introduced by
The trait Helldar\LaravelLangPublisher\Concerns\Logger requires the property $output which is not provided by Helldar\LaravelLangPublisher\Support\Path.
Loading history...
12
13
    public function source(string $package, string $locale): string
14
    {
15
        $this->log('Getting the path to the source files of the localization:', $package, $locale);
16
17
        if ($this->isEnglish($locale)) {
18
            return $this->cleanable($this->getBasePath(), $package, $this->getSourcePath());
19
        }
20
21
        return $this->locales($package, $locale);
22
    }
23
24
    public function target(string $locale, bool $is_json = false): string
25
    {
26
        $this->log('Getting the path to the target files of the localization:', $locale, $is_json);
27
28
        $path = $this->getTargetPath();
29
30
        $suffix = $is_json ? '' : '/' . $locale;
31
32
        return $this->clean($path) . $suffix;
33
    }
34
35
    public function targetFull(string $locale, ?string $filename, bool $is_json = false): string
36
    {
37
        $this->log('Getting the full path to the target files of the localization:', $locale, $filename, $is_json);
38
39
        $suffix = $is_json ? '../' . $locale . '.json' : $filename;
40
41
        return $this->target($locale) . '/' . $suffix;
42
    }
43
44
    public function locales(string $package, string $locale = null): string
45
    {
46
        $this->log('Getting the path to the source translation files for', $package, $locale);
47
48
        return $this->cleanable($this->getBasePath(), $package, $this->getLocalesPath(), $locale);
49
    }
50
51
    public function filename(string $path): string
52
    {
53
        $this->log('Getting file name without extension and path:', $path);
54
55
        $path = pathinfo($path, PATHINFO_FILENAME);
56
57
        return $this->clean($path);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->clean($path) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
It seems like $path can also be of type array; however, parameter $path of Helldar\LaravelLangPublisher\Support\Path::clean() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
58
    }
59
60
    public function extension(string $path): string
61
    {
62
        $this->log('Getting file extension from path:', $path);
63
64
        $path = pathinfo($path, PATHINFO_EXTENSION);
65
66
        return $this->clean($path);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->clean($path) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
It seems like $path can also be of type array; however, parameter $path of Helldar\LaravelLangPublisher\Support\Path::clean() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
67
    }
68
69
    protected function cleanable(...$values): string
70
    {
71
        $this->log('Cleaning values to compile a path:', $values);
72
73
        foreach ($values as &$value) {
74
            $value = $this->clean($value);
75
        }
76
77
        return implode('/', $values);
78
    }
79
80
    protected function clean(string $path = null): ?string
81
    {
82
        $this->log('Clearing the path from the trailing character:', $path);
83
84
        return ! empty($path) ? rtrim($path, '\\/') : $path;
85
    }
86
87
    protected function isEnglish(string $locale): bool
88
    {
89
        $this->log('Check if localization is English: ' . $locale);
90
91
        return $locale === LocalesList::ENGLISH;
92
    }
93
94
    protected function getBasePath(): string
95
    {
96
        return ConfigFacade::basePath();
97
    }
98
99
    protected function getSourcePath(): string
100
    {
101
        return ConfigFacade::sourcePath();
102
    }
103
104
    protected function getLocalesPath(): string
105
    {
106
        return ConfigFacade::localesPath();
107
    }
108
109
    protected function getTargetPath(): string
110
    {
111
        return ConfigFacade::resourcesPath();
112
    }
113
}
114