Passed
Push — main ( 8320d0...c2748c )
by Andrey
45:28 queued 42:35
created

Path::directory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

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

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

70
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
71
    }
72
73
    public function basename(string $path): string
74
    {
75
        $this->log('Getting file basename without extension and path:', $path);
76
77
        $path = pathinfo($path, PATHINFO_BASENAME);
78
79
        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

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

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