StaticProxyConstructor   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\ProxyGenerator\NullObject\MethodGenerator;
6
7
use Laminas\Code\Generator\Exception\InvalidArgumentException;
8
use ProxyManager\Generator\MethodGenerator;
9
use ProxyManager\ProxyGenerator\Util\Properties;
10
use ReflectionClass;
11
use ReflectionProperty;
12
use function array_map;
13
use function implode;
14
15
/**
16
 * The `staticProxyConstructor` implementation for null object proxies
17
 */
18
class StaticProxyConstructor extends MethodGenerator
19
{
20
    /**
21
     * Constructor
22
     *
23
     * @param ReflectionClass $originalClass Reflection of the class to proxy
24
     *
25
     * @throws InvalidArgumentException
26
     */
27
    public function __construct(ReflectionClass $originalClass)
28 2
    {
29
        parent::__construct('staticProxyConstructor', [], self::FLAG_PUBLIC | self::FLAG_STATIC);
30 2
31
        $nullableProperties = array_map(
32 2
            static function (ReflectionProperty $publicProperty) : string {
33
                return '$instance->' . $publicProperty->getName() . ' = null;';
34 1
            },
35 2
            Properties::fromReflectionClass($originalClass)
36 2
                ->onlyNullableProperties()
37
                ->getPublicProperties()
38
        );
39 2
40 2
        $this->setReturnType($originalClass->getName());
41 2
        $this->setDocBlock('Constructor for null object initialization');
42
        $this->setBody(
43
            'static $reflection;' . "\n\n"
44
            . '$reflection = $reflection ?? new \ReflectionClass(__CLASS__);' . "\n"
45 2
            . '$instance   = $reflection->newInstanceWithoutConstructor();' . "\n\n"
46 2
            . ($nullableProperties ? implode("\n", $nullableProperties) . "\n\n" : '')
47
            . 'return $instance;'
48 2
        );
49
    }
50
}
51