Passed
Push — master ( 0d7b76...076a5e )
by Randy
02:12
created

Validator::new()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Dgame\Object;
4
5
use ReflectionMethod;
6
use ReflectionProperty;
7
use function Dgame\Conditional\debug;
8
9
/**
10
 * Class Validator
11
 * @package Dgame\Object
12
 */
13
final class Validator
14
{
15
    /**
16
     * @var ObjectFacade
17
     */
18
    private $facade;
19
20
    /**
21
     * Validator constructor.
22
     *
23
     * @param ObjectFacade $facade
24
     */
25
    public function __construct(ObjectFacade $facade)
26
    {
27
        $this->facade = $facade;
28
    }
29
30
    /**
31
     * @param ObjectFacade $facade
32
     *
33
     * @return Validator
34
     */
35
    public static function new(ObjectFacade $facade): self
36
    {
37
        return new self($facade);
38
    }
39
40
    /**
41
     * @param ReflectionProperty $property
42
     *
43
     * @return bool
44
     */
45 View Code Duplication
    public function validateProperty(ReflectionProperty $property): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
46
    {
47
        if (!$property->isPublic()) {
48
            debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Property %s is not public', $property->getName());
49
50
            return false;
51
        }
52
53
        if ($property->isStatic()) {
54
            debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Property %s is static', $property->getName());
55
56
            return false;
57
        }
58
59
        return true;
60
    }
61
62
    /**
63
     * @param ReflectionMethod $method
64
     *
65
     * @return bool
66
     */
67 View Code Duplication
    public function validateMethod(ReflectionMethod $method): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
68
    {
69
        if (!$method->isPublic()) {
70
            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...
71
72
            return false;
73
        }
74
75
        if ($method->isStatic()) {
76
            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...
77
78
            return false;
79
        }
80
81
        return true;
82
    }
83
84
    /**
85
     * @param ReflectionMethod $method
86
     * @param                  $value
87
     *
88
     * @return bool
89
     */
90
    public function validateSetterMethod(ReflectionMethod $method, $value): bool
91
    {
92
        if (!$this->validateMethod($method)) {
93
            return false;
94
        }
95
96 View Code Duplication
        if ($value === null && $method->getNumberOfParameters() !== 0 && !$method->getParameters()[0]->allowsNull()) {
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...
97
            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...
98
99
            return false;
100
        }
101
102
        if ($method->getNumberOfParameters() === 0) {
103
            debug(ObjectFacade::DEBUG_LABEL)->output('[Warning] Method %s does not accept any parameters', $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
106
        return true;
107
    }
108
109
    /**
110
     * @param ReflectionMethod $method
111
     *
112
     * @return bool
113
     */
114
    public function validateGetterMethod(ReflectionMethod $method): bool
115
    {
116
        if (!$this->validateMethod($method)) {
117
            return false;
118
        }
119
120
        $value = $method->invoke($this->facade->getObject());
121 View Code Duplication
        if ($value === null && $method->hasReturnType() && !$method->getReturnType()->allowsNull()) {
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...
122
            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...
123
124
            return false;
125
        }
126
127
        return true;
128
    }
129
}