Passed
Pull Request — main (#109)
by Andrey
71:41 queued 56:39
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\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
    public function source(string $package, string $locale): string
15 24
    {
16
        $this->log('Getting the path to the source files of the localization:', $package, $locale);
17 24
18
        if ($this->isEnglish($locale)) {
19 24
            return $this->cleanable($this->getBasePath(), $package, $this->getSourcePath());
20 24
        }
21
22
        return $this->locales($package, $locale);
23 7
    }
24
25
    public function target(string $locale, bool $is_json = false): string
26 24
    {
27
        $this->log('Getting the path to the target files of the localization:', $locale, $is_json);
28 24
29
        $path = $this->getTargetPath();
30 24
31
        $suffix = $is_json ? '' : '/' . $locale;
32 24
33
        return $this->clean($path) . $suffix;
34 24
    }
35
36
    public function targetFull(string $locale, ?string $filename): string
37 15
    {
38
        $this->log('Getting the full path to the target files of the localization:', $locale, $filename);
39 15
40
        $is_json = ! empty($filename) && $this->isJson($filename);
41 15
42
        $path = $this->target($locale, $is_json);
43 15
44
        return $path . '/' . $filename;
45 15
    }
46
47
    public function locales(string $package, string $locale = null): string
48 19
    {
49
        $this->log('Getting the path to the source translation files for', $package, $locale);
50 19
51
        return $this->cleanable($this->getBasePath(), $package, $this->getLocalesPath(), $locale);
52 19
    }
53
54
    public function directory(string $path): string
55 14
    {
56
        $this->log('Getting the directory name from file path:', $path);
57 14
58
        $path = pathinfo($path, PATHINFO_DIRNAME);
59 14
60
        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

60
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
61 14
    }
62
63
    public function filename(string $path): string
64 14
    {
65
        $this->log('Getting file name without extension and path:', $path);
66 14
67
        $path = pathinfo($path, PATHINFO_FILENAME);
68 14
69
        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 14
    }
71
72
    public function basename(string $path): string
73
    {
74
        $this->log('Getting file basename without extension and path:', $path);
75
76
        $path = pathinfo($path, PATHINFO_BASENAME);
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
    public function extension(string $path): string
82 14
    {
83
        $this->log('Getting file extension from path:', $path);
84 14
85
        $path = pathinfo($path, PATHINFO_EXTENSION);
86 14
87
        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 14
    }
89
90
    public function clean(string $path = null, bool $both = false): ?string
91 24
    {
92
        $this->log('Clearing the path from the trailing character:', $path);
93 24
94
        if (! empty($path)) {
95 24
            $chars = '\\/';
96 24
97
            return $both ? trim($path, $chars) : rtrim($path, $chars);
98 24
        }
99
100
        return $path;
101 19
    }
102
103
    protected function cleanable(...$values): string
104 24
    {
105
        $this->log('Cleaning values to compile a path:', $values);
106 24
107
        foreach ($values as &$value) {
108 24
            $value = $this->clean($value);
109 24
        }
110
111
        return implode('/', $values);
112 24
    }
113
114
    protected function getBasePath(): string
115 24
    {
116
        return ConfigFacade::basePath();
117 24
    }
118
119 24
    protected function getSourcePath(): string
120
    {
121
        return ConfigFacade::sourcePath();
122 24
    }
123
124 24
    protected function getLocalesPath(): string
125
    {
126
        return ConfigFacade::localesPath();
127 24
    }
128
129 24
    protected function getTargetPath(): string
130
    {
131
        return ConfigFacade::resourcesPath();
132 19
    }
133
}
134