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

c()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 22
rs 9.2222
cc 6
nc 2
nop 1
1
<?php
2
3
namespace VoidEngine;
4
5
class Components
6
{
7
    protected static array $objects = [];
8
9
    public static function add (NetObject $object): void
10
    {
11
        self::$objects[$object->selector] = \WeakReference::create ($object);
0 ignored issues
show
Bug introduced by
The type WeakReference 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...
12
    }
13
14
    public static function get (int $selector): ?NetObject
15
    {
16
        if (!isset (self::$objects[$selector]))
17
            return null;
18
        
19
        $object = self::$objects[$selector]->get ();
20
21
        if ($object === null)
22
            self::remove ($selector);
23
24
        return $object;
25
    }
26
27
    public static function exists (int $selector): bool
28
    {
29
        return isset (self::$objects[$selector]);
30
    }
31
32
    public static function getObjects (): array
33
    {
34
        return self::$objects;
35
    }
36
37
    public static function remove (int $selector): void
38
    {
39
        unset (self::$objects[$selector]);
40
    }
41
42
    public static function clean (): void
43
    {
44
        foreach (self::$objects as $selector => $reference)
45
            if ($reference->get () === null)
46
                unset (self::$objects[$selector]);
47
    }
48
}
49
50
function _c (int $selector): ?NetObject
51
{
52
    return Components::get ($selector);
53
}
54
55
// TODO: поддержка многоуровневых ссылок вида родитель->родитель->объект
56
function c (string $name): ?NetObject
57
{
58
    if (($object = _c($name)) !== null)
0 ignored issues
show
Bug introduced by
$name of type string is incompatible with the type integer expected by parameter $selector of VoidEngine\_c(). ( Ignorable by Annotation )

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

58
    if (($object = _c(/** @scrutinizer ignore-type */ $name)) !== null)
Loading history...
59
        return $object;
60
61
    foreach (Components::getObjects () as $selector => $reference)
62
    {
63
        $object = $reference->get ();
64
65
        if ($object === null)
66
            continue;
67
68
        try
69
        {
70
            if ($object->name == $name)
71
                return $object;
72
        }
73
74
        catch (\WinformsException $e) {}
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Unused Code introduced by
catch (\WinformsException $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...
75
    }
76
77
    return null;
78
}
79