Passed
Push — master ( 1ddfa9...ac520b )
by Jakub
01:32
created

PropertyAccessInjector   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 26
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createInjector() 0 9 3
A inject() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Zalas\PHPUnit\Doubles\Injector;
5
6
final class PropertyAccessInjector implements Injector
7
{
8
    /**
9
     * @var string
10
     */
11
    private $classContext;
12
13 24
    public function __construct(?string $classContext = null)
14
    {
15 24
        $this->classContext = $classContext;
16
    }
17
18 22
    public function inject(/*object */$target, string $property, /*object */$object): void
19
    {
20 22
        $this->createInjector($target)($property, $object);
21
    }
22
23 22
    private function createInjector(/*object */$target): \Closure
24
    {
25
        $injector = function (string $property, /*object */$object): void {
26 22
            if (\property_exists($this, $property) && null === $this->$property) {
27 20
                $this->$property = $object;
28
            }
29 22
        };
30
31 22
        return $injector->bindTo($target, $this->classContext ?? $target);
32
    }
33
}
34