Passed
Push — main ( 9ad958...bccbba )
by Andrey
72:16 queued 69:33
created

Path   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 121
ccs 44
cts 52
cp 0.8462
rs 10
c 3
b 0
f 0
wmc 21

15 Methods

Rating   Name   Duplication   Size   Complexity  
A source() 0 9 2
A filename() 0 7 1
A cleanable() 0 9 2
A targetFull() 0 7 2
A isEnglish() 0 5 1
A target() 0 9 2
A getSourcePath() 0 3 1
A clean() 0 5 2
A locales() 0 5 1
A getBasePath() 0 3 1
A extension() 0 7 1
A getLocalesPath() 0 3 1
A directory() 0 7 1
A getTargetPath() 0 3 1
A sourceFull() 0 7 2
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use Helldar\LaravelLangPublisher\Concerns\Logger;
6
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList;
7
use Helldar\LaravelLangPublisher\Facades\Config as ConfigFacade;
8
9
final class Path
10
{
11
    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...
12
13 20
    public function source(string $package, string $locale): string
14
    {
15 20
        $this->log('Getting the path to the source files of the localization:', $package, $locale);
16
17 20
        if ($this->isEnglish($locale)) {
18 20
            return $this->cleanable($this->getBasePath(), $package, $this->getSourcePath());
19
        }
20
21 5
        return $this->locales($package, $locale);
22
    }
23
24
    public function sourceFull(string $package, string $locale, ?string $filename, bool $is_json = false): string
25
    {
26
        $this->log('Getting the full path to the source files of the localization:', $package, $locale, $filename, $is_json);
27
28
        $suffix = $is_json ? $locale . '.json' : $filename;
29
30
        return $this->source($package, $locale) . '/' . $suffix;
31
    }
32
33 20
    public function target(string $locale, bool $is_json = false): string
34
    {
35 20
        $this->log('Getting the path to the target files of the localization:', $locale, $is_json);
36
37 20
        $path = $this->getTargetPath();
38
39 20
        $suffix = $is_json ? '' : '/' . $locale;
40
41 20
        return $this->clean($path) . $suffix;
42
    }
43
44 12
    public function targetFull(string $locale, ?string $filename, bool $is_json = false): string
45
    {
46 12
        $this->log('Getting the full path to the target files of the localization:', $locale, $filename, $is_json);
47
48 12
        $suffix = $is_json ? '../' . $locale . '.json' : $filename;
49
50 12
        return $this->target($locale) . '/' . $suffix;
51
    }
52
53 16
    public function locales(string $package, string $locale = null): string
54
    {
55 16
        $this->log('Getting the path to the source translation files for', $package, $locale);
56
57 16
        return $this->cleanable($this->getBasePath(), $package, $this->getLocalesPath(), $locale);
58
    }
59
60
    public function directory(string $path): string
61
    {
62
        $this->log('Getting the path to a directory:', $path);
63
64
        $path = pathinfo($path, PATHINFO_DIRNAME);
65
66
        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

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

75
        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...
76
    }
77
78 11
    public function extension(string $path): string
79
    {
80 11
        $this->log('Getting file extension from path:', $path);
81
82 11
        $path = pathinfo($path, PATHINFO_EXTENSION);
83
84 11
        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

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