InterfaceMethodCreator::fromInterfaceMethod()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace evelikto\di\creator;
4
5
/** Creates dependency if:
6
 * 1. $name is an interface
7
 * 2. Interface method factory is defined in the config class
8
 */
9
trait InterfaceMethodCreator
10
{
11
    /**
12
     * Resolves dependency from an interface factory method. Factory method name is formed from the
13
     * interface name through converting it to a valid camel-case identifier. For example,
14
     * to create \Monolog\Logger for \Psr\Log\LoggerInterface dependency use this:
15
     *
16
     * public function psrLogLoggerInterface() {
17
     *     return new \Monolog\Logger('channelname');
18
     * }
19
     *
20
     * @param   string      $name   Name of the interface to be created
21
     * @return  Bean|null           New $name instance or null if no such class
0 ignored issues
show
Bug introduced by
The type evelikto\di\creator\Bean 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...
22
     * @throws  NotTheRequestedInterface if the created instance is not a $name
23
     */
24
    protected function fromInterfaceMethod(string $name) {
25
        if (interface_exists($name, true) === false)
26
            return null;
27
28
        $bean = $this->fromMethod($this->interfaceToFactory($name));
0 ignored issues
show
Bug introduced by
It seems like fromMethod() 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

28
        /** @scrutinizer ignore-call */ 
29
        $bean = $this->fromMethod($this->interfaceToFactory($name));
Loading history...
29
        if ($bean === null)
30
            return null;
31
32
        if (is_subclass_of($bean->value, $name) === false)
33
            throw new NotTheRequestedInterface(get_class($bean->value), $name);
34
35
        $bean->addAlias($name);
36
        return $bean;
37
    }
38
39
    /**
40
     * Converts FQIN to a valid camel-case method name:
41
     *   \Psr\Log\LoggerInterface => psrLogLoggerInterface
42
     */
43
    private function interfaceToFactory(string $name) : string {
44
        return lcfirst(str_replace("\\", '', ucwords($name, "\\")));
45
    }
46
}