Passed
Push — main ( 3f2cb1...ed2a6d )
by Andrey
86:25 queued 84:06
created

Path::directory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

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

57
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
58
    }
59
60 14
    public function extension(string $path): string
61
    {
62 14
        $this->log('Getting file extension from path:', $path);
63
64 14
        $path = pathinfo($path, PATHINFO_EXTENSION);
65
66 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

66
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
67
    }
68
69 24
    protected function cleanable(...$values): string
70
    {
71 24
        $this->log('Cleaning values to compile a path:', $values);
72
73 24
        foreach ($values as &$value) {
74 24
            $value = $this->clean($value);
75
        }
76
77 24
        return implode('/', $values);
78
    }
79
80 24
    protected function clean(string $path = null): ?string
81
    {
82 24
        $this->log('Clearing the path from the trailing character:', $path);
83
84 24
        return ! empty($path) ? rtrim($path, '\\/') : $path;
85
    }
86
87 24
    protected function isEnglish(string $locale): bool
88
    {
89 24
        $this->log('Check if localization is English: ' . $locale);
90
91 24
        return $locale === LocalesList::ENGLISH;
92
    }
93
94 24
    protected function getBasePath(): string
95
    {
96 24
        return ConfigFacade::basePath();
97
    }
98
99 24
    protected function getSourcePath(): string
100
    {
101 24
        return ConfigFacade::sourcePath();
102
    }
103
104 19
    protected function getLocalesPath(): string
105
    {
106 19
        return ConfigFacade::localesPath();
107
    }
108
109 24
    protected function getTargetPath(): string
110
    {
111 24
        return ConfigFacade::resourcesPath();
112
    }
113
}
114