Completed
Push — master ( 2f25cf...59cc7f )
by Marco
43:54 queued 18:55
created

BindProxyProperties::__construct()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 56
c 0
b 0
f 0
ccs 27
cts 27
cp 1
rs 8.6488
cc 5
nc 5
nop 3
crap 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator;
6
7
use ProxyManager\Exception\UnsupportedProxiedClassException;
8
use ProxyManager\Generator\MethodGenerator;
9
use ProxyManager\ProxyGenerator\Util\Properties;
10
use ReflectionClass;
11
use Zend\Code\Generator\ParameterGenerator;
12
use Zend\Code\Generator\PropertyGenerator;
13
use function implode;
14
use function var_export;
15
16
/**
17
 * The `bindProxyProperties` method implementation for access interceptor scope localizers
18
 */
19
class BindProxyProperties extends MethodGenerator
20
{
21
    /**
22
     * Constructor
23
     */
24
    public function __construct(
25 4
        ReflectionClass $originalClass,
26
        PropertyGenerator $prefixInterceptors,
27
        PropertyGenerator $suffixInterceptors
28
    ) {
29
        parent::__construct(
30 4
            'bindProxyProperties',
31 4
            [
32
                new ParameterGenerator('localizedObject', $originalClass->getName()),
33 4
                new ParameterGenerator('prefixInterceptors', 'array', []),
34 4
                new ParameterGenerator('suffixInterceptors', 'array', []),
35 4
            ],
36
            static::FLAG_PRIVATE,
37 4
            null,
38 4
            "@override constructor to setup interceptors\n\n"
39
            . '@param \\' . $originalClass->getName() . " \$localizedObject\n"
40 4
            . "@param \\Closure[] \$prefixInterceptors method interceptors to be used before method logic\n"
41 4
            . '@param \\Closure[] $suffixInterceptors method interceptors to be used before method logic'
42 4
        );
43
44
        $localizedProperties        = [];
45 4
        $properties                 = Properties::fromReflectionClass($originalClass);
46
        $nonReferenceableProperties = $properties
47 4
            ->onlyNonReferenceableProperties()
48
            ->onlyInstanceProperties();
49 4
50 3
        if (! $nonReferenceableProperties->empty()) {
51
            throw UnsupportedProxiedClassException::nonReferenceableLocalizedReflectionProperties(
52 3
                $originalClass,
53
                $nonReferenceableProperties
54
            );
55 4
        }
56 2
57
        $propertiesThatCanBeReferenced = $properties->onlyPropertiesThatCanBeUnset();
58 2
59 2
        foreach ($propertiesThatCanBeReferenced->getAccessibleProperties() as $property) {
60 2
            $propertyName = $property->getName();
61 2
62
            $localizedProperties[] = '$this->' . $propertyName . ' = & $localizedObject->' . $propertyName . ';';
63
        }
64 4
65 4
        foreach ($propertiesThatCanBeReferenced->getPrivateProperties() as $property) {
66 4
            $propertyName = $property->getName();
67 4
68
            $localizedProperties[] = "\\Closure::bind(function () use (\$localizedObject) {\n    "
69 4
                . '$this->' . $propertyName . ' = & $localizedObject->' . $propertyName . ";\n"
70
                . '}, $this, ' . var_export($property->getDeclaringClass()->getName(), true)
71
                . ')->__invoke();';
72
        }
73
74
        $this->setBody(
75
            ($localizedProperties ? implode("\n\n", $localizedProperties) . "\n\n" : '')
76
            . '$this->' . $prefixInterceptors->getName() . " = \$prefixInterceptors;\n"
77
            . '$this->' . $suffixInterceptors->getName() . ' = $suffixInterceptors;'
78
        );
79
    }
80
}
81