ProxyDIFactory::autoloader()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 0
loc 29
ccs 8
cts 8
cp 1
crap 2
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\Proxy;
4
5
use Psr\Container\ContainerInterface;
6
7
/**
8
 * Create a proxy application for the $di container. This class initiates the
9
 * the base proxy class ProxyDI and creates an autoloader which creates new
10
 * instanses of proxy classes. These instances are the actual implementation
11
 * that enables the access to the corresponding $di service.
12
 */
13
class ProxyDIFactory
14
{
15
    /**
16
     * Init the poxy base class and add an autoloader which can create new
17
     * instances of specific proxy service classes that maps to a $di service.
18
     *
19
     * @param DIInterface $di The service container holding framework
0 ignored issues
show
Bug introduced by
The type Anax\Proxy\DIInterface 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...
20
     *                        services.
21
     *
22
     * @return void.
0 ignored issues
show
Documentation Bug introduced by
The doc comment void. at position 0 could not be parsed: Unknown type name 'void.' at position 0 in void..
Loading history...
23
     *
24
     * @SuppressWarnings(PHPMD.StaticAccess)
25
     */
26 4
    public static function init(ContainerInterface $di) : void
27
    {
28 4
        ProxyDI::setDI($di);
29 4
        spl_autoload_register(__CLASS__ . "::autoloader");
30 4
    }
31
32
33
34
    /**
35
     * Autoloader for Proxy\DI realtime static proxy access to $di.
36
     *
37
     * @param string $class The name of the class to create, the
38
     *                      classname must be a direct subclass of
39
     *                      \Anax\Proxy\DI\.
40
     *
41
     * @return void
42
     *
43
     * @SuppressWarnings(PHPMD.EvalExpression)
44
     */
45 3
    public static function autoloader($class) : void
46
    {
47 3
        $prefix = "Anax\\Proxy\\DI";
48
49
        // Check if it is a class below namespace $prefix
50 3
        if (strncmp($prefix, $class, strlen($prefix))) {
51 1
            return;
52
        }
53
54
        // Get the classname after the $prefix
55 2
        $relativeClass = substr($class, strlen($prefix) + 1);
56
57
        $classDefinition = <<< EOD
58
namespace Anax\Proxy\DI;
59
60
use Anax\Proxy\ProxyDI;
61
use Anax\Proxy\ProxyInterface;
62
63 2
class $relativeClass extends ProxyDI implements ProxyInterface
64
{
65
    public static function getServiceName()
66
    {
67 2
        return lcfirst("$relativeClass");
68
    }
69
}
70
71
EOD;
72
73 2
        eval($classDefinition);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
74 2
    }
75
}
76