Passed
Pull Request — main (#84)
by Andrey
59:33 queued 44:34
created

Path::extension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

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
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;
12
13
    public function source(string $locale): string
14
    {
15
        $this->log('Getting the path to the source files of the localization: ' . $locale);
16
17
        if ($this->isEnglish($locale)) {
18
            return ConfigFacade::basePath();
19
        }
20
21
        return $this->locales($locale);
22
    }
23
24
    public function target(string $locale, bool $is_json = false): string
25
    {
26
        $this->log('Getting the path to the target files of the localization: ' . $locale);
27
28
        $path = ConfigFacade::resourcesPath();
29
30
        $suffix = $is_json ? '' : '/' . $locale;
31
32
        return $this->clean($path) . $suffix;
33
    }
34
35
    public function locales(string $locale = null): string
36
    {
37
        $this->log('Getting the path to excluding English localization: ' . $locale);
38
39
        $path = ConfigFacade::localesPath();
40
41
        return $this->clean($path) . '/' . $locale;
42
    }
43
44
    public function filename(string $path): string
45
    {
46
        $this->log('Getting file name without extension and path: ' . $path);
47
48
        $path = pathinfo($path, PATHINFO_FILENAME);
49
50
        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

50
        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...
51
    }
52
53
    public function directory(string $path): string
54
    {
55
        $this->log('Getting the path to a directory: ' . $path);
56
57
        $path = pathinfo($path, PATHINFO_DIRNAME);
58
59
        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

59
        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...
60
    }
61
62
    public function extension(string $path): string
63
    {
64
        $this->log('Getting file extension from path: ' . $path);
65
66
        $path = pathinfo($path, PATHINFO_EXTENSION);
67
68
        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

68
        return $this->clean(/** @scrutinizer ignore-type */ $path);
Loading history...
69
    }
70
71
    protected function clean(string $path = null): ?string
72
    {
73
        $this->log('Clearing the path from the trailing character: ' . $path);
74
75
        return ! empty($path) ? rtrim($path, '\\/') : $path;
76
    }
77
78
    protected function isEnglish(string $locale): bool
79
    {
80
        $this->log('Check if localization is English: ' . $locale);
81
82
        return $locale === LocalesList::ENGLISH;
83
    }
84
}
85