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.
24 * Variable substitutions to perform in the docblock
27 public static function getCodeDocs($reflection, $type = NULL, $vars = []) {
28 $comment = $reflection->getDocComment();
29 foreach ($vars as $key => $val) {
30 $comment = str_replace('$' . strtoupper(\CRM_Utils_String
::pluralize($key)), \CRM_Utils_String
::pluralize($val), $comment);
31 $comment = str_replace('$' . strtoupper($key), $val, $comment);
33 $docs = self
::parseDocBlock($comment);
35 // Recurse into parent functions
36 if (isset($docs['inheritDoc']) ||
isset($docs['inheritdoc'])) {
37 unset($docs['inheritDoc'], $docs['inheritdoc']);
38 $newReflection = NULL;
41 $name = $reflection->getName();
42 $reflectionClass = $reflection->getDeclaringClass()->getParentClass();
43 if ($reflectionClass) {
44 $getItem = "get$type";
45 $newReflection = $reflectionClass->$getItem($name);
49 $newReflection = $reflection->getParentClass();
52 catch (\ReflectionException
$e) {
56 $additionalDocs = self
::getCodeDocs($newReflection, $type, $vars);
57 if (!empty($docs['comment']) && !empty($additionalDocs['comment'])) {
58 $docs['comment'] .= "\n\n" . $additionalDocs['comment'];
60 $docs +
= $additionalDocs;
67 * @param string $comment
70 public static function parseDocBlock($comment) {
73 foreach (preg_split("/((\r?\n)|(\r\n?))/", $comment) as $num => $line) {
74 if (!$num ||
strpos($line, '*/') !== FALSE) {
77 $line = ltrim(trim($line), '*');
78 if (strlen($line) && $line[0] === ' ') {
79 $line = substr($line, 1);
81 if (strpos(ltrim($line), '@') === 0) {
82 $words = explode(' ', ltrim($line, ' @'));
83 $key = array_shift($words);
86 $info['type'] = explode('|', $words[0]);
88 elseif ($key == 'return') {
89 $info['return'] = explode('|', $words[0]);
91 elseif ($key == 'options') {
92 $val = str_replace(', ', ',', implode(' ', $words));
93 $info['options'] = explode(',', $val);
95 elseif ($key == 'throws' ||
$key == 'see') {
96 $info[$key][] = implode(' ', $words);
98 elseif ($key == 'param' && $words) {
99 $type = $words[0][0] !== '$' ?
explode('|', array_shift($words)) : NULL;
100 $param = rtrim(array_shift($words), '-:()/');
101 $info['params'][$param] = [
103 'description' => $words ?
ltrim(implode(' ', $words), '-: ') : '',
108 // Unrecognized annotation, but we'll duly add it to the info array
109 $val = implode(' ', $words);
110 $info[$key] = strlen($val) ?
$val : TRUE;
114 $info['params'][$param]['comment'] .= $line . "\n";
117 $info['description'] = ucfirst($line);
120 if (isset($info['comment'])) {
121 $info['comment'] .= "\n";
124 $info['comment'] = NULL;
127 // For multi-line description.
128 elseif (count($info) === 1 && isset($info['description']) && substr($info['description'], -1) !== '.') {
129 $info['description'] .= ' ' . $line;
132 $info['comment'] = isset($info['comment']) ?
"{$info['comment']}\n$line" : $line;
135 if (isset($info['comment'])) {
136 $info['comment'] = rtrim($info['comment']);
142 * List all traits used by a class and its parents.
144 * @param object|string $class
147 public static function getTraits($class) {
149 // Get traits of this class + parent classes
151 $traits = array_merge(class_uses($class), $traits);
152 } while ($class = get_parent_class($class));
153 // Get traits of traits
154 foreach ($traits as $trait => $same) {
155 $traits = array_merge(class_uses($trait), $traits);