Passed
Push — master ( 079f1e...99c93a )
by Observer
01:36
created

EngineAdditions::compile()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
c 0
b 0
f 0
nc 128
nop 12
dl 0
loc 28
rs 8.2111

1 Method

Rating   Name   Duplication   Size   Complexity  
A NetObject::dispose() 0 3 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace VoidEngine;
4
5
use VoidCore;
6
7
class NetObject implements \ArrayAccess
8
{
9
    protected int $selector = 0;
10
    // protected ?string $name = null;
11
    // protected bool $isCollection = false;
12
13
    public function __construct ($name, $assembly = false, ...$args)
14
    {
15
        foreach ($args as $id => $arg)
16
            $args[$id] = EngineAdditions::uncoupleSelector ($arg);
17
        
18
        if (is_int ($name) && VoidCore::objectExists ($name))
19
            $this->selector = $name;
20
21
        elseif (is_string ($name))
22
            $this->selector = VoidCore::createObject ($name, $assembly, ...$args);
23
24
        else throw new \Exception ('Incorrect params passed');
25
26
        /*$this->isCollection = $this->getType ()
27
            ->isSubclassOf (VoidCore::typeof ('System.Collectons.Generic.ICollection'));*/
28
    }
29
30
    public function dispose (): void
31
    {
32
        VoidCore::removeObjects ($this->selector);
33
    }
34
35
    # Основные магические методы
36
37
    public function __get (string $name)
38
    {
39
        switch (strtolower ($name))
40
        {
41
            case 'count':
42
            case 'length':
43
                try
44
                {
45
                    return $this->getProperty ('Count');
46
                }
47
48
                catch (\WinformsException $e)
49
                {
50
                    return $this->getProperty ('Length');
51
                }
52
            break;
53
54
            case 'list':
55
                $size = $this->count;
56
                $list = [];
57
                
58
				for ($i = 0; $i < $size; ++$i)
59
                    $list[] = EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $i));
60
                
61
                return $list;
62
            break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
63
64
            case 'names':
65
                $size  = $this->count;
66
                $names = [];
67
                
68
                for ($i = 0; $i < $size; ++$i)
69
                    try
70
                    {
71
                        $names[] = VoidCore::getProperty (VoidCore::getArrayValue ($this->selector, [$i, VC_OBJECT]), 'Text');
72
                    }
73
74
                    catch (\WinformsException $e)
75
                    {
76
                        $names[] = VoidCore::getArrayValue ($this->selector, [$i, VC_STRING]);
77
                    }
78
                
79
                return $names;
80
            break;
81
        }
82
83
        if (method_exists ($this, $method = 'get_'. $name))
84
            return $this->$method ();
85
86
        return isset ($this->$name) ?
87
            $this->$name : EngineAdditions::coupleSelector ($this->getProperty ($name));
88
    }
89
90
    public function __set (string $name, $value): void
91
    {
92
        if (substr ($name, -5) == 'Event')
93
            Events::setEvent ($this->selector, substr ($name, 0, -5), $value);
94
95
        elseif (method_exists ($this, $method = 'set_'. $name))
96
            $this->$method ($value);
97
        
98
        else $this->setProperty ($name, EngineAdditions::uncoupleSelector ($value));
99
    }
100
101
    public function __call (string $name, array $args)
102
    {
103
        return EngineAdditions::coupleSelector ($this->callMethod ($name, array_map (
104
            fn ($arg) => EngineAdditions::uncoupleSelector ($arg), $args)));
105
    }
106
107
    # Управление VoidCore
108
109
    protected function getProperty ($name)
110
    {
111
        return VoidCore::getProperty ($this->selector, $name);
112
    }
113
114
    protected function setProperty (string $name, $value): void
115
    {
116
        VoidCore::setProperty ($this->selector, $name, $value);
117
    }
118
119
    protected function callMethod (string $name, array $args = [])
120
    {
121
        return VoidCore::callMethod ($this->selector, $name, ...$args);
122
    }
123
124
    # ArrayAccess
125
126
    public function offsetSet ($index, $value)
