ProxyAccessor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 68
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A loadClass() 0 9 2
A evalClass() 0 10 2
A getCanonicalClassName() 0 4 1
A load() 0 13 2
1
<?php
2
3
namespace PHPRealCoverage\Proxy;
4
5
class ProxyAccessor
6
{
7
    /**
8
     * @var string
9
     */
10
    private $proxyName;
11
12
    /**
13
     * @var ClassMetadata
14
     */
15
    private $classmetadata;
16
17
    /**
18
     * @var string
19
     */
20
    private $originalClassName;
21
22
    public function __construct(ClassMetadata $classMetadata)
23
    {
24
        $this->originalClassName = $classMetadata->getName();
25
        $this->proxyName = "\\" . $classMetadata->getNamespace() . "\\" . $this->originalClassName;
26
        $this->classmetadata = $classMetadata;
27
    }
28
29
    public function loadClass(ClassMetadata $class2)
30
    {
31
        $proxy = $this->proxyName;
32
        if (!$this->evalClass($class2)) {
33
            return false;
34
        }
35
        $proxy::setInstanceClass($this->getCanonicalClassName($class2));
36
        return true;
37
    }
38
39
    private function evalClass(ClassMetadata $class)
40
    {
41
        $class->setName($class->getName() . '__PROXY__');
42
        while (class_exists($this->getCanonicalClassName($class))) {
43
            $class->setName($class->getName() . mt_rand(0, 999));
44
        }
45
46
        $result = @eval((string)$class) !== false;
47
        return $result;
48
    }
49
50
    /**
51
     * @param ClassMetadata $class2
52
     * @return string
53
     */
54
    public function getCanonicalClassName(ClassMetadata $class2)
55
    {
56
        return "\\" . trim($class2->getNamespace(), "\\") . "\\" . $class2->getName();
57
    }
58
59
    public function load()
60
    {
61
        $this->evalClass($this->classmetadata);
62
63
        $builder = new ProxyBuilder();
64
        $builder->setNamespace($this->classmetadata->getNamespace());
65
        $builder->setClassName($this->originalClassName);
66
        $builder->setParentClass("\\" . $this->classmetadata->getNamespace() . "\\" . $this->classmetadata->getName());
67
        foreach ($this->classmetadata->getMethods() as $method) {
68
            $builder->addMethod($method);
69
        }
70
        $builder->loadProxy();
0 ignored issues
show
Unused Code introduced by
The call to the method PHPRealCoverage\Proxy\ProxyBuilder::loadProxy() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
71
    }
72
}
73