Base::fake()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 5
c 3
b 0
f 0
dl 0
loc 11
rs 9.6111
cc 5
nc 4
nop 2
1
<?php
2
3
namespace Knp\FriendlyContexts\Faker\Provider;
4
5
use Faker\Provider\Base as FakerProvider;
6
7
abstract class Base extends FakerProvider
8
{
9
    protected $parent;
10
11
    public function initialise()
12
    {
13
        foreach ($this->generator->getProviders() as $provider) {
14
            if ($this->supportsParent($provider)) {
15
                $this->setParent($provider);
16
            }
17
        }
18
    }
19
20
    public function getParent()
21
    {
22
        return $this->parent;
23
    }
24
25
    public function setParent(FakerProvider $parent)
26
    {
27
        $this->parent = $parent;
28
29
        return $this;
30
    }
31
32
    public function supportsParent($parent)
33
    {
34
        return false;
35
    }
36
37
    public function isFakable($property)
38
    {
39
        if (null !== $this->parent) {
40
            if ($this->hasFakerMethod($this->parent, $property)) {
41
                return true;
42
            }
43
        }
44
45
        return $this->hasFakerMethod($this, $property);
46
    }
47
48
    public function fake($property, array $args = [])
49
    {
50
        $object = method_exists($this, $property) ? $this : null;
51
52
        if ((null !== $this->parent) && ($this->hasFakerMethod($this->parent, $property))) {
53
            $object = $this->parent;
54
        }
55
56
        $method = $this->getFakerMethod($object, $property);
57
58
        return $method->invokeArgs($method->isStatic() ? null : $object, $args);
59
    }
60
61
    protected function hasFakerMethod($object, $method)
62
    {
63
        $rfl = new \ReflectionClass($object);
64
65
        return $rfl->hasMethod($method);
66
    }
67
68
    protected function getFakerMethod($object, $method)
69
    {
70
        $rfl = new \ReflectionClass($object);
71
72
        return $rfl->getMethod($method);
73
    }
74
}
75