Passed
Push — master ( 7f8b3f...7ac9f0 )
by Observer
01:45
created

setTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
nc 1
nop 2
dl 0
loc 15
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace VoidEngine;
4
5
function err_status (bool $status = null): bool
6
{
7
    $oldStatus = $GLOBALS['__debug']['error_status'];
8
9
    if ($status !== null)
10
        $GLOBALS['__debug']['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);
1 ignored issue
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 enum_parse (string $baseType, string $value)
66
{
67
    try
68
    {
69
        return \VoidCore::callMethod (\VoidCore::getClass ('System.Enum',''), ['parse', 'object'], \VoidCore::typeof ($baseType), $value, true);
1 ignored issue
show
Bug introduced by
The type VoidCore was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
70
    }
71
72
    catch (\WinFormsException $e)
0 ignored issues
show
Bug introduced by
The type WinFormsException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
73
    {
74
        return (new WFClass ($baseType))->$value;
75
    }
76
}
77
78
function getNetArray (string $type, array $items = []): WFObject
79
{
80
    $array = (new WFClass ('System.Array', null))
81
        ->createInstance (\VoidCore::typeof ($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

81
        ->/** @scrutinizer ignore-call */ createInstance (\VoidCore::typeof ($type), $size = sizeof ($items));
Loading history...
82
83
    for ($i = 0; $i < $size; ++$i)
84
        $array[$i] = array_shift ($items);
85
    
86
    return $array;
87
}
88
89
function dir_create (string $path, int $mode = 0777): void
90
{
91
    if (!is_dir ($path))
92
        mkdir ($path, $mode, true);
93
}
94
95
function dir_delete (string $path): bool
96
{
97
    if (!is_dir ($path))
98
        return false;
99
100
    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

100
    foreach (array_slice (/** @scrutinizer ignore-type */ scandir ($path), 2) as $file)
Loading history...
101
        if (is_dir ($file = $path .'/'. $file))
102
        {
103
            dir_delete ($file);
104
105
            if (is_dir ($file))
106
                rmdir ($file);
107
        }
108
109
        else unlink ($file);
110
111
    rmdir ($path);
112
113
    return true;
114
}
115
116
function dir_clean (string $path): void
117
{
118
    dir_delete ($path);
119
    dir_create ($path);
120
}
121
122
function dir_copy (string $from, string $to): bool
123
{
124
    if (!is_dir ($from))
125
        return false;
126
127
    if (!is_dir ($to))
128
        dir_create ($to);
129
130
    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

130
    foreach (array_slice (/** @scrutinizer ignore-type */ scandir ($from), 2) as $file)
Loading history...
131
        if (is_dir ($f = $from .'/'. $file))
132
            dir_copy ($f, $to .'/'. $file);
133
134
        else copy ($f, $to .'/'. $file);
135
136
    return true;
137
}
138
139
function getARGBColor (string $color)
140
{
141
    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

141
    return (new WFClass ('System.Drawing.ColorTranslator'))->/** @scrutinizer ignore-call */ fromHtml ($color);
Loading history...
142
}
143
144
function replaceSl (string $string): string
145
{
146
    return str_replace ('\\', '/', $string);
147
}
148
149
function replaceSr (string $string): string
150
{
151
    return str_replace ('/', '\\', $string);
152
}
153
154
function basenameNoExt (string $path): string
155
{
156
    return pathinfo ($path, PATHINFO_FILENAME);
157
}
158
159
function file_ext (string $path): string
160
{
161
    return strtolower (pathinfo ($path, PATHINFO_EXTENSION));
162
}
163
164
function filepathNoExt (string $path): string
165
{
166
    return dirname ($path) .'/'. basenameNoExt ($path);
167
}
168
169
function array_first (array $array)
170
{
171
    return array_shift ($array);
172
}
173
174
function array_end (array $array)
175
{
176
    return array_pop ($array);
177
}
178
179
function substr_icount (string $haystack, string $needle, ...$params): int
180
{
181
	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

181
	return substr_count (strtolower ($haystack), strtolower ($needle), /** @scrutinizer ignore-type */ ...$params);
Loading history...
182
}
183
184
function str_replace_assoc (string $subject, array $replacements): string
185
{
186
	return str_replace (array_keys ($replacements), array_values ($replacements), $subject);
187
}
188
189
function pre (...$args): void
190
{
191
	message (sizeof ($args) < 2 ? current ($args) : $args);
1 ignored issue
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

191
	/** @scrutinizer ignore-call */ 
192
 message (sizeof ($args) < 2 ? current ($args) : $args);
Loading history...
192
}
193
194
function messageBox (string $message, string $caption = '', ...$args)
195
{
196
    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

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

349
	$timer->/** @scrutinizer ignore-call */ 
350
         start ();
Loading history...
350
    
351
    return $timer;
352
}
353
354
// FIXME: выполняется несколько раз, а не единожды
355
function setTimeout (int $timeout, callable $function): Timer
356
{
357
    $timer = new Timer;
358
    $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...
359
    
360
    $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...
361
    {
362
        call_user_func ($function, $self);
363
364
        $self->stop ();
365
    };
366
    
367
    $timer->start ();
368
    
369
	return $timer;
370
}
371
372
class Clipboard
373
{
374
    public static function getText ()
375
    {
376
        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

376
        return (new WFClass ('System.Windows.Forms.Clipboard'))->/** @scrutinizer ignore-call */ getText ();
Loading history...
377
    }
378
    
379
    public static function setText (string $text): void
380
    {
381
        (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

381
        (new WFClass ('System.Windows.Forms.Clipboard'))->/** @scrutinizer ignore-call */ setText ($text);
Loading history...
382
    }
383
    
384
    public static function getFiles (): array
385
    {
386
        return (new WFClass ('System.Windows.Forms.Clipboard'))->getFileDropList ()->list;
0 ignored issues
show
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...
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

386
        return (new WFClass ('System.Windows.Forms.Clipboard'))->/** @scrutinizer ignore-call */ getFileDropList ()->list;
Loading history...
387
    }
388
    
389
    public static function setFiles (array $files): void
390
    {
391
        $collection = new WFObject ('System.Collections.Specialized.StringCollection');
392
393
        foreach ($files as $file)
394
            $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

394
            $collection->/** @scrutinizer ignore-call */ 
395
                         add ((string) $file);
Loading history...
395
396
        (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

396
        (new WFClass ('System.Windows.Forms.Clipboard'))->/** @scrutinizer ignore-call */ setFileDropList ($collection);
Loading history...
397
        \VoidCore::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...
398
    }
399
}
400
401
class Cursor
402
{
403
    protected $cursor;
404
405
    public function __construct (int $handle = null)
406
    {
407
        $handle !== null ?
408
            $this->cursor = new WFObject ('System.Windows.Forms.Cursor', false, $handle) :
409
            $this->cursor = new WFClass ('System.Windows.Forms.Cursor');
410
    }
411
412
    public function getPosition (): array
413
    {
414
        $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...
415
416
        return [
417
            $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...
418
            $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...
419
        ];
420
    }
421
}
422
423
function get_cursor_x (int $handle = null): int
424
{
425
    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...
426
}
427
428
function get_cursor_y (int $handle = null): int
429
{
430
    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...
431
}
432
433
function get_cursor_pos (int $handle = null): array
434
{
435
    return (new Cursor ($handle))->getPosition ();
436
}
437
438
set_error_handler (function ($no, $str, $file, $line)
439
{
440
    // Мог ли я здесь сделать более адекватный код с использованием pow/sqrt? Да, мог
441
    // Почему не сделал? Скорость важнее
442
    static $errarr = [
443
        1     => 'E_ERROR',
444
        2     => 'E_WARNING',
445
        4     => 'E_PARSE',
446
        8     => 'E_NOTICE',
447
        16    => 'E_CORE_ERROR',
448
        32    => 'E_CORE_WARNING',
449
        64    => 'E_COMPILE_ERROR',
450
        128   => 'E_COMPILE_WARNING',
451
        256   => 'E_USER_ERROR',
452
        512   => 'E_USER_WARNING',
453
        1024  => 'E_USER_NOTICE',
454
        2048  => 'E_STRICT',
455
        4096  => 'E_RECOVERABLE_ERROR',
456
        8192  => 'E_DEPRECATED',
457
        16384 => 'E_USER_DEPRECATED'
458
    ];
459
460
    if ($GLOBALS['__debug']['error_status'])
461
        message ([
1 ignored issue
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

461
        /** @scrutinizer ignore-call */ 
462
        message ([
Loading history...
462
            'type'      => $errarr[$no],
463
            'text'      => $str,
464
            'file'      => $file,
465
            'line'      => $line
466
        ], 'PHP Script Error');
467
});
468