Passed
Push — main ( 545ade...3cfdef )
by Andrey
79:39 queued 77:29
created

Path::getSourcePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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: ' . $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 sourceFull(string $package, string $locale, ?string $filename, bool $is_json = false): string
25
    {
26
        $suffix = $is_json ? $locale . '.json' : $filename;
27
28
        return $this->source($package, $locale) . '/' . $suffix;
29
    }
30
31 20
    public function target(string $locale, bool $is_json = false): string
32
    {
33 20
        $this->log('Getting the path to the target files of the localization: ' . $locale);
34
35 20
        $path = $this->getTargetPath();
36
37 20
        $suffix = $is_json ? '' : '/' . $locale;
38
39 20
        return $this->clean($path) . $suffix;
40
    }
41
42 12
    public function targetFull(string $locale, ?string $filename, bool $is_json = false): string
43
    {
44 12
        $suffix = $is_json ? '../' . $locale . '.json' : $filename;
45
46 12
        return $this->target($locale) . '/' . $suffix;
47
    }
48
49 16
    public function locales(string $package, string $locale = null): string
50
    {
51 16
        $this->log('Getting the path to excluding English localization: ' . $locale);
52
53 16
        return $this->cleanable($this->getBasePath(), $package, $this->getLocalesPath(), $locale);
54
    }
55
56
    public function directory(string $path): string
57
    {
58
        $this->log('Getting the path to a directory: ' . $path);
59
60
        $path = pathinfo($path, PATHINFO_DIRNAME);
61
62
        return $this->clean($path);
0 ignored issues
show
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

62
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
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...
63
    }
64
65 12
    public function filename(string $path): string
66
    {
67 12
        $this->log('Getting file name without extension and path: ' . $path);
68
69 12
        $path = pathinfo($path, PATHINFO_FILENAME);
70
71 12
        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

71
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
72
    }
73
74 11
    public function extension(string $path): string
75
    {
76 11
        $this->log('Getting file extension from path: ' . $path);
77
78 11
        $path = pathinfo($path, PATHINFO_EXTENSION);
79
80 11
        return $this->clean($path);
0 ignored issues
show
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

80
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
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...
81
    }
82
83 20
    protected function cleanable(...$values): string
84
    {
85 20
        $this->log('Preparing a directory from an array of variables...');
86
87 20
        foreach ($values as &$value) {
88 20
            $value = $this->clean($value);
89
        }
90
91 20
        return implode('/', $values);
92
    }
93
94 20
    protected function clean(string $path = null): ?string
95
    {
96 20
        $this->log('Clearing the path from the trailing character: ' . $path);
97
98 20
        return ! empty($path) ? rtrim($path, '\\/') : $path;
99
    }
100
101 20
    protected function isEnglish(string $locale): bool
102
    {
103 20
        $this->log('Check if localization is English: ' . $locale);
104
105 20
        return $locale === LocalesList::ENGLISH;
106
    }
107
108 20
    protected function getBasePath(): string
109
    {
110 20
        return ConfigFacade::basePath();
111
    }
112
113 20
    protected function getSourcePath(): string
114
    {
115 20
        return ConfigFacade::sourcePath();
116
    }
117
118 16
    protected function getLocalesPath(): string
119
    {
120 16
        return ConfigFacade::localesPath();
121
    }
122
123 20
    protected function getTargetPath(): string
124
    {
125 20
        return ConfigFacade::resourcesPath();
126
    }
127
}
128