InterfaceAliasCreator::fromInterfaceAlias()   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 alias constant is defined in the config class
8
 * 3. Alias is a constructable class
9
 */
10
trait InterfaceAliasCreator
11
{
12
    /**
13
     * Resolves dependency from an interface alias. Alias is a class constant in the config class.
14
     * Alias is formed by replacing backslashes in the interface name with underlines and converting
15
     * it to uppercase. Value of the alias is a class name. For example to create \Monolog\Logger
16
     * for a \Psr\Log\LoggerInterface dependency use this:
17
     *
18
     * const PSR_LOG_LOGGERINTERFACE = '\Monolog\Logger';
19
     *
20
     * @param   string      $name         Name of the interface to be created
21
     * @return  Bean|null                 Wrapped 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 fromInterfaceAlias(string $name) {
25
        if (interface_exists($name, true) === false)
26
            return null;
27
28
        $bean = $this->fromClass($this->fromConst($this->interfaceToAlias($name)));
0 ignored issues
show
Bug introduced by
It seems like fromConst() 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
        $bean = $this->fromClass($this->/** @scrutinizer ignore-call */ fromConst($this->interfaceToAlias($name)));
Loading history...
Bug introduced by
It seems like fromClass() 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->fromClass($this->fromConst($this->interfaceToAlias($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 const name:
41
     * Psr\Log\LoggerInterface => PSR_LOG_LOGGERINTERFACE
42
     */
43
    private function interfaceToAlias(string $name) : string {
44
        return strtoupper(str_replace("\\", '_', $name));
45
    }
46
}