Passed
Push — apcu ( 494dbb...18abe3 )
by Akihito
01:32
created

NewInstance::progress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 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
    public function __invoke(string $interface, string $name = ''): void
39
    {
40
        $dependencyIndex = $interface . '-' . $name;
41
        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

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