DefinitionGenerator::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cekta\DI;
6
7
use Cekta\DI\Strategy\Definition\Factory;
8
9
class DefinitionGenerator
10
{
11
    /**
12
     * @var Reflection
13
     */
14
    private $reflection;
15
16
    public function __construct(Reflection $reflection)
17
    {
18
        $this->reflection = $reflection;
19
    }
20
21
    public function __invoke(string ...$classes): string
22
    {
23
        $factory = Factory::class;
24
        $body = '';
25
        foreach ($classes as $class) {
26
            if ($this->reflection->isInstantiable($class)) {
27
                $dependencies = var_export($this->reflection->getDependencies($class), true);
28
                $body .= PHP_EOL . "'{$class}' => new $factory('$class', ...$dependencies)," . PHP_EOL;
29
            }
30
        }
31
        return "<?php
32
33
declare(strict_types=1);
34
35
return [{$body}];";
36
    }
37
}
38