3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
12 namespace Civi\Api4\Utils
;
15 * Just another place to put static functions...
17 class ReflectionUtils
{
20 * @param \Reflector|\ReflectionClass $reflection
22 * If we are not reflecting the class itself, specify "Method", "Property", etc.
26 public static function getCodeDocs($reflection, $type = NULL) {
27 $docs = self
::parseDocBlock($reflection->getDocComment());
29 // Recurse into parent functions
30 if (isset($docs['inheritDoc']) ||
isset($docs['inheritdoc'])) {
31 unset($docs['inheritDoc'], $docs['inheritdoc']);
32 $newReflection = NULL;
35 $name = $reflection->getName();
36 $reflectionClass = $reflection->getDeclaringClass()->getParentClass();
37 if ($reflectionClass) {
38 $getItem = "get$type";
39 $newReflection = $reflectionClass->$getItem($name);
43 $newReflection = $reflection->getParentClass();
46 catch (\ReflectionException
$e) {
50 $additionalDocs = self
::getCodeDocs($newReflection, $type);
51 if (!empty($docs['comment']) && !empty($additionalDocs['comment'])) {
52 $docs['comment'] .= "\n\n" . $additionalDocs['comment'];
54 $docs +
= $additionalDocs;
61 * @param string $comment
64 public static function parseDocBlock($comment) {
67 foreach (preg_split("/((\r?\n)|(\r\n?))/", $comment) as $num => $line) {
68 if (!$num ||
strpos($line, '*/') !== FALSE) {
71 $line = preg_replace('/[ ]+/', ' ', ltrim(trim($line), '* '));
72 if (strpos($line, '@') === 0) {
73 $words = explode(' ', $line);
74 $key = substr(array_shift($words), 1);
77 $info['type'] = explode('|', $words[0]);
79 elseif ($key == 'return') {
80 $info['return'] = explode('|', $words[0]);
82 elseif ($key == 'options') {
83 $val = str_replace(', ', ',', implode(' ', $words));
84 $info['options'] = explode(',', $val);
86 elseif ($key == 'throws' ||
$key == 'see') {
87 $info[$key][] = implode(' ', $words);
89 elseif ($key == 'param' && $words) {
90 $type = $words[0][0] !== '$' ?
explode('|', array_shift($words)) : NULL;
91 $param = rtrim(array_shift($words), '-:()/');
92 $info['params'][$param] = [
94 'description' => $words ?
ltrim(implode(' ', $words), '-: ') : '',
99 // Unrecognized annotation, but we'll duly add it to the info array
100 $val = implode(' ', $words);
101 $info[$key] = strlen($val) ?
$val : TRUE;
105 $info['params'][$param]['comment'] .= $line . "\n";
108 $info['description'] = $line;
111 if (isset($info['comment'])) {
112 $info['comment'] .= "\n";
116 $info['comment'] = isset($info['comment']) ?
"{$info['comment']}\n$line" : $line;
119 if (isset($info['comment'])) {
120 $info['comment'] = trim($info['comment']);
126 * List all traits used by a class and its parents.
128 * @param object|string $class
131 public static function getTraits($class) {
133 // Get traits of this class + parent classes
135 $traits = array_merge(class_uses($class), $traits);
136 } while ($class = get_parent_class($class));
137 // Get traits of traits
138 foreach ($traits as $trait => $same) {
139 $traits = array_merge(class_uses($trait), $traits);