Issues (150)

src/Blocks/ConfigTrait.php (4 issues)

Labels
Severity
1
<?php
2
3
namespace SoliDry\Blocks;
4
5
use SoliDry\Types\ConfigInterface;
6
use SoliDry\Types\PhpInterface;
7
use SoliDry\Types\ApiInterface;
8
9
/**
10
 * Class ConfigTrait
11
 *
12
 * @package SoliDry\Blocks
13
 * @property string sourceCode
14
 */
15
trait ConfigTrait
16
{
17
    public array $openedBrackets = [];
18
19
    /**
20
     *  Opens config's root element
21
     */
22
    private function openRoot(): void
23
    {
24
        $this->sourceCode .= PhpInterface::PHP_RETURN . PhpInterface::SPACE
25
            . PhpInterface::OPEN_BRACKET . PHP_EOL;
26
    }
27
28
    /**
29
     *  Closes config's root element
30
     */
31
    private function closeRoot(): void
32
    {
33
        $this->sourceCode .= PhpInterface::CLOSE_BRACKET . PhpInterface::SEMICOLON;
34
    }
35
36
    /**
37
     * Sets the default value of the $param name
38
     *
39
     * @param string $param
40
     * @param mixed $defaultValue
41
     */
42
    private function setParamDefault(string $param, $defaultValue): void
43
    {
44
        $this->sourceCode .= PhpInterface::TAB_PSR4 . PhpInterface::TAB_PSR4 . PhpInterface::QUOTES . $param .
45
            PhpInterface::QUOTES
46
            . PhpInterface::SPACE . PhpInterface::DOUBLE_ARROW . PhpInterface::SPACE
47
            . ((bool)$defaultValue === true ? PhpInterface::PHP_TYPES_BOOL_TRUE : $defaultValue) .
48
            PhpInterface::COMMA . PHP_EOL;
49
    }
50
51
    /**
52
     * Sets any config related param
53
     * @param string $param
54
     * @param string $type
55
     * @param string|bool $value
56
     * @param int $tabs
57
     */
58
    private function setParam(string $param, string $type, string $value, int $tabs = 1): void
59
    {
60
        // todo: this is ugly and stupid, because of Types misconception in RAML, PHP and YAML parse
61
        if ($type === ApiInterface::RAML_TYPE_BOOLEAN) {
62
            if ($value === PhpInterface::PHP_TYPES_BOOL_TRUE || (int)$value === 1) { // Yaml::parse converts true to 1, false to 0
63
                $value = PhpInterface::PHP_TYPES_BOOL_TRUE;
64
            } else {
65
                $value = PhpInterface::PHP_TYPES_BOOL_FALSE;
66
            }
67
        } else if ($type !== ApiInterface::RAML_TYPE_NUMBER) {
68
            if ($type === ApiInterface::RAML_TYPE_STRING) {
69
                $value = PhpInterface::QUOTES . $value . PhpInterface::QUOTES;
70
            } else {
71
                settype($value, ApiInterface::RAML_TO_PHP_TYPES[$type]);
72
            }
73
        }
74
        $this->setTabs($tabs);
75
        $this->sourceCode .= PhpInterface::QUOTES . $param . PhpInterface::QUOTES
76
            . PhpInterface::SPACE . PhpInterface::DOUBLE_ARROW . PhpInterface::SPACE
77
            . $value . PhpInterface::COMMA . PHP_EOL;
78
    }
79
80
    /**
81
     * @param int $amount
82
     *
83
     * @return mixed
84
     */
85
    abstract protected function setTabs(int $amount = 1): void;
86
87
    /**
88
     * Opens finite state machine
89
     *
90
     * @param string $entity
91
     * @param string $field
92
     */
93
    private function openFsm(string $entity, string $field): void
94
    {
95
        $this->openEntity(strtolower($entity), 2);
96
        $this->openEntity(strtolower($field), 3);
97
        $this->setParam(ConfigInterface::ENABLED, ApiInterface::RAML_TYPE_BOOLEAN, PhpInterface::PHP_TYPES_BOOL_TRUE, 4);
98
        $this->openEntity(ConfigInterface::STATES, 4);
99
    }
100
101
    private function openCache(string $entity): void
102
    {
103
        $this->openEntity(strtolower($entity), 2);
104
        $this->setParam(ConfigInterface::ENABLED,  ApiInterface::RAML_TYPE_BOOLEAN,PhpInterface::PHP_TYPES_BOOL_TRUE, 3);
105
    }
106
107
    /**
108
     * Opens finite state machine
109
     *
110
     * @param string $entity
111
     * @param string $field
112
     */
113
    private function openSc(string $entity, string $field): void
114
    {
115
        $this->openEntity(strtolower($entity), 2);
116
        $this->openEntity(strtolower($field), 3);
117
        $this->setParam(ConfigInterface::ENABLED, ApiInterface::RAML_TYPE_BOOLEAN, PhpInterface::PHP_TYPES_BOOL_TRUE, 4);
118
    }
119
120
121
122
    /**
123
     * @param string $entity
124
     * @param string $field
125
     */
126
    private function openBitMask(string $entity, string $field): void
127
    {
128
        $this->openEntity(strtolower($entity), 2);
129
        $this->openEntity(strtolower($field), 3);
130
        $this->setParam(ConfigInterface::ENABLED, ApiInterface::RAML_TYPE_BOOLEAN, PhpInterface::PHP_TYPES_BOOL_TRUE, 4);
131
        $this->openEntity(ConfigInterface::FLAGS, 4);
132
    }
133
134
    /**
135
     * Opens config file entity
136
     * @param string $entity
137
     * @param int $tabs
138
     */
139
    private function openEntity(string $entity, int $tabs = 1): void
140
    {
141
        $this->sourceCode .= $this->setTabs($tabs) . PhpInterface::QUOTES . $entity
0 ignored issues
show
Are you sure the usage of $this->setTabs($tabs) targeting SoliDry\Blocks\ConfigTrait::setTabs() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Are you sure $this->setTabs($tabs) of type void can be used in concatenation? ( Ignorable by Annotation )

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

141
        $this->sourceCode .= /** @scrutinizer ignore-type */ $this->setTabs($tabs) . PhpInterface::QUOTES . $entity
Loading history...
142
            . PhpInterface::QUOTES . PhpInterface::DOUBLE_ARROW . PhpInterface::SPACE
143
            . PhpInterface::OPEN_BRACKET . PHP_EOL;
144
        $this->openedBrackets[] = $tabs;
145
    }
146
147
    /**
148
     * Closes any configuration entity
149
     *
150
     * @param int $tabs
151
     * @param bool $isSingle
152
     */
153
    public function closeEntity(int $tabs = 1, $isSingle = false): void
154
    {
155
        $this->sourceCode .= $this->setTabs($tabs) . PhpInterface::CLOSE_BRACKET . PhpInterface::COMMA . PHP_EOL;
0 ignored issues
show
Are you sure the usage of $this->setTabs($tabs) targeting SoliDry\Blocks\ConfigTrait::setTabs() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Are you sure $this->setTabs($tabs) of type void can be used in concatenation? ( Ignorable by Annotation )

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

155
        $this->sourceCode .= /** @scrutinizer ignore-type */ $this->setTabs($tabs) . PhpInterface::CLOSE_BRACKET . PhpInterface::COMMA . PHP_EOL;
Loading history...
156
        if ($isSingle === true) {
157
            unset($this->openedBrackets[count($this->openedBrackets) - 1]);
158
            $this->openedBrackets = array_values($this->openedBrackets);
159
        }
160
    }
161
162
    /**
163
     *  Closes entities by reversing array of prev opened
164
     */
165
    private function closeEntities(): void
166
    {
167
        $this->openedBrackets = array_reverse($this->openedBrackets);
168
        foreach ($this->openedBrackets as $k => $tabs) {
169
            $this->closeEntity($tabs);
170
            unset($this->openedBrackets[$k]);
171
        }
172
    }
173
}