MagicClone   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 2
dl 0
loc 42
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 24 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator;
6
7
use Laminas\Code\Generator\PropertyGenerator;
8
use ProxyManager\Generator\MagicMethodGenerator;
9
use ReflectionClass;
10
use function array_keys;
11
use function str_replace;
12
13
/**
14
 * Magic `__clone` for lazy loading value holder objects
15
 */
16
class MagicClone extends MagicMethodGenerator
17
{
18
    private const TEMPLATE = <<<'PHP'
19
$this->{{$valueHolder}} = clone $this->{{$valueHolder}};
20
21
foreach ($this->{{$prefix}} as $key => $value) {
22
    $this->{{$prefix}}[$key] = clone $value;
23
}
24
25
foreach ($this->{{$suffix}} as $key => $value) {
26
    $this->{{$suffix}}[$key] = clone $value;
27
}
28
PHP;
29
30
    /**
31
     * Constructor
32
     */
33
    public function __construct(
34
        ReflectionClass $originalClass,
35 1
        PropertyGenerator $valueHolderProperty,
36
        PropertyGenerator $prefixInterceptors,
37
        PropertyGenerator $suffixInterceptors
38
    ) {
39
        parent::__construct($originalClass, '__clone');
40
41 1
        $valueHolder = $valueHolderProperty->getName();
42
        $prefix      = $prefixInterceptors->getName();
43 1
        $suffix      = $suffixInterceptors->getName();
44 1
45 1
        $replacements = [
46
            '{{$valueHolder}}' => $valueHolder,
47
            '{{$prefix}}' => $prefix,
48 1
            '{{$suffix}}' => $suffix,
49 1
        ];
50 1
51
        $this->setBody(str_replace(
52
            array_keys($replacements),
53 1
            $replacements,
54 1
            self::TEMPLATE
55 1
        ));
56 1
    }
57
}
58