Passed
Push — main ( 55f929...127e84 )
by Andrey
161:41 queued 158:10
created

Path::sourceFull()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 6
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 12
    public function source(string $locale): string
14
    {
15 12
        $this->log('Getting the path to the source files of the localization: ' . $locale);
16
17 12
        if ($this->isEnglish($locale)) {
18 12
            return ConfigFacade::basePath();
19
        }
20
21 5
        return $this->locales($locale);
22
    }
23
24
    public function sourceFull(string $locale, ?string $filename, bool $is_json = false): string
25
    {
26
        $suffix = $is_json ? $locale . '.json' : $filename;
27
28
        return $this->source($locale) . '/' . $suffix;
29
    }
30
31 12
    public function target(string $locale, bool $is_json = false): string
32
    {
33 12
        $this->log('Getting the path to the target files of the localization: ' . $locale);
34
35 12
        $path = ConfigFacade::resourcesPath();
36
37 12
        $suffix = $is_json ? '' : '/' . $locale;
38
39 12
        return $this->clean($path) . $suffix;
40
    }
41
42 12
    public function targetFull(string $locale, ?string $filename, bool $is_json = false): string
43
    {
44 12
        $suffix = $is_json ? '../' . $locale . '.json' : $filename;
45
46 12
        return $this->target($locale) . '/' . $suffix;
47
    }
48
49 5
    public function locales(string $locale = null): string
50
    {
51 5
        $this->log('Getting the path to excluding English localization: ' . $locale);
52
53 5
        $path = ConfigFacade::localesPath();
54
55 5
        return $this->clean($path) . '/' . $locale;
56
    }
57
58 12
    public function filename(string $path): string
59
    {
60 12
        $this->log('Getting file name without extension and path: ' . $path);
61
62 12
        $path = pathinfo($path, PATHINFO_FILENAME);
63
64 12
        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

64
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
65
    }
66
67
    public function directory(string $path): string
68
    {
69
        $this->log('Getting the path to a directory: ' . $path);
70
71
        $path = pathinfo($path, PATHINFO_DIRNAME);
72
73
        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

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

82
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
83
    }
84
85 12
    protected function clean(string $path = null): ?string
86
    {
87 12
        $this->log('Clearing the path from the trailing character: ' . $path);
88
89 12
        return ! empty($path) ? rtrim($path, '\\/') : $path;
90
    }
91
92 12
    protected function isEnglish(string $locale): bool
93
    {
94 12
        $this->log('Check if localization is English: ' . $locale);
95
96 12
        return $locale === LocalesList::ENGLISH;
97
    }
98
}
99