Doubler::createTestDouble()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Zalas\PHPUnit\Doubles\TestCase;
5
6
use Zalas\PHPUnit\Doubles\Extractor\Extractor;
7
use Zalas\PHPUnit\Doubles\Extractor\Property;
8
use Zalas\PHPUnit\Doubles\Injector\Injector;
9
10
/**
11
 * @internal
12
 */
13
class Doubler
14
{
15
    /**
16
     * @var Extractor
17
     */
18
    private $extractor;
19
20
    /**
21
     * @var Injector
22
     */
23
    private $injector;
24
25
    /**
26
     * @var callable
27
     */
28
    private $doubleFactory;
29
30
    /**
31
     * @var string
32
     */
33
    private $doubleType;
34
35 18
    public function __construct(Extractor $extractor, Injector $injector, callable $doubleFactory, string $doubleType)
36
    {
37 18
        $this->extractor = $extractor;
38 18
        $this->injector = $injector;
39 18
        $this->doubleFactory = $doubleFactory;
40 18
        $this->doubleType = $doubleType;
41
    }
42
43 18
    public function createDoubles(/*object */$testCase)
44
    {
45 18
        foreach ($this->getTestDoubleProperties($testCase) as $property) {
46 18
            $this->injector->inject($testCase, $property->getName(), $this->createTestDouble($property));
47
        }
48
    }
49
50 18
    private function createTestDouble(Property $property)
51
    {
52 18
        return ($this->doubleFactory)($property->getTypesFiltered(function (string $type): bool {
53
            return $type !== $this->doubleType;
54 18
        }));
55
    }
56
57
    /**
58
     * @param object $testCase
59
     *
60
     * @return Property[]
61
     */
62 18
    private function getTestDoubleProperties(/*object */$testCase): array
63
    {
64 18
        return $this->extractor->extract($testCase, function (Property $property): bool {
65
            $doubleTypes = $property->getTypesFiltered(function (string $type): bool {
66
                return $type === $this->doubleType;
67
            });
68
69
            return \count($doubleTypes) > 0;
70 18
        });
71
    }
72
}
73