Passed
Branch master (72fde7)
by Observer
01:31
created

c()   F

Complexity

Conditions 21
Paths 183

Size

Total Lines 85
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 85
c 0
b 0
f 0
rs 3.475
nc 183
nop 2
cc 21

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace VoidEngine;
4
5
function err_status (bool $status = null): bool
6
{
7
    $oldStatus = $GLOBALS['error_status'];
8
9
    if ($status !== null)
10
        $GLOBALS['error_status'] = $status;
11
12
    return $oldStatus;
13
}
14
15
function err_no (): bool
16
{
17
    return err_status (false);
18
}
19
20
function err_yes (): bool
21
{
22
    return err_status (true);
23
}
24
25
function run (string $path, ...$args)
26
{
27
    return (new Process)->start ($path, ...$args);
0 ignored issues
show
Bug introduced by
The method start() does not exist on VoidEngine\Process. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

27
    return (new Process)->/** @scrutinizer ignore-call */ start ($path, ...$args);
Loading history...
28
}
29
30
function vbs_exec (string $code)
31
{
32
    file_put_contents ($path = getenv ('temp') .'/'. crc32 ($code) .'.vbs', $code);
33
34
    (new \COM ('WScript.Shell'))->Run ($path, 0, true);
35
36
    unlink ($path);
37
}
38
39
function php_errors_check (string $code): ?array
40
{
41
    try
42
    {
43
        eval ('return; '. $code);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
44
    }
45
46
    catch (\Throwable $e)
0 ignored issues
show
Unused Code introduced by
catch (\Throwable $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
47
    {
48
        return [
49
            'text' => $e->getMessage (), 
50
			'line' => $e->getLine ()
51
        ];
52
    }
53
54
    return null;
55
}
56
57
function enum (string $name): array
58
{
59
    return [
60
        substr ($name, strrpos ($name, '.') + 1),
61
        ($name = substr ($name, 0, strrpos ($name, '.'))) .', '. substr ($name, 0, strrpos ($name, '.'))
62
    ];
63
}
64
65
function getNetArray (string $type, array $items = []): WFObject
66
{
67
    $array = (new WFClass ('System.Array', null))
68
        ->createInstance (VoidEngine::objectType ($type), $size = sizeof ($items));
0 ignored issues
show
Bug introduced by
The method createInstance() does not exist on VoidEngine\WFClass. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

68
        ->/** @scrutinizer ignore-call */ createInstance (VoidEngine::objectType ($type), $size = sizeof ($items));
Loading history...
69
70
    for ($i = 0; $i < $size; ++$i)
71
        $array[$i] = array_shift ($items);
72
    
73
    return $array;
74
}
75
76
function dir_create (string $path, int $mode = 0777): void
77
{
78
    if (!is_dir ($path))
79
        mkdir ($path, $mode, true);
80
}
81
82
function dir_delete (string $path): bool
83
{
84
    if (!is_dir ($path))
85
        return false;
86
87
    foreach (array_slice (scandir ($path), 2) as $file)
0 ignored issues
show
Bug introduced by
It seems like scandir($path) can also be of type false; however, parameter $array of array_slice() 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

87
    foreach (array_slice (/** @scrutinizer ignore-type */ scandir ($path), 2) as $file)
Loading history...
88
        if (is_dir ($file = $path .'/'. $file))
89
        {
90
            dir_delete ($file);
91
92
            if (is_dir ($file))
93
                rmdir ($file);
94
        }
95
96
        else unlink ($file);
97
98
    rmdir ($path);
99
100
    return true;
101
}
102
103
function dir_clean (string $path): void
104
{
105
    dir_delete ($path);
106
    dir_create ($path);
107
}
108
109
function dir_copy (string $from, string $to): bool
110
{
111
    if (!is_dir ($from))
112
        return false;
113
114
    if (!is_dir ($to))
115
        dir_create ($to);
116
117
    foreach (array_slice (scandir ($from), 2) as $file)
0 ignored issues
show
Bug introduced by
It seems like scandir($from) can also be of type false; however, parameter $array of array_slice() 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

117
    foreach (array_slice (/** @scrutinizer ignore-type */ scandir ($from), 2) as $file)
Loading history...
118
        if (is_dir ($f = $from .'/'. $file))
119
            dir_copy ($f, $to .'/'. $file);
120
121
        else copy ($f, $to .'/'. $file);
122
123
    return true;
124
}
125
126
function getARGBColor (string $color)
127
{
128
    return (new WFClass ('System.Drawing.ColorTranslator'))->fromHtml ($color);
0 ignored issues
show
Bug introduced by
The method fromHtml() does not exist on VoidEngine\WFClass. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

128
    return (new WFClass ('System.Drawing.ColorTranslator'))->/** @scrutinizer ignore-call */ fromHtml ($color);
Loading history...
129
}
130
131
function replaceSl (string $string): string
132
{
133
    return str_replace ('\\', '/', $string);
134
}
135
136
function replaceSr (string $string): string
137
{
138
    return str_replace ('/', '\\', $string);
139
}
140
141
function basenameNoExt (string $path): string
142
{
143
    return pathinfo ($path, PATHINFO_FILENAME);
144
}
145
146
function file_ext (string $path): string
147
{
148
    return strtolower (pathinfo ($path, PATHINFO_EXTENSION));
149
}
150
151
function filepathNoExt (string $path): string
152
{
153
    return dirname ($path) .'/'. basenameNoExt ($path);
154
}
155
156
function array_first (array $array)
157
{
158
    return array_shift ($array);
159
}
160
161
function array_end (array $array)
162
{
163
    return array_pop ($array);
164
}
165
166
function substr_icount (string $haystack, string $needle, ...$params): int
167
{
168
	return substr_count (strtolower ($haystack), strtolower ($needle), ...$params);
0 ignored issues
show
Bug introduced by
$params is expanded, but the parameter $offset of substr_count() does not expect variable arguments. ( Ignorable by Annotation )

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

168
	return substr_count (strtolower ($haystack), strtolower ($needle), /** @scrutinizer ignore-type */ ...$params);
Loading history...
169
}
170
171
function str_replace_assoc (string $subject, array $replacements): string
172
{
173
	return str_replace (array_keys ($replacements), array_values ($replacements), $subject);
174
}
175
176
function pre (...$args): void
177
{
178
	message (sizeof ($args) < 2 ? current ($args) : $args);
0 ignored issues
show
Bug introduced by
The function message was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

178
	/** @scrutinizer ignore-call */ 
179
 message (sizeof ($args) < 2 ? current ($args) : $args);
Loading history...
179
}
180
181
function messageBox (string $message, string $caption = '', ...$args)
182
{
183
    return (new MessageBox)->show ($message, $caption, ...$args);
0 ignored issues
show
Bug introduced by
The method show() does not exist on VoidEngine\MessageBox. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

183
    return (new MessageBox)->/** @scrutinizer ignore-call */ show ($message, $caption, ...$args);
Loading history...
184
}
185
186
class Components
187
{
188
    static $components = [];
189
190
    public static function addComponent (int $selector, object $object): void
191
    {
192
        self::$components[$selector] = $object;
193
    }
194
195
    public static function getComponent (int $selector)
196
    {
197
        return isset (self::$components[$selector]) ?
198
            self::$components[$selector] : false;
199
    }
200
201
    public static function componentExists (int $selector): bool
202
    {
203
        return isset (self::$components[$selector]);
204
    }
205
206
    public static function removeComponent (int $selector): void
207
    {
208
        unset (self::$components[$selector]);
209
    }
210
211
    public static function cleanJunk (): void
212
    {
213
        // TODO: более строгие правила очистки мусорных объектов
214
        
215
        foreach (self::$components as $selector => $object)
216
        {
217
            try
218
            {
219
                if ($object->getType ()->isSubclassOf (VoidEngine::objectType ('System.Windows.Forms.Form', 'System.Windows.Forms')))
220
                    continue;
221
            }
222
223
            catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
224
            
225
            VoidEngine::destructObject ($selector);
226
227
            if (!VoidEngine::objectExists ($selector))
228
                unset (self::$components[$selector]);
229
        }
230
    }
231
}
232
233
function _c (int $selector)
234
{
235
    return Components::getComponent ($selector);
236
}
237
238
function c ($name, bool $returnAllSimilarObjects = false)
239
{
240
    if (is_int ($name) && is_object ($object = _c ($name)))
241
        return $object;
242
243
    else
244
    {
245
        $path    = explode ('->', $name);
246
        $similar = [];
247
248
        foreach (Components::$components as $object)
249
            try
250
            {
251
                if ($object->name == end ($path))
252
                {
253
                    if (sizeof ($path) > 1)
254
                        try
255
                        {
256
                            if (is_object ($parent = _c($object->parent->selector)))
257
                            {
258
                                if (c(join ('->', array_slice ($path, 0, -1))) == $parent)
259
                                {
260
                                    if ($returnAllSimilarObjects)
261
                                        $similar[] = $object;
262
263
                                    else return $object;
264
                                }
265
266
                                else continue;
267
                            }
268
269
                            else continue;
270
                        }
271
272
                        catch (\Throwable $e)
273
                        {
274
                            continue;
275
                        }
276
277
                    else
278
                    {
279
                        if ($returnAllSimilarObjects)
280
                            $similar[] = $object;
281
282
                        else return $object;
283
                    }
284
                }
285
            }
286
287
            catch (\Exception $e)
288
            {
289
                continue;
290
            }
291
292
        if (sizeof ($path) == 2)
293
        {
294
            $objects = c($path[1], true);
295
296
            if (is_array ($objects))
297
            {
298
                foreach ($objects as $id => $object)
299
                    try
300
                    {
301
                        while (is_object ($parent = _c($object->parent->selector)))
302
                        {
303
                            if ($parent->getType ()->isSubclassOf (VoidEngine::objectType ('System.Windows.Forms.Form', 'System.Windows.Forms')) && $parent->name == $path[0])
304
                                return $objects[$id];
305
306
                            else $object = $parent;
307
                        }
308
                    }
309
310
                    catch (\Throwable $e)
311
					{
312
						continue;
313
					}
314
315
                return false;
316
            }
317
318
            else return false;
319
        }
320
321
        else return $returnAllSimilarObjects && sizeof ($similar) > 0 ?
322
            $similar : false;
323
    }
324
}
325
326
function setTimer (int $interval, callable $function): Timer
327
{
328
    $timer = new Timer;
329
    $timer->interval = $interval;
0 ignored issues
show
Bug Best Practice introduced by
The property interval does not exist on VoidEngine\Timer. Since you implemented __set, consider adding a @property annotation.
Loading history...
330
    
331
    $timer->tickEvent = function ($self) use ($function)
0 ignored issues
show
Bug Best Practice introduced by
The property tickEvent does not exist on VoidEngine\Timer. Since you implemented __set, consider adding a @property annotation.
Loading history...
332
    {
333
        call_user_func ($function, $self);
334
    };
335
    
336
	$timer->start ();
0 ignored issues
show
Bug introduced by
The method start() does not exist on VoidEngine\Timer. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

336
	$timer->/** @scrutinizer ignore-call */ 
337
         start ();
Loading history...
337
    
338
    return $timer;
339
}
340
341
// FIXME: выполняется несколько раз, а не единожды
342
function setTimeout (int $timeout, callable $function): Timer
343
{
344
    $timer = new Timer;
345
    $timer->interval = $timeout;
0 ignored issues
show
Bug Best Practice introduced by
The property interval does not exist on VoidEngine\Timer. Since you implemented __set, consider adding a @property annotation.
Loading history...
346
    
347
    $timer->tickEvent = function ($self) use ($function)
0 ignored issues
show
Bug Best Practice introduced by
The property tickEvent does not exist on VoidEngine\Timer. Since you implemented __set, consider adding a @property annotation.
Loading history...
348
    {
349
        call_user_func ($function, $self);
350
351
        $self->stop ();
352
    };
353
    
354
    $timer->start ();
355
    
356
	return $timer;
357
}
358
359
class Clipboard
360
{
361
    public static function getText ()
362
    {
363
        return (new WFClass ('System.Windows.Forms.Clipboard'))->getText ();
0 ignored issues
show
Bug introduced by
The method getText() does not exist on VoidEngine\WFClass. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

363
        return (new WFClass ('System.Windows.Forms.Clipboard'))->/** @scrutinizer ignore-call */ getText ();
Loading history...
364
    }
365
    
366
    public static function setText (string $text): void
367
    {
368
        (new WFClass ('System.Windows.Forms.Clipboard'))->setText ($text);
0 ignored issues
show
Bug introduced by
The method setText() does not exist on VoidEngine\WFClass. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

368
        (new WFClass ('System.Windows.Forms.Clipboard'))->/** @scrutinizer ignore-call */ setText ($text);
Loading history...
369
    }
370
    
371
    public static function getFiles (): array
372
    {
373
        return (new WFClass ('System.Windows.Forms.Clipboard'))->getFileDropList ()->list;
0 ignored issues
show
Bug Best Practice introduced by
The property list does not exist on VoidEngine\WFObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method getFileDropList() does not exist on VoidEngine\WFClass. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

373
        return (new WFClass ('System.Windows.Forms.Clipboard'))->/** @scrutinizer ignore-call */ getFileDropList ()->list;
Loading history...
Bug Best Practice introduced by
The expression return new VoidEngine\WF...getFileDropList()->list could return the type VoidEngine\WFObject which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
374
    }
375
    
376
    public static function setFiles (array $files): void
377
    {
378
        $collection = new WFObject ('System.Collections.Specialized.StringCollection');
379
380
        foreach ($files as $file)
381
            $collection->add ((string) $file);
0 ignored issues
show
Bug introduced by
The method add() does not exist on VoidEngine\WFObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

381
            $collection->/** @scrutinizer ignore-call */ 
382
                         add ((string) $file);
Loading history...
382
383
        (new WFClass ('System.Windows.Forms.Clipboard'))->setFileDropList ($collection);
0 ignored issues
show
Bug introduced by
The method setFileDropList() does not exist on VoidEngine\WFClass. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

383
        (new WFClass ('System.Windows.Forms.Clipboard'))->/** @scrutinizer ignore-call */ setFileDropList ($collection);
Loading history...
384
        VoidEngine::removeObjects ($collection->selector);
0 ignored issues
show
Bug Best Practice introduced by
The property $selector is declared protected in VoidEngine\WFObject. Since you implement __get, consider adding a @property or @property-read.
Loading history...
385
    }
386
}
387
388
class Cursor
389
{
390
    protected $cursor;
391
392
    public function __construct (int $handle = null)
393
    {
394
        $handle !== null ?
395
            $this->cursor = new WFObject ('System.Windows.Forms.Cursor', 'auto', $handle) :
396
            $this->cursor = new WFClass ('System.Windows.Forms.Cursor');
397
    }
398
399
    public function getPosition (): array
400
    {
401
        $pos = $this->cursor->position;
0 ignored issues
show
Bug Best Practice introduced by
The property position does not exist on VoidEngine\WFObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property position does not exist on VoidEngine\WFClass. Since you implemented __get, consider adding a @property annotation.
Loading history...
402
403
        return [
404
            $pos->x,
0 ignored issues
show
Bug Best Practice introduced by
The property x does not exist on VoidEngine\WFObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
405
            $pos->y
0 ignored issues
show
Bug Best Practice introduced by
The property y does not exist on VoidEngine\WFObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
406
        ];
407
    }
408
}
409
410
function get_cursor_x (int $handle = null): int
411
{
412
    return (new Cursor ($handle))->getPosition ()[0];
0 ignored issues
show
Bug Best Practice introduced by
The expression return new VoidEngine\Cu...ndle)->getPosition()[0] could return the type VoidEngine\WFObject which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
413
}
414
415
function get_cursor_y (int $handle = null): int
416
{
417
    return (new Cursor ($handle))->getPosition ()[1];
0 ignored issues
show
Bug Best Practice introduced by
The expression return new VoidEngine\Cu...ndle)->getPosition()[1] could return the type VoidEngine\WFObject which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
418
}
419
420
function get_cursor_pos (int $handle = null): array
421
{
422
    return (new Cursor ($handle))->getPosition ();
423
}
424
425
set_error_handler (function ($no, $str, $file, $line)
426
{
427
    // Мог ли я здесь сделать более адекватный код с использованием pow/sqrt? Да, мог
428
    // Почему не сделал? Скорость важнее
429
    static $errarr = [
430
        1     => 'E_ERROR',
431
        2     => 'E_WARNING',
432
        4     => 'E_PARSE',
433
        8     => 'E_NOTICE',
434
        16    => 'E_CORE_ERROR',
435
        32    => 'E_CORE_WARNING',
436
        64    => 'E_COMPILE_ERROR',
437
        128   => 'E_COMPILE_WARNING',
438
        256   => 'E_USER_ERROR',
439
        512   => 'E_USER_WARNING',
440
        1024  => 'E_USER_NOTICE',
441
        2048  => 'E_STRICT',
442
        4096  => 'E_RECOVERABLE_ERROR',
443
        8192  => 'E_DEPRECATED',
444
        16384 => 'E_USER_DEPRECATED'
445
    ];
446
447
    if ($GLOBALS['error_status'])
448
        message ([
0 ignored issues
show
Bug introduced by
The function message was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

448
        /** @scrutinizer ignore-call */ 
449
        message ([
Loading history...
449
            'type'      => $errarr[$no],
450
            'text'      => $str,
451
            'file'      => $file,
452
            'line'      => $line,
453
            'backtrace' => debug_backtrace ()
454
        ], 'PHP Script Error');
455
});
456