Completed
Pull Request — 1.x (#352)
by Akihito
01:26
created

NewInstance::getCompiled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 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> */
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)) {
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>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
85
     */
86
    public function getFailed(): array
87
    {
88
        return $this->failed;
89
    }
90
}
91