Passed
Pull Request — 9.x (#103)
by Andrey
12:39
created

Path::directory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

63
        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...
64
    }
65
66
    public function filename(string $path): string
67
    {
68
        $this->log('Getting file name without extension and path:', $path);
69
70
        $path = pathinfo($path, PATHINFO_FILENAME);
71
72
        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

72
        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...
73
    }
74
75
    public function basename(string $path): string
76
    {
77
        $this->log('Getting file basename without extension and path:', $path);
78
79
        $path = pathinfo($path, PATHINFO_BASENAME);
80
81
        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

81
        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...
82
    }
83
84
    public function extension(string $path): string
85
    {
86
        $this->log('Getting file extension from path:', $path);
87
88
        $path = pathinfo($path, PATHINFO_EXTENSION);
89
90
        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

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