Completed
Push — master ( 969ba5...87cf68 )
by Przemysław eRIZ
02:24
created

RecursiveGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 31
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addClassMapping() 0 3 1
A generateForProperty() 0 14 3
A __construct() 0 3 1
1
<?php
2
3
namespace Er1z\FakeMock\Generator;
4
5
use Er1z\FakeMock\FakeMock;
6
use Er1z\FakeMock\Generator\RecursiveGenerator\ClassMapper;
7
use Er1z\FakeMock\Generator\RecursiveGenerator\ClassMapperInterface;
8
use Er1z\FakeMock\Metadata\FieldMetadata;
9
10
class RecursiveGenerator implements GeneratorInterface
11
{
12
    /**
13
     * @var ClassMapperInterface
14
     */
15
    protected $classMapper;
16
17
    public function __construct(?ClassMapperInterface $classMapper = null)
18
    {
19
        $this->classMapper = $classMapper ?? new ClassMapper();
20
    }
21
22
    public function generateForProperty(FieldMetadata $field, FakeMock $fakemock)
23
    {
24
        if (!$field->configuration->recursive) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $field->configuration->recursive of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
25
            return null;
26
        }
27
28
        $value = $this->classMapper->getObjectForField($field);
29
        if (!$value) {
30
            return null;
31
        }
32
33
        $value = $fakemock->fill($value);
34
35
        return $value;
36
    }
37
38
    public function addClassMapping(string $intefaceOrAbstractFqcn, string $targetFqcn)
39
    {
40
        $this->classMapper->addClassMapping($intefaceOrAbstractFqcn, $targetFqcn);
41
    }
42
}
43