Passed
Pull Request — main (#104)
by Andrey
84:07 queued 69:01
created

Path::getTargetPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
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 20
    public function source(string $package, string $locale): string
14
    {
15 20
        $this->log('Getting the path to the source files of the localization:', $package, $locale);
16
17 20
        if ($this->isEnglish($locale)) {
18 20
            return $this->cleanable($this->getBasePath(), $package, $this->getSourcePath());
19
        }
20
21 5
        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 20
    }
34
35 20
    public function targetFull(string $locale, ?string $filename, bool $is_json = false): string
36
    {
37 20
        $this->log('Getting the full path to the target files of the localization:', $locale, $filename, $is_json);
38
39 20
        $suffix = $is_json ? '../' . $locale . '.json' : $filename;
40
41 20
        return $this->target($locale) . '/' . $suffix;
42
    }
43
44 12
    public function locales(string $package, string $locale = null): string
45
    {
46 12
        $this->log('Getting the path to the source translation files for', $package, $locale);
47
48 12
        return $this->cleanable($this->getBasePath(), $package, $this->getLocalesPath(), $locale);
49
    }
50 12
51
    public function filename(string $path): string
52
    {
53 16
        $this->log('Getting file name without extension and path:', $path);
54
55 16
        $path = pathinfo($path, PATHINFO_FILENAME);
56
57 16
        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 11
    protected function cleanable(...$values): string
70
    {
71 11
        $this->log('Cleaning values to compile a path:', $values);
72
73 11
        foreach ($values as &$value) {
74
            $value = $this->clean($value);
75 11
        }
76
77
        return implode('/', $values);
78 11
    }
79
80 11
    protected function clean(string $path = null): ?string
81
    {
82 11
        $this->log('Clearing the path from the trailing character:', $path);
83
84 11
        return ! empty($path) ? rtrim($path, '\\/') : $path;
85
    }
86
87 20
    protected function isEnglish(string $locale): bool
88
    {
89 20
        $this->log('Check if localization is English: ' . $locale);
90
91 20
        return $locale === LocalesList::ENGLISH;
92 20
    }
93
94
    protected function getBasePath(): string
95 20
    {
96
        return ConfigFacade::basePath();
97
    }
98 20
99
    protected function getSourcePath(): string
100 20
    {
101
        return ConfigFacade::sourcePath();
102 20
    }
103
104
    protected function getLocalesPath(): string
105 20
    {
106
        return ConfigFacade::localesPath();
107 20
    }
108
109 20
    protected function getTargetPath(): string
110
    {
111
        return ConfigFacade::resourcesPath();
112 20
    }
113
}
114