NullObjectGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 7
dl 0
loc 37
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 28 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\ProxyGenerator;
6
7
use Laminas\Code\Generator\ClassGenerator;
8
use Laminas\Code\Generator\Exception\InvalidArgumentException;
9
use Laminas\Code\Reflection\MethodReflection;
10
use ProxyManager\Exception\InvalidProxiedClassException;
11
use ProxyManager\Generator\Util\ClassGeneratorUtils;
12
use ProxyManager\Proxy\NullObjectInterface;
13
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion;
14
use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\NullObjectMethodInterceptor;
15
use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\StaticProxyConstructor;
16
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
17
use ReflectionClass;
18
19
/**
20
 * Generator for proxies implementing {@see \ProxyManager\Proxy\NullObjectInterface}
21
 *
22
 * {@inheritDoc}
23
 */
24
class NullObjectGenerator implements ProxyGeneratorInterface
25
{
26
    /**
27
     * {@inheritDoc}
28
     *
29
     * @throws InvalidProxiedClassException
30
     * @throws InvalidArgumentException
31
     */
32
    public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) : void
33 5
    {
34
        CanProxyAssertion::assertClassCanBeProxied($originalClass);
35 5
36
        $interfaces = [NullObjectInterface::class];
37 5
38
        if ($originalClass->isInterface()) {
39 5
            $interfaces[] = $originalClass->getName();
40 1
        } else {
41
            $classGenerator->setExtendedClass($originalClass->getName());
42 4
        }
43
44
        $classGenerator->setImplementedInterfaces($interfaces);
45 5
46
        foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass, []) as $method) {
47 5
            $classGenerator->addMethodFromGenerator(
48 4
                NullObjectMethodInterceptor::generateMethod(
49 4
                    new MethodReflection($method->getDeclaringClass()->getName(), $method->getName())
50 4
                )
51
            );
52
        }
53
54
        ClassGeneratorUtils::addMethodIfNotFinal(
55 5
            $originalClass,
56 5
            $classGenerator,
57 5
            new StaticProxyConstructor($originalClass)
58 5
        );
59
    }
60
}
61