Completed
Push — master ( 1ca13f...00509a )
by Randy
03:19
created

Validator::isValidSetterMethod()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 38
Code Lines 20

Duplication

Lines 18
Ratio 47.37 %

Importance

Changes 0
Metric Value
dl 18
loc 38
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 7
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
use function Dgame\Conditional\debug;
10
11
/**
12
 * Class Validator
13
 * @package Dgame\Object
14
 */
15
final class Validator
16
{
17
    /**
18
     * @var ObjectFacade
19
     */
20
    private $facade;
21
22
    /**
23
     * Validator constructor.
24
     *
25
     * @param ObjectFacade $facade
26
     */
27
    public function __construct(ObjectFacade $facade)
28
    {
29
        $this->facade = $facade;
30
    }
31
32
    /**
33
     * @param ObjectFacade $facade
34
     *
35
     * @return Validator
36
     */
37
    public static function new(ObjectFacade $facade): self
38
    {
39
        return new self($facade);
40
    }
41
42
    /**
43
     * @param ReflectionProperty $property
44
     *
45
     * @return bool
46
     */
47
    public function isValidProperty(ReflectionProperty $property): bool
48
    {
49
        if (!$property->isPublic()) {
50
            debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Property %s is not public', $property->getName());
51
52
            return false;
53
        }
54
55
        if ($property->isStatic()) {
56
            debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Property %s is static', $property->getName());
57
58
            return false;
59
        }
60
61
        return true;
62
    }
63
64
    /**
65
     * @param ReflectionMethod $method
66
     *
67
     * @return bool
68
     */
69
    public function isValidMethod(ReflectionMethod $method): bool
70
    {
71
        if (!$method->isPublic()) {
72
            debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Method %s is not public', $method->getName());
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
73
74
            return false;
75
        }
76
77
        if ($method->isStatic()) {
78
            debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Method %s is static', $method->getName());
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
79
80
            return false;
81
        }
82
83
        return true;
84
    }
85
86
    /**
87
     * @param ReflectionMethod $method
88
     * @param                  $value
89
     *
90
     * @return bool
91
     */
92
    public function isValidSetterMethod(ReflectionMethod $method, $value): bool
93
    {
94
        if (!$this->isValidMethod($method)) {
95
            return false;
96
        }
97
98
        if ($method->getNumberOfParameters() === 0) {
99
            return true;
100
        }
101
102
        if ($method->getNumberOfRequiredParameters() > 1) {
103
            debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Method %s need more than one parameter', $method->getName());
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
104
105
            return false;
106
        }
107
108 View Code Duplication
        if ($value === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            if ($method->getParameters()[0]->allowsNull()) {
110
                return true;
111
            }
112
113
            debug(ObjectFacade::DEBUG_LABEL)->output('[Error] First parameter of method %s is not allowed to be null', $method->getName());
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
114
115
            return false;
116
        }
117
118 View Code Duplication
        if (!$this->isValidValue($method->getParameters()[0], $value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
            debug(ObjectFacade::DEBUG_LABEL)->output(
120
                '[Warning] Method %s does not accept value %s',
121
                $method->getName(),
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
122
                var_export($value, true)
123
            );
124
125
            return false;
126
        }
127
128
        return true;
129
    }
130
131
    /**
132
     * @param ReflectionParameter $parameter
133
     * @param                     $value
134
     *
135
     * @return bool
136
     */
137
    public function isValidValue(ReflectionParameter $parameter, $value): bool
138
    {
139
        return !$parameter->hasType() || Type::from($parameter)->accept($value);
140
    }
141
142
    /**
143
     * @param ReflectionMethod $method
144
     *
145
     * @return bool
146
     */
147
    public function validateGetterMethod(ReflectionMethod $method): bool
148
    {
149
        if (!$this->isValidMethod($method)) {
150
            return false;
151
        }
152
153
        $value = $method->invoke($this->facade->getObject());
154
        if ($value === null && $method->hasReturnType() && !$method->getReturnType()->allowsNull()) {
155
            debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Method %s return value is not allowed to be null', $method->getName());
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
156
157
            return false;
158
        }
159
160
        return true;
161
    }
162
}