Passed
Pull Request — main (#123)
by Andrey
58:14 queued 43:11
created

Path::target()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 10
c 3
b 0
f 0
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use Helldar\LaravelLangPublisher\Concerns\Contains;
6
use Helldar\LaravelLangPublisher\Concerns\Logger;
7
use Helldar\LaravelLangPublisher\Facades\Config as ConfigFacade;
8
9
final class Path
10
{
11
    use Contains;
12
    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...
13
14 43
    public function source(string $package, string $locale): string
15
    {
16 43
        $this->log('Getting the path to the source files of the localization:', $package, $locale);
17
18 43
        if ($this->isEnglish($locale)) {
19 43
            return $this->cleanable($this->getBasePath(), $package, $this->getSourcePath());
20
        }
21
22 14
        return $this->locales($package, $locale);
23
    }
24
25 43
    public function sourceFull(string $package, string $locale, string $filename): string
26
    {
27 43
        $this->log('Getting the path to the source file of the localization:', $package, $locale, $filename);
28
29 43
        $path = $this->source($package, $locale);
30
31 43
        return $this->cleanable($path, $filename);
32
    }
33 43
34
    public function target(string $locale, bool $is_json = false): string
35
    {
36 30
        $this->log('Getting the path to the target files of the localization:', $locale, $is_json);
37
38 30
        $path = $this->getTargetPath();
39
40 30
        $suffix = $is_json ? '' : '/' . $locale;
41
42 30
        return $this->clean($path) . $suffix;
43
    }
44 30
45
    public function targetFull(string $locale, ?string $filename): string
46
    {
47 36
        $this->log('Getting the full path to the target files of the localization:', $locale, $filename);
48
49 36
        $is_json = ! empty($filename) && $this->isJson($filename);
50
51 36
        $path = $this->target($locale, $is_json);
52
53
        return $path . '/' . $filename;
54 28
    }
55
56 28
    public function locales(string $package, string $locale = null): string
57
    {
58 28
        $this->log('Getting the path to the source translation files for', $package, $locale);
59
60 28
        return $this->cleanable($this->getBasePath(), $package, $this->getLocalesPath(), $locale);
61
    }
62
63 28
    public function directory(string $path): string
64
    {
65 28
        $this->log('Getting the directory name from file path:', $path);
66
67 28
        $path = pathinfo($path, PATHINFO_DIRNAME);
68
69 28
        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

69
        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...
70
    }
71
72
    public function filename(string $path): string
73
    {
74
        $this->log('Getting file name without extension and path:', $path);
75
76
        $path = pathinfo($path, PATHINFO_FILENAME);
77
78
        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

78
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
79
    }
80
81 28
    public function basename(string $path): string
82
    {
83 28
        $this->log('Getting file basename without extension and path:', $path);
84
85 28
        $path = pathinfo($path, PATHINFO_BASENAME);
86
87 28
        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

87
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
88
    }
89
90 43
    public function extension(string $path): string
91
    {
92 43
        $this->log('Getting file extension from path:', $path);
93
94 43
        $path = pathinfo($path, PATHINFO_EXTENSION);
95 43
96
        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

96
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
97 43
    }
98
99
    public function clean(string $path = null, bool $both = false): ?string
100 36
    {
101
        $this->log('Clearing the path from the trailing character:', $path);
102
103 43
        if (! empty($path)) {
104
            $chars = '\\/';
105 43
106
            return $both ? trim($path, $chars) : rtrim($path, $chars);
107 43
        }
108 43
109
        return $path;
110
    }
111 43
112
    protected function cleanable(...$values): string
113
    {
114 43
        $this->log('Cleaning values to compile a path:', $values);
115
116 43
        foreach ($values as &$value) {
117
            $value = $this->clean($value);
118
        }
119 43
120
        return implode('/', $values);
121 43
    }
122
123
    protected function getBasePath(): string
124 36
    {
125
        return ConfigFacade::basePath();
126 36
    }
127
128
    protected function getSourcePath(): string
129 43
    {
130
        return ConfigFacade::sourcePath();
131 43
    }
132
133
    protected function getLocalesPath(): string
134
    {
135
        return ConfigFacade::localesPath();
136
    }
137
138
    protected function getTargetPath(): string
139
    {
140
        return ConfigFacade::resourcesPath();
141
    }
142
}
143