Merge pull request #16425 from colemanw/markdown
[civicrm-core.git] / Civi / Api4 / Utils / ReflectionUtils.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 namespace Civi\Api4\Utils;
13
14 /**
15 * Just another place to put static functions...
16 */
17 class ReflectionUtils {
18
19 /**
20 * @param \Reflector|\ReflectionClass $reflection
21 * @param string $type
22 * If we are not reflecting the class itself, specify "Method", "Property", etc.
23 *
24 * @return array
25 */
26 public static function getCodeDocs($reflection, $type = NULL) {
27 $docs = self::parseDocBlock($reflection->getDocComment());
28
29 // Recurse into parent functions
30 if (isset($docs['inheritDoc']) || isset($docs['inheritdoc'])) {
31 unset($docs['inheritDoc'], $docs['inheritdoc']);
32 $newReflection = NULL;
33 try {
34 if ($type) {
35 $name = $reflection->getName();
36 $reflectionClass = $reflection->getDeclaringClass()->getParentClass();
37 if ($reflectionClass) {
38 $getItem = "get$type";
39 $newReflection = $reflectionClass->$getItem($name);
40 }
41 }
42 else {
43 $newReflection = $reflection->getParentClass();
44 }
45 }
46 catch (\ReflectionException $e) {
47 }
48 if ($newReflection) {
49 // Mix in
50 $additionalDocs = self::getCodeDocs($newReflection, $type);
51 if (!empty($docs['comment']) && !empty($additionalDocs['comment'])) {
52 $docs['comment'] .= "\n\n" . $additionalDocs['comment'];
53 }
54 $docs += $additionalDocs;
55 }
56 }
57 return $docs;
58 }
59
60 /**
61 * @param string $comment
62 * @return array
63 */
64 public static function parseDocBlock($comment) {
65 $info = [];
66 $param = NULL;
67 foreach (preg_split("/((\r?\n)|(\r\n?))/", $comment) as $num => $line) {
68 if (!$num || strpos($line, '*/') !== FALSE) {
69 continue;
70 }
71 $line = ltrim(trim($line), '*');
72 if (strlen($line) && $line[0] === ' ') {
73 $line = substr($line, 1);
74 }
75 if (strpos(ltrim($line), '@') === 0) {
76 $words = explode(' ', ltrim($line, ' @'));
77 $key = array_shift($words);
78 $param = NULL;
79 if ($key == 'var') {
80 $info['type'] = explode('|', $words[0]);
81 }
82 elseif ($key == 'return') {
83 $info['return'] = explode('|', $words[0]);
84 }
85 elseif ($key == 'options') {
86 $val = str_replace(', ', ',', implode(' ', $words));
87 $info['options'] = explode(',', $val);
88 }
89 elseif ($key == 'throws' || $key == 'see') {
90 $info[$key][] = implode(' ', $words);
91 }
92 elseif ($key == 'param' && $words) {
93 $type = $words[0][0] !== '$' ? explode('|', array_shift($words)) : NULL;
94 $param = rtrim(array_shift($words), '-:()/');
95 $info['params'][$param] = [
96 'type' => $type,
97 'description' => $words ? ltrim(implode(' ', $words), '-: ') : '',
98 'comment' => '',
99 ];
100 }
101 else {
102 // Unrecognized annotation, but we'll duly add it to the info array
103 $val = implode(' ', $words);
104 $info[$key] = strlen($val) ? $val : TRUE;
105 }
106 }
107 elseif ($param) {
108 $info['params'][$param]['comment'] .= $line . "\n";
109 }
110 elseif ($num == 1) {
111 $info['description'] = $line;
112 }
113 elseif (!$line) {
114 if (isset($info['comment'])) {
115 $info['comment'] .= "\n";
116 }
117 }
118 else {
119 $info['comment'] = isset($info['comment']) ? "{$info['comment']}\n$line" : $line;
120 }
121 }
122 if (isset($info['comment'])) {
123 $info['comment'] = trim($info['comment']);
124 }
125 return $info;
126 }
127
128 /**
129 * List all traits used by a class and its parents.
130 *
131 * @param object|string $class
132 * @return array
133 */
134 public static function getTraits($class) {
135 $traits = [];
136 // Get traits of this class + parent classes
137 do {
138 $traits = array_merge(class_uses($class), $traits);
139 } while ($class = get_parent_class($class));
140 // Get traits of traits
141 foreach ($traits as $trait => $same) {
142 $traits = array_merge(class_uses($trait), $traits);
143 }
144 return $traits;
145 }
146
147 }