CreateWithNonPublicConstructor   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 11
dl 0
loc 32
c 0
b 0
f 0
rs 10
ccs 13
cts 13
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 14 1
1
<?php
2
/**
3
 * File was created 17.05.2016 06:09
4
 */
5
6
namespace PeekAndPoke\Component\Creator;
7
8
9
/**
10
 * Creator for instantiating classes with a private/protected constructor.
11
 *
12
 * The given class MUST have a constructor and the constructor MUST have zero required parameters.
13
 *
14
 * @author Karsten J. Gerber <[email protected]>
15
 */
16
class CreateWithNonPublicConstructor extends AbstractCreator
17
{
18
    /** @var string */
19
    private $ctorScopeFqcn;
20
21
    /**
22
     * @param \ReflectionClass $class
23
     */
24 2
    public function __construct(\ReflectionClass $class)
25
    {
26 2
        parent::__construct($class);
27
28 2
        $this->ctorScopeFqcn = $class->getConstructor()->getDeclaringClass()->getName();
29 2
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34 1
    public function create($data = null)
35
    {
36 1
        $fqcn = $this->fqcn;
37
38
        // we call the private/protected constructor with the scope of the class that is defining it
39 1
        $closure = \Closure::bind(
40 1
            function () use ($fqcn) {
41 1
                return new $fqcn();
42 1
            },
43 1
            null,
44 1
            $this->ctorScopeFqcn
45
        );
46
47 1
        return $closure();
48
    }
49
}
50