ClassCreator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 7
dl 0
loc 17
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromClass() 0 9 3
1
<?php
2
3
namespace evelikto\di\creator;
4
use evelikto\di\Bean;
5
6
/** Creates dependency if identifier is a classname */
7
trait ClassCreator
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           New $name instance or null if no such class
14
     */
15
    protected function fromClass(string $name) {
16
        if (class_exists($name, true) === false)
17
            return null;
18
19
        if (method_exists($name, '__construct') === false)
20
            return new Bean($name, new $name);
21
22
        $method = new \ReflectionMethod($name, '__construct');
23
        return new Bean($name, 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

23
        return new Bean($name, new $name(...$this->/** @scrutinizer ignore-call */ autowire($method)));
Loading history...
Bug introduced by
$name of type object is incompatible with the type string expected by parameter $name of evelikto\di\Bean::__construct(). ( Ignorable by Annotation )

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

23
        return new Bean(/** @scrutinizer ignore-type */ $name, new $name(...$this->autowire($method)));
Loading history...
24
    }
25
}