MagicSet   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 6
dl 0
loc 47
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 39 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator;
6
7
use InvalidArgumentException;
8
use Laminas\Code\Generator\ParameterGenerator;
9
use Laminas\Code\Generator\PropertyGenerator;
10
use ProxyManager\Generator\MagicMethodGenerator;
11
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\Util\InterceptorGenerator;
12
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
13
use ProxyManager\ProxyGenerator\Util\GetMethodIfExists;
14
use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator;
15
use ReflectionClass;
16
17
/**
18
 * Magic `__set` for method interceptor value holder objects
19
 */
20
class MagicSet extends MagicMethodGenerator
21
{
22
    /**
23
     * Constructor
24
     *
25
     * @throws InvalidArgumentException
26
     */
27
    public function __construct(
28
        ReflectionClass $originalClass,
29 1
        PropertyGenerator $valueHolder,
30
        PropertyGenerator $prefixInterceptors,
31
        PropertyGenerator $suffixInterceptors,
32
        PublicPropertiesMap $publicProperties
33
    ) {
34
        parent::__construct(
35
            $originalClass,
36 1
            '__set',
37 1
            [new ParameterGenerator('name'), new ParameterGenerator('value')]
38 1
        );
39 1
40
        $parent          = GetMethodIfExists::get($originalClass, '__set');
41
        $valueHolderName = $valueHolder->getName();
42 1
43 1
        $callParent = PublicScopeSimulator::getPublicAccessSimulationCode(
44
            PublicScopeSimulator::OPERATION_SET,
45 1
            'name',
46 1
            'value',
47 1
            $valueHolder,
48 1
            'returnValue'
49 1
        );
50 1
51
        if (! $publicProperties->isEmpty()) {
52
            $callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n"
53 1
                . '    $returnValue = ($this->' . $valueHolderName . '->$name = $value);'
54 1
                . "\n} else {\n    " . $callParent . "\n}\n\n";
55 1
        }
56 1
57
        $this->setBody(InterceptorGenerator::createInterceptedMethodBody(
58
            $callParent,
59 1
            $this,
60 1
            $valueHolder,
61 1
            $prefixInterceptors,
62 1
            $suffixInterceptors,
63 1
            $parent
64 1
        ));
65 1
    }
66
}
67