Passed
Push — main ( aea149...a0cc24 )
by Chema
04:40 queued 25s
created

PhelBuildConfig::normalizePhpExtension()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phel\Config;
6
7
use JsonSerializable;
8
9
use function count;
10
11
/**
12
 * @psalm-suppress DeprecatedProperty
13
 */
14
final class PhelBuildConfig implements JsonSerializable
15
{
16
    public const DEST_DIR = 'dir';
17
18
    public const MAIN_PHEL_NAMESPACE = 'main-phel-namespace';
19
20
    public const MAIN_PHP_FILENAME = 'main-php-filename';
21
22
    public const MAIN_PHP_PATH = 'main-php-path';
23
24
    private const DEFAULT_DEST_DIR = 'out';
25
26
    private const DEFAULT_PHP_FILENAME = 'index.php';
27
28
    private string $mainPhelNamespace = '';
29
30
    private string $destDir = '';
31
32
    /** @deprecated in favor of $mainPhpPath */
33
    private string $mainPhpFilename = '';
34
35
    private string $mainPhpPath = '';
36
37 4
    public static function fromArray(array $array): self
38
    {
39 4
        $self = new self();
40 4
        if (isset($array[self::MAIN_PHEL_NAMESPACE])) {
41 4
            $self->mainPhelNamespace = $array[self::MAIN_PHEL_NAMESPACE];
42
        }
43
44 4
        if (isset($array[self::DEST_DIR])) {
45 4
            $self->destDir = $array[self::DEST_DIR];
46
        }
47
48 4
        if (isset($array[self::MAIN_PHP_FILENAME])) {
49 4
            $self->mainPhpFilename = $array[self::MAIN_PHP_FILENAME];
0 ignored issues
show
Deprecated Code introduced by
The property Phel\Config\PhelBuildConfig::$mainPhpFilename has been deprecated: in favor of $mainPhpPath ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

49
            /** @scrutinizer ignore-deprecated */ $self->mainPhpFilename = $array[self::MAIN_PHP_FILENAME];

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
50
        }
51
52 4
        if (isset($array[self::MAIN_PHP_PATH])) {
53 4
            $self->mainPhpPath = $array[self::MAIN_PHP_PATH];
54
        }
55
56 4
        return $self;
57
    }
58
59 18
    public function jsonSerialize(): array
60
    {
61 18
        return [
62 18
            self::MAIN_PHEL_NAMESPACE => $this->mainPhelNamespace,
63 18
            self::DEST_DIR => $this->getDestDir(),
64 18
            self::MAIN_PHP_FILENAME => $this->getPhpFilename(),
65 18
            self::MAIN_PHP_PATH => $this->getMainPhpPath(),
66 18
        ];
67
    }
68
69 7
    public function setMainPhelNamespace(string $namespace): self
70
    {
71 7
        $this->mainPhelNamespace = $namespace;
72 7
        return $this;
73
    }
74
75 3
    public function getMainPhelNamespace(): string
76
    {
77 3
        return $this->mainPhelNamespace;
78
    }
79
80 11
    public function setMainPhpPath(string $path): self
81
    {
82 11
        $this->mainPhpPath = $this->normalizePhpExtension($path);
83 11
        return $this;
84
    }
85
86 18
    public function getMainPhpPath(): string
87
    {
88 18
        if (str_contains($this->mainPhpPath, '/')) {
89 9
            return $this->mainPhpPath;
90
        }
91
92 9
        return sprintf(
93 9
            '%s/%s',
94 9
            $this->getDestDir(),
95 9
            $this->getPhpFilename(),
96 9
        );
97
    }
98
99
    /**
100
     * @deprecated in favor of setMainPhpPath()
101
     */
102 2
    public function setMainPhpFilename(string $name): self
103
    {
104 2
        $this->mainPhpFilename = $name;
0 ignored issues
show
Deprecated Code introduced by
The property Phel\Config\PhelBuildConfig::$mainPhpFilename has been deprecated: in favor of $mainPhpPath ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

104
        /** @scrutinizer ignore-deprecated */ $this->mainPhpFilename = $name;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
105 2
        return $this;
106
    }
107
108 3
    public function setDestDir(string $dir): self
109
    {
110 3
        $this->destDir = $dir;
111 3
        return $this;
112
    }
113
114 4
    public function shouldCreateEntryPointPhpFile(): bool
115
    {
116 4
        return (bool)$this->mainPhelNamespace;
117
    }
118
119 18
    private function getDestDir(): string
120
    {
121 18
        if ($this->destDir !== '') {
122 3
            return $this->destDir;
123
        }
124
125 15
        if ($this->mainPhpPath !== '') {
126 11
            $explode = explode('/', $this->mainPhpPath);
127 11
            if (count($explode) !== 1) {
128 9
                array_pop($explode);
129 9
                return implode('/', $explode);
130
            }
131
        }
132
133 6
        return self::DEFAULT_DEST_DIR;
134
    }
135
136 18
    private function getPhpFilename(): string
137
    {
138 18
        if ($this->mainPhpPath !== '') {
139 11
            $explode = explode('/', $this->mainPhpPath);
140 11
            if (count($explode) === 1) {
141 2
                return $explode[0];
142
            }
143
144 9
            return array_pop($explode);
145
        }
146
147 7
        if ($this->mainPhpFilename !== '') {
0 ignored issues
show
Deprecated Code introduced by
The property Phel\Config\PhelBuildConfig::$mainPhpFilename has been deprecated: in favor of $mainPhpPath ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

147
        if (/** @scrutinizer ignore-deprecated */ $this->mainPhpFilename !== '') {

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
148 1
            return $this->normalizePhpExtension($this->mainPhpFilename);
0 ignored issues
show
Deprecated Code introduced by
The property Phel\Config\PhelBuildConfig::$mainPhpFilename has been deprecated: in favor of $mainPhpPath ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

148
            return $this->normalizePhpExtension(/** @scrutinizer ignore-deprecated */ $this->mainPhpFilename);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
149
        }
150
151 6
        return self::DEFAULT_PHP_FILENAME;
152
    }
153
154 12
    private function normalizePhpExtension(string $string): string
155
    {
156 12
        $suffix = '.php';
157 12
        if (str_ends_with($string, $suffix)) {
158 8
            return $string;
159
        }
160
161 4
        return $string . $suffix;
162
    }
163
}
164