Passed
Push — master ( 00509a...e004d4 )
by Randy
02:49
created

Validator::isValidParameterValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 2
1
<?php
2
3
namespace Dgame\Object;
4
5
use Dgame\Type\Type;
6
use ReflectionMethod;
7
use ReflectionParameter;
8
use ReflectionProperty;
9
10
/**
11
 * Class Validator
12
 * @package Dgame\Object
13
 */
14
final class Validator
15
{
16
    /**
17
     * @var ObjectFacade
18
     */
19
    private $facade;
20
21
    /**
22
     * Validator constructor.
23
     *
24
     * @param ObjectFacade $facade
25
     */
26
    public function __construct(ObjectFacade $facade)
27
    {
28
        $this->facade = $facade;
29
    }
30
31
    /**
32
     * @param ObjectFacade $facade
33
     *
34
     * @return Validator
35
     */
36
    public static function new(ObjectFacade $facade): self
37
    {
38
        return new self($facade);
39
    }
40
41
    /**
42
     * @param ReflectionProperty $property
43
     *
44
     * @return bool
45
     */
46
    public function isValidProperty(ReflectionProperty $property): bool
47
    {
48
        return $property->isPublic() && !$property->isStatic();
49
    }
50
51
    /**
52
     * @param ReflectionMethod $method
53
     *
54
     * @return bool
55
     */
56
    public function isValidMethod(ReflectionMethod $method): bool
57
    {
58
        return $method->isPublic() && !$method->isStatic();
59
    }
60
61
    /**
62
     * @param ReflectionMethod $method
63
     * @param                  $value
64
     *
65
     * @return bool
66
     */
67
    public function isValidSetterMethod(ReflectionMethod $method, $value): bool
68
    {
69
        if (!$this->isValidMethod($method)) {
70
            return false;
71
        }
72
73
        if ($method->getNumberOfParameters() === 0) {
74
            return true;
75
        }
76
77
        if ($method->getNumberOfRequiredParameters() > 1) {
78
            return false;
79
        }
80
81
        if ($value === null) {
82
            return $method->getParameters()[0]->allowsNull();
83
        }
84
85
        return $this->isValidParameterValue($method->getParameters()[0], $value);
86
    }
87
88
    /**
89
     * @param ReflectionParameter $parameter
90
     * @param                     $value
91
     *
92
     * @return bool
93
     */
94
    public function isValidParameterValue(ReflectionParameter $parameter, $value): bool
95
    {
96
        return !$parameter->hasType() || Type::from($parameter)->accept($value);
97
    }
98
99
    /**
100
     * @param ReflectionMethod $method
101
     *
102
     * @return bool
103
     */
104
    public function validateGetterMethod(ReflectionMethod $method): bool
105
    {
106
        if (!$this->isValidMethod($method)) {
107
            return false;
108
        }
109
110
        $value = $method->invoke($this->facade->getObject());
111
112
        return $value !== null || !$method->hasReturnType() || $method->getReturnType()->allowsNull();
113
    }
114
}