1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
namespace gossi\codegen\utils; |
18
|
|
|
|
19
|
|
|
class ReflectionUtils { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
* @param bool $publicOnly |
24
|
|
|
*/ |
25
|
1 |
|
public static function getOverrideableMethods(\ReflectionClass $class, $publicOnly = false) { |
26
|
1 |
|
$filter = \ReflectionMethod::IS_PUBLIC; |
27
|
|
|
|
28
|
1 |
|
if (!$publicOnly) { |
29
|
1 |
|
$filter |= \ReflectionMethod::IS_PROTECTED; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return array_filter($class->getMethods($filter), function ($method) { |
33
|
1 |
|
return !$method->isFinal() && !$method->isStatic(); |
34
|
1 |
|
}); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
* @param string $docComment |
40
|
|
|
*/ |
41
|
1 |
|
public static function getUnindentedDocComment($docComment) { |
42
|
1 |
|
$lines = explode("\n", $docComment); |
43
|
1 |
|
for ($i = 0, $c = count($lines); $i < $c; $i++) { |
44
|
1 |
|
if (0 === $i) { |
45
|
1 |
|
$docBlock = $lines[0] . "\n"; |
46
|
1 |
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$docBlock .= ' ' . ltrim($lines[$i]); |
50
|
|
|
|
51
|
1 |
|
if ($i + 1 < $c) { |
52
|
1 |
|
$docBlock .= "\n"; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return $docBlock; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
* @param \ReflectionFunctionAbstract $function |
62
|
|
|
*/ |
63
|
1 |
|
public static function getFunctionBody(\ReflectionFunctionAbstract $function) { |
64
|
1 |
|
$source = file($function->getFileName()); |
65
|
1 |
|
$start = $function->getStartLine() - 1; |
66
|
1 |
|
$end = $function->getEndLine(); |
67
|
1 |
|
$body = implode('', array_slice($source, $start, $end - $start)); |
68
|
1 |
|
$open = strpos($body, '{'); |
69
|
1 |
|
$close = strrpos($body, '}'); |
70
|
1 |
|
return trim(substr($body, $open + 1, (strlen($body) - $close) * -1)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|