Completed
Push — 1.x ( c86fd3...4b8bd5 )
by Akihito
14s queued 12s
created

NewInstance::__invoke()   A

Complexity

Conditions 5
Paths 20

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 24
rs 9.4555
cc 5
nc 20
nop 2
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 count;
12
use function get_class;
13
use function in_array;
14
use function printf;
15
use function sprintf;
16
17
use const PHP_EOL;
18
19
final class NewInstance
20
{
21
    /** @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...
22
    private $compiled = [];
23
24
    /** @var array<string, string> */
25
    private $failed = [];
26
27
    /** @var InjectorInterface */
28
    private $injector;
29
30
    public function __construct(InjectorInterface $injector)
31
    {
32
        $this->injector = $injector;
33
    }
34
35
    public function __invoke(string $interface, string $name = ''): void
36
    {
37
        $dependencyIndex = $interface . '-' . $name;
38
        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

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