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

PropertyAccessInjector::inject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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