forked from jsebrech/php-o
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionClass.php
More file actions
160 lines (143 loc) · 4.28 KB
/
Copy pathReflectionClass.php
File metadata and controls
160 lines (143 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace O;
if (!class_exists("\\O\\StringClass")) include("StringClass.php");
/**
* Reflection class with type hinting and extended docblock parsing
*/
class ReflectionClass extends \ReflectionClass
{
/**
* @param int $filter
* @return \O\ReflectionMethod[]
*/
public function getMethods($filter = NULL) {
$methods = parent::getMethods($filter);
foreach ($methods as $index => $method) {
$methods[$index] = new ReflectionMethod(
$this->getName(), $method->getName());
};
return $methods;
}
/**
* @param string $name
* @return \O\ReflectionMethod
*/
public function getMethod($name) {
return new \O\ReflectionMethod($this->getName(), $name);
}
/**
* @param int $filter
* @return \O\ReflectionProperty[]
*/
public function getProperties($filter = NULL) {
if ($filter === NULL) {
$filter =
ReflectionProperty::IS_STATIC |
ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PROTECTED |
ReflectionProperty::IS_PRIVATE;
};
$properties = parent::getProperties($filter);
foreach ($properties as $index => $property) {
$properties[$index] = new ReflectionProperty(
$this->getName(), $property->getName());
};
return $properties;
}
/**
* @param string $name
* @return \O\ReflectionProperty
*/
public function getProperty($name) {
return new \O\ReflectionProperty($this->getName(), $name);
}
/**
* @param bool $onlytext
* @return string
*/
public function getDocComment($onlytext = FALSE) {
$doc = parent::getDocComment();
if ($onlytext) {
$doc = s($doc)->preg_replace("/(?<=[\r\n])[\\s]*\*(\ )?(?![\/])/", "");
$doc = s($doc)->preg_replace("/^[\\s]*\/\*\*[\\s]*[\r\n]*/", "");
$doc = s($doc)->preg_replace("/[\r\n]*[\\s]*\*\/$/", "");
};
return (string) $doc;
}
}
class ReflectionProperty extends \ReflectionProperty
{
public function getDocComment($onlytext = FALSE) {
$doc = parent::getDocComment();
if ($onlytext) {
$doc = s($doc)->preg_replace("/(?<=[\r\n])[\\s]*\*(\ )?(?![\/])/", "");
$doc = s($doc)->preg_replace("/^[\\s]*\/\*\*[\\s]*[\r\n]*/", "");
$doc = s($doc)->preg_replace("/[\r\n]*[\\s]*\*\/$/", "");
};
return (string) $doc;
}
public function getType() {
$doc = $this->getDocComment();
$matches = array();
$pattern = "/\@var[\\s]+([\\S]+)/";
if (s($doc)->preg_match($pattern, $matches)) {
return $matches[1];
} else {
return NULL;
}
}
}
class ReflectionMethod extends \ReflectionMethod
{
public function getDocComment($onlytext = FALSE) {
$doc = parent::getDocComment();
if ($onlytext) {
$doc = s($doc)->preg_replace("/(?<=[\r\n])[\\s]*\*(\ )?(?![\/])/", "");
$doc = s($doc)->preg_replace("/^[\\s]*\/\*\*[\\s]*[\r\n]*/", "");
$doc = s($doc)->preg_replace("/[\r\n]*[\\s]*\*\/$/", "");
};
return (string) $doc;
}
public function getDeclaringClass() {
return new \O\ReflectionClass(parent::getDeclaringClass()->getName());
}
public function getParameters() {
$params = parent::getParameters();
foreach ($params as $index => $param) {
$params[$index] = new \O\ReflectionParameter(
array($this->getDeclaringClass()->getName(), $this->getName()),
$param->getName());
};
return $params;
}
public function getParameter($name) {
return new ReflectionParameter(
array($this->getDeclaringClass()->getName(), $this->getName()),
$name);
}
}
class ReflectionParameter extends \ReflectionParameter
{
public function getDocComment() {
$methoddoc = $this->getDeclaringFunction()->getDocComment(TRUE);
$parts = s($methoddoc)->explode("@param");
for ($i = 1; $i < count($parts); $i++) $parts[$i] = "@param".$parts[$i];
a($parts)->shift();
$filter = "/\@param[^\\$]+\\$".$this->getName()."(?![\\w])/";
foreach ($parts as $part) {
if (s($part)->preg_match($filter)) {
return $part;
};
};
return "";
}
public function getDeclaringFunction() {
$f = parent::getDeclaringFunction();
/** @var $f ReflectionMethod */
if (is_a($f, "ReflectionMethod")) {
return new \O\ReflectionMethod($f->getDeclaringClass()->getName(), $f->getName());
} else {
return $f;
}
}
}