Issues (157)

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 $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 {
68
            if ($type !== ApiInterface::RAML_TYPE_NUMBER) {
69
                if ($type === ApiInterface::RAML_TYPE_STRING) {
70
                    $value = PhpInterface::QUOTES . $value . PhpInterface::QUOTES;
71
                } else {
72
                    settype($value, ApiInterface::RAML_TO_PHP_TYPES[$type]);
73
                }
74
            }
75
        }
76
        $this->setTabs($tabs);
77
        $this->sourceCode .= PhpInterface::QUOTES . $param . PhpInterface::QUOTES
78
            . PhpInterface::SPACE . PhpInterface::DOUBLE_ARROW . PhpInterface::SPACE
79
            . $value . PhpInterface::COMMA . PHP_EOL;
80
    }
81
82
    /**
83
     * @param int $amount
84
     *
85
     * @return mixed
86
     */
87
    abstract protected function setTabs(int $amount = 1): void;
88
89
    /**
90
     * Opens finite state machine
91
     *
92
     * @param string $entity
93
     * @param string $field
94
     */
95
    private function openFsm(string $entity, string $field): void
96
    {
97
        $this->openEntity(strtolower($entity), 2);
98
        $this->openEntity(strtolower($field), 3);
99
        $this->setParam(ConfigInterface::ENABLED, ApiInterface::RAML_TYPE_BOOLEAN, PhpInterface::PHP_TYPES_BOOL_TRUE, 4);
100
        $this->openEntity(ConfigInterface::STATES, 4);
101
    }
102
103
    private function openCache(string $entity): void
104
    {
105
        $this->openEntity(strtolower($entity), 2);
106
        $this->setParam(ConfigInterface::ENABLED,  ApiInterface::RAML_TYPE_BOOLEAN,PhpInterface::PHP_TYPES_BOOL_TRUE, 3);
107
    }
108
109
    /**
110
     * Opens finite state machine
111
     *
112
     * @param string $entity
113
     * @param string $field
114
     */
115
    private function openSc(string $entity, string $field): void
116
    {
117
        $this->openEntity(strtolower($entity), 2);
118
        $this->openEntity(strtolower($field), 3);
119
        $this->setParam(ConfigInterface::ENABLED, ApiInterface::RAML_TYPE_BOOLEAN, PhpInterface::PHP_TYPES_BOOL_TRUE, 4);
120
    }
121
122
123
124
    /**
125
     * @param string $entity
126
     * @param string $field
127
     */
128
    private function openBitMask(string $entity, string $field): void
129
    {
130
        $this->openEntity(strtolower($entity), 2);
131
        $this->openEntity(strtolower($field), 3);
132
        $this->setParam(ConfigInterface::ENABLED, ApiInterface::RAML_TYPE_BOOLEAN, PhpInterface::PHP_TYPES_BOOL_TRUE, 4);
133
        $this->openEntity(ConfigInterface::FLAGS, 4);
134
    }
135
136
    /**
137
     * Opens config file entity
138
     * @param string $entity
139
     * @param int $tabs
140
     */
141
    private function openEntity(string $entity, int $tabs = 1): void
142
    {
143
        $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

143
        $this->sourceCode .= /** @scrutinizer ignore-type */ $this->setTabs($tabs) . PhpInterface::QUOTES . $entity
Loading history...
144
            . PhpInterface::QUOTES . PhpInterface::DOUBLE_ARROW . PhpInterface::SPACE
145
            . PhpInterface::OPEN_BRACKET . PHP_EOL;
146
        $this->openedBrackets[] = $tabs;
147
    }
148
149
    /**
150
     * Closes any configuration entity
151
     *
152
     * @param int $tabs
153
     * @param bool $isSingle
154
     */
155
    public function closeEntity(int $tabs = 1, $isSingle = false): void
156
    {
157
        $this->sourceCode .= $this->setTabs($tabs) . PhpInterface::CLOSE_BRACKET . PhpInterface::COMMA . PHP_EOL;
0 ignored issues
show
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

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