Test Setup Failed
Push — master ( d81ac1...e02663 )
by Php Easy Api
04:31 queued 12s
created

ReflectionProcess::getProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Support;
4
5
use ReflectionClass;
6
use ReflectionMethod;
7
use ReflectionProperty;
8
use ReflectionException;
9
10
class ReflectionProcess
11
{
12
    /**
13
     * @var array $singletons
14
     */
15
    protected static $singletons = [];
16
17
    /**
18
     * @var $namespace
0 ignored issues
show
Documentation Bug introduced by
The doc comment $namespace at position 0 could not be parsed: Unknown type name '$namespace' at position 0 in $namespace.
Loading history...
19
     */
20
    protected $namespace;
21
22
    /**
23
     * @var $reflection
0 ignored issues
show
Documentation Bug introduced by
The doc comment $reflection at position 0 could not be parsed: Unknown type name '$reflection' at position 0 in $reflection.
Loading history...
24
     */
25
    protected $reflection;
26
27
    /**
28
     * @var $request
0 ignored issues
show
Documentation Bug introduced by
The doc comment $request at position 0 could not be parsed: Unknown type name '$request' at position 0 in $request.
Loading history...
29
     */
30
    protected $request;
31
32
    /**
33
     * @var null|string
34
     */
35
    protected $documentData;
36
37
    /**
38
     * Controller constructor.
39
     * @param $namespace
40
     */
41
    public function __construct($namespace = null)
42
    {
43
        if($namespace!==null){
44
            $this->namespace = $namespace;
45
        }
46
    }
47
48
    /**
49
     * get document data
50
     *
51
     * @return string|null
52
     */
53
    public function getDocumentData()
54
    {
55
        return $this->documentData;
56
    }
57
58
    /**
59
     * @return ReflectionProperty[]
60
     *
61
     * @throws ReflectionException
62
     */
63
    public function getProperties()
64
    {
65
        return $this->getReflectionClass()->getProperties();
66
    }
67
68
    /**
69
     * @return ReflectionClass
70
     *
71
     * @throws ReflectionException
72
     */
73
    public function getReflectionClass()
74
    {
75
        if(!isset(static::$singletons['reflectionClass'])){
76
            static::$singletons['reflectionClass'] = new ReflectionClass($this->namespace);
77
        }
78
        return static::$singletons['reflectionClass'];
79
    }
80
81
    /**
82
     * @param $method
83
     * @return ReflectionMethod
84
     *
85
     * @throws ReflectionException
86
     */
87
    public function getReflectionMethod($method)
88
    {
89
        return new ReflectionMethod($this->namespace,$method);
90
    }
91
92
    /**
93
     * resolve if the method document is available for container
94
     *
95
     * @param null|string $method
96
     * @param null|string $param
97
     * @return bool
98
     *
99
     * @throws ReflectionException
100
     */
101
    public function isAvailableMethodDocument($method=null,$param=null)
102
    {
103
        $document = $this->reflectionMethodParams($method)->document;
104
105
        if(is_string($document)
106
            && preg_match('#@'.$param.'\((.*?)\)\r\n#is',$document,$data)) {
107
            if (is_array($data) && isset($data[1])) {
108
                $this->documentData = $data[1];
109
                return true;
110
            }
111
        }
112
113
        return false;
114
    }
115
116
    /**
117
     * @param $method
118
     * @return object
119
     *
120
     * @throws ReflectionException
121
     */
122
    public function reflectionMethodParams($method)
123
    {
124
        $reflection = $this->getReflectionMethod($method);
125
126
        return (object)[
127
            'reflection'    => $reflection,
128
            'document'      => $reflection->getDocComment(),
129
            'parameters'    => $reflection->getParameters(),
130
            'isProtected'   => $reflection->isProtected(),
131
        ];
132
    }
133
134
    /**
135
     * @param null $namespace
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $namespace is correct as it would always require null to be passed?
Loading history...
136
     * @return $this
137
     */
138
    public function __invoke($namespace = null)
139
    {
140
        if($namespace!==null){
0 ignored issues
show
introduced by
The condition $namespace !== null is always false.
Loading history...
141
            $this->namespace = $namespace;
142
        }
143
        return $this;
144
    }
145
}