Passed
Push — main ( c2748c...e886f6 )
by Andrey
90:35 queued 86:23
created

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

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

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
72
    public function basename(string $path): string
73
    {
74
        $this->log('Getting file basename without extension and path:', $path);
75
76
        $path = pathinfo($path, PATHINFO_BASENAME);
77
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
81 14
    public function extension(string $path): string
82
    {
83 14
        $this->log('Getting file extension from path:', $path);
84
85 14
        $path = pathinfo($path, PATHINFO_EXTENSION);
86
87 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

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