InjectableCreator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 11
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromClass() 0 15 4
1
<?php
2
3
namespace evelikto\di\creator;
4
use evelikto\di\{Bean, Injectable};
5
6
/** Creates dependency if identifier is a classname implementing the Injectable interface */
7
trait InjectableCreator
8
{
9
    /**
10
     * Resolves dependency from a given class name. Constructor injection will be used if needed.
11
     *
12
     * @param   string      $name   Name of the class to be created
13
     * @return  Bean|null           Wrapped new $name instance or null if no such class
14
     * @throws  ClassNotInjectable  If $name is not an Injectable
15
     */
16
    protected function fromClass(string $name) {
17
        if (class_exists($name, true) === false)
18
            return null;
19
20
        if (method_exists($name, '__construct') === false)
21
            $value = new $name;
22
        else {
23
            $method = new \ReflectionMethod($name, '__construct');
24
            $value = new $name(...$this->autowire($method));
0 ignored issues
show
Bug introduced by
It seems like autowire() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
            $value = new $name(...$this->/** @scrutinizer ignore-call */ autowire($method));
Loading history...
25
        }
26
27
        if ($value instanceof Injectable)
28
            return new Bean($name, $value);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type object; however, parameter $name of evelikto\di\Bean::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
            return new Bean(/** @scrutinizer ignore-type */ $name, $value);
Loading history...
29
30
        throw new ClassNotInjectable($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type object; however, parameter $name of evelikto\di\creator\Clas...jectable::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        throw new ClassNotInjectable(/** @scrutinizer ignore-type */ $name);
Loading history...
31
    }
32
}