127
	{
128
        try
129
        {
130
            $index === null ?
131
                $this->callMethod ('Add', EngineAdditions::uncoupleSelector ($value)) :
0 ignored issues
show
Bug introduced by
It seems like VoidEngine\EngineAdditio...ncoupleSelector($value) can also be of type integer; however, parameter $args of VoidEngine\NetObject::callMethod() does only seem to accept array, 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

131
                $this->callMethod ('Add', /** @scrutinizer ignore-type */ EngineAdditions::uncoupleSelector ($value)) :
Loading history...
132
                $this->callMethod ('Insert', $index, EngineAdditions::uncoupleSelector ($value));
0 ignored issues
show
Unused Code introduced by
The call to VoidEngine\NetObject::callMethod() has too many arguments starting with VoidEngine\EngineAdditio...ncoupleSelector($value). ( Ignorable by Annotation )

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

132
                $this->/** @scrutinizer ignore-call */ 
133
                       callMethod ('Insert', $index, EngineAdditions::uncoupleSelector ($value));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
133
        }
134
135
        catch (\Throwable $e)
136
        {
137
            $index === null ?
138
                VoidCore::setArrayValue ($this->selector, $this->count, EngineAdditions::uncoupleSelector ($value)) :
139
                VoidCore::setArrayValue ($this->selector, $index, EngineAdditions::uncoupleSelector ($value));
140
        }
141
    }
142
	
143
	public function offsetGet ($index)
144
	{
145
		return EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $index), $this->selector);
0 ignored issues
show
Unused Code introduced by
The call to VoidEngine\EngineAdditions::coupleSelector() has too many arguments starting with $this->selector. ( Ignorable by Annotation )

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

145
		return EngineAdditions::/** @scrutinizer ignore-call */ coupleSelector (VoidCore::getArrayValue ($this->selector, $index), $this->selector);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
146
    }
147
	
148
	public function offsetUnset ($index): void
149
	{
150
		$this->callMethod ('RemoveAt', $index);
151
    }
152
    
153
    public function offsetExists ($index): bool
154
    {
155
        try
156
        {
157
            $this->offsetGet ($index);
158
        }
159
160
        catch (\WinformsException $e)
161
        {
162
            return false;
163
        }
164
165
        return true;
166
    }
167
168
    # Итерация массивов
169
170
    public function foreach (callable $callback, string $type = null): void
171
    {
172
        $size = $this->count;
173
174
        for ($i = 0; $i < $size; ++$i)
175
            $callback (EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $type !== null ? [$i, $type] : $i), $this->selector), $i);
0 ignored issues
show
Unused Code introduced by
The call to VoidEngine\EngineAdditions::coupleSelector() has too many arguments starting with $this->selector. ( Ignorable by Annotation )

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

175
            $callback (EngineAdditions::/** @scrutinizer ignore-call */ coupleSelector (VoidCore::getArrayValue ($this->selector, $type !== null ? [$i, $type] : $i), $this->selector), $i);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
176
    }
177
178
    public function where (callable $comparator, string $type = null): array
179
    {
180
        $size   = $this->count;
181
        $return = [];
182
183
        for ($i = 0; $i < $size; ++$i)
184
            if ($comparator ($value = EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $type !== null ? [$i, $type] : $i), $this->selector), $i))
0 ignored issues
show
Unused Code introduced by
The call to VoidEngine\EngineAdditions::coupleSelector() has too many arguments starting with $this->selector. ( Ignorable by Annotation )

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

184
            if ($comparator ($value = EngineAdditions::/** @scrutinizer ignore-call */ coupleSelector (VoidCore::getArrayValue ($this->selector, $type !== null ? [$i, $type] : $i), $this->selector), $i))

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
185
                $return[] = $value;
186
187
        return $return;
188
    }
189
190
    # Магические методы
191
192
    public function __destruct ()
193
    {
194
        VoidCore::destructObject ($this->selector);
195
    }
196
197
    public function __toString (): string
198
    {
199
        return $this->selector;
200
    }
201
202
    public function __debugInfo (): array
203
    {
204
        $info = ['selector' => $this->selector];
205
206
        try
207
        {
208
            $info['name'] = $this->getProperty ('Name');
209
        }
210
211
        catch (\WinformsException $e) {}
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
212
213
        try
214
        {
215
            $info['info'] = $this->callMethod ('ToString');
216
        }
217
218
        catch (\WinformsException $e) {}
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
219
220
        return $info;
221
    }
222
}
223
224
class NetClass extends NetObject
225
{
226
    public function __construct ($name, $assembly = false, ...$args)
227
    {
228
        if (is_int ($name) && VoidCore::objectExists ($name))
229
            $this->selector = $name;
230
231
        elseif (is_string ($name))
232
            $this->selector = VoidCore::getClass ($name, $assembly, ...$args);
233
234
        else throw new \Exception ('Incorrect params passed');
235
    }
236
}
237
238
class EngineAdditions
239
{
240
    public static function coupleSelector ($selector)
241
    {
242
        return is_int ($selector) && VoidCore::objectExists ($selector) ?
243
            new NetObject ($selector) : $selector;
244
    }
245
246
    public static function uncoupleSelector ($object)
247
    {
248
        return is_object ($object) && $object instanceof NetObject ?
249
            $object->selector : $object;
250
    }
251
}
252