Passed
Push — 10.x ( 195203...176c92 )
by Andrey
13:36
created

Path::sourceFull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
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 24
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 24
        if ($this->isEnglish($locale)) {
19
            return $this->cleanable($this->getBasePath(), $package, $this->getSourcePath());
20
        }
21 7
22
        return $this->locales($package, $locale);
23
    }
24 24
25
    public function sourceFull(string $package, string $locale, string $filename): string
26 24
    {
27
        $this->log('Getting the path to the source file of the localization:', $package, $locale, $filename);
28 24
29
        $path = $this->source($package, $locale);
30 24
31
        return $this->cleanable($path, $filename);
32 24
    }
33
34
    public function target(string $locale, bool $is_json = false): string
35 15
    {
36
        $this->log('Getting the path to the target files of the localization:', $locale, $is_json);
37 15
38
        $path = $this->getTargetPath();
39 15
40
        $suffix = $is_json ? '' : '/' . $locale;
41 15
42
        return $this->clean($path) . $suffix;
43
    }
44 19
45
    public function targetFull(string $locale, ?string $filename): string
46 19
    {
47
        $this->log('Getting the full path to the target files of the localization:', $locale, $filename);
48 19
49
        $is_json = ! empty($filename) && $this->isJson($filename);
50
51 14
        $path = $this->target($locale, $is_json);
52
53 14
        return $path . '/' . $filename;
54
    }
55 14
56
    public function locales(string $package, string $locale = null): string
57 14
    {
58
        $this->log('Getting the path to the source translation files for', $package, $locale);
59
60 14
        return $this->cleanable($this->getBasePath(), $package, $this->getLocalesPath(), $locale);
61
    }
62 14
63
    public function directory(string $path): string
64 14
    {
65
        $this->log('Getting the directory name from file path:', $path);
66 14
67
        $path = pathinfo($path, PATHINFO_DIRNAME);
68
69 24
        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 24
72
    public function filename(string $path): string
73 24
    {
74 24
        $this->log('Getting file name without extension and path:', $path);
75
76
        $path = pathinfo($path, PATHINFO_FILENAME);
77 24
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 24
81
    public function basename(string $path): string
82 24
    {
83
        $this->log('Getting file basename without extension and path:', $path);
84 24
85
        $path = pathinfo($path, PATHINFO_BASENAME);
86
87 24
        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 24
90
    public function extension(string $path): string
91 24
    {
92
        $this->log('Getting file extension from path:', $path);
93
94 24
        $path = pathinfo($path, PATHINFO_EXTENSION);
95
96 24
        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
    }
98
99 24
    public function clean(string $path = null, bool $both = false): ?string
100
    {
101 24
        $this->log('Clearing the path from the trailing character:', $path);
102
103
        if (! empty($path)) {
104 19
            $chars = '\\/';
105
106 19
            return $both ? trim($path, $chars) : rtrim($path, $chars);
107
        }
108
109 24
        return $path;
110
    }
111 24
112
    protected function cleanable(...$values): string
113
    {
114
        $this->log('Cleaning values to compile a path:', $values);
115
116
        foreach ($values as &$value) {
117
            $value = $this->clean($value);
118
        }
119
120
        return implode('/', $values);
121
    }
122
123
    protected function getBasePath(): string
124
    {
125
        return ConfigFacade::basePath();
126
    }
127
128
    protected function getSourcePath(): string
129
    {
130
        return ConfigFacade::sourcePath();
131
    }
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