Passed
Pull Request — master (#127)
by Wouter
02:39
created

ValueReplacingGenerator::generate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 15
rs 10
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Zenstruck\Foundry\Proxy;
4
5
use Laminas\Code\Generator\ClassGenerator;
6
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\PropertyGenerator\ValueHolderProperty;
7
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
8
use ReflectionClass;
9
10
/**
11
 * A generator decorator to add {@see SetValueHolderMethod}.
12
 *
13
 * @author Wouter de Jong <[email protected]>
14
 */
15
class ValueReplacingGenerator implements ProxyGeneratorInterface
16
{
17
    /** @var ProxyGeneratorInterface */
18
    private $generator;
19
20
    public function __construct(ProxyGeneratorInterface $generator)
21
    {
22
        $this->generator = $generator;
23
    }
24
25
    public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator): void
26
    {
27
        $this->generator->generate($originalClass, $classGenerator);
28
29
        $valueHolderProperty = null;
30
        foreach ($classGenerator->getProperties() as $property) {
31
            if ($property instanceof ValueHolderProperty) {
32
                $valueHolderProperty = $property;
33
34
                break;
35
            }
36
        }
37
        \assert($valueHolderProperty instanceof ValueHolderProperty);
38
39
        $classGenerator->addMethodFromGenerator(new SetValueHolderMethod($valueHolderProperty));
40
    }
41
}
42