Passed
Push — apcu ( 2b2a42...345b67 )
by Akihito
01:42
created

NewInstance   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 27
c 2
b 0
f 0
dl 0
loc 73
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 24 5
A progress() 0 12 2
A getCompiled() 0 3 1
A getFailed() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Compiler;
6
7
use Ray\Di\Exception\Unbound;
8
use Ray\Di\InjectorInterface;
9
use Throwable;
10
11
use function assert;
12
use function class_exists;
13
use function count;
14
use function get_class;
15
use function in_array;
16
use function interface_exists;
17
use function printf;
18
use function sprintf;
19
20
use const PHP_EOL;
21
22
final class NewInstance
23
{
24
    /** @var list<string> */
0 ignored issues
show
Bug introduced by
The type BEAR\Package\Compiler\list 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...
25
    private $compiled = [];
26
27
    /** @var array<string, string> */
28
    private $failed = [];
29
30
    /** @var InjectorInterface */
31
    private $injector;
32
33
    public function __construct(InjectorInterface $injector)
34
    {
35
        $this->injector = $injector;
36
    }
37
38
    /**
39
     * @param ''|class-string $interface
0 ignored issues
show
Documentation Bug introduced by
The doc comment ''|class-string at position 0 could not be parsed: Unknown type name '''' at position 0 in ''|class-string.
Loading history...
40
     */
41
    public function __invoke(string $interface, string $name = ''): void
42
    {
43
        $dependencyIndex = $interface . '-' . $name;
44
        if (in_array($dependencyIndex, $this->compiled, true)) {
0 ignored issues
show
Bug introduced by
$this->compiled of type BEAR\Package\Compiler\list is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

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

44
        if (in_array($dependencyIndex, /** @scrutinizer ignore-type */ $this->compiled, true)) {
Loading history...
45
            // @codeCoverageIgnoreStart
46
            printf("S %s:%s\n", $interface, $name);
47
            // @codeCoverageIgnoreEnd
48
        }
49
50
        try {
51
            $this->injector->getInstance($interface, $name);
52
            $this->compiled[] = $dependencyIndex;
53
            $this->progress('.');
54
        } catch (Unbound $e) {
55
            if ($dependencyIndex === 'Ray\Aop\MethodInvocation-') {
56
                return;
57
            }
58
59
            $this->failed[$dependencyIndex] = $e->getMessage();
60
            $this->progress('F');
61
            // @codeCoverageIgnoreStart
62
        } catch (Throwable $e) {
63
            $this->failed[$dependencyIndex] = sprintf('%s: %s', get_class($e), $e->getMessage());
64
            $this->progress('F');
65
            // @codeCoverageIgnoreEnd
66
        }
67
    }
68
69
    private function progress(string $char): void
70
    {
71
        /**
72
         * @var int
73
         */
74
        static $cnt = 0;
75
76
        echo $char;
77
        $cnt++;
78
        if ($cnt === 60) {
79
            $cnt = 0;
80
            echo PHP_EOL;
81
        }
82
    }
83
84
    public function getCompiled(): int
85
    {
86
        return count($this->compiled);
87
    }
88
89
    /**
90
     * @return array<string, string>
91
     */
92
    public function getFailed(): array
93
    {
94
        return $this->failed;
95
    }
96
}
97