NewInstance::getFailed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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 count;
13
use function in_array;
14
use function is_int;
15
use function printf;
16
use function sprintf;
17
18
use const PHP_EOL;
19
20
final class NewInstance
21
{
22
    /** @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...
23
    private array $compiled = [];
24
25
    /** @var array<string, string> */
26
    private array $failed = [];
27
28
    public function __construct(
29
        private InjectorInterface $injector,
30
    ) {
31
    }
32
33
    /** @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...
34
    public function __invoke(string $interface, string $name = ''): void
35
    {
36
        $dependencyIndex = $interface . '-' . $name;
37
        if (in_array($dependencyIndex, $this->compiled, true)) {
38
            // @codeCoverageIgnoreStart
39
            printf("S %s:%s\n", $interface, $name);
40
            // @codeCoverageIgnoreEnd
41
        }
42
43
        try {
44
            $this->injector->getInstance($interface, $name);
45
            $this->compiled[] = $dependencyIndex;
46
            $this->progress('.');
47
        } catch (Unbound $e) {
48
            if ($dependencyIndex === 'Ray\Aop\MethodInvocation-') {
49
                return;
50
            }
51
52
            $this->failed[$dependencyIndex] = $e->getMessage();
53
            $this->progress('F');
54
            // @codeCoverageIgnoreStart
55
        } catch (Throwable $e) {
56
            $this->failed[$dependencyIndex] = sprintf('%s: %s', $e::class, $e->getMessage());
57
            $this->progress('F');
58
            // @codeCoverageIgnoreEnd
59
        }
60
    }
61
62
    private function progress(string $char): void
63
    {
64
        static $cnt = 0;
65
66
        echo $char;
67
        assert(is_int($cnt));
68
        $cnt++;
69
        if ($cnt !== 60) {
70
            return;
71
        }
72
73
        $cnt = 0;
74
        echo PHP_EOL;
75
    }
76
77
    public function getCompiled(): int
78
    {
79
        return count($this->compiled);
80
    }
81
82
    /** @return array<string, string> */
83
    public function getFailed(): array
84
    {
85
        return $this->failed;
86
    }
87
}
88