3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
28 namespace Civi\Api4\Utils
;
31 * Just another place to put static functions...
33 class ReflectionUtils
{
36 * @param \Reflector|\ReflectionClass $reflection
38 * If we are not reflecting the class itself, specify "Method", "Property", etc.
42 public static function getCodeDocs($reflection, $type = NULL) {
43 $docs = self
::parseDocBlock($reflection->getDocComment());
45 // Recurse into parent functions
46 if (isset($docs['inheritDoc']) ||
isset($docs['inheritdoc'])) {
47 unset($docs['inheritDoc'], $docs['inheritdoc']);
48 $newReflection = NULL;
51 $name = $reflection->getName();
52 $reflectionClass = $reflection->getDeclaringClass()->getParentClass();
53 if ($reflectionClass) {
54 $getItem = "get$type";
55 $newReflection = $reflectionClass->$getItem($name);
59 $newReflection = $reflection->getParentClass();
62 catch (\ReflectionException
$e) {
66 $additionalDocs = self
::getCodeDocs($newReflection, $type);
67 if (!empty($docs['comment']) && !empty($additionalDocs['comment'])) {
68 $docs['comment'] .= "\n\n" . $additionalDocs['comment'];
70 $docs +
= $additionalDocs;
77 * @param string $comment
80 public static function parseDocBlock($comment) {
82 foreach (preg_split("/((\r?\n)|(\r\n?))/", $comment) as $num => $line) {
83 if (!$num ||
strpos($line, '*/') !== FALSE) {
86 $line = ltrim(trim($line), '* ');
87 if (strpos($line, '@') === 0) {
88 $words = explode(' ', $line);
89 $key = substr($words[0], 1);
91 $info['type'] = explode('|', $words[1]);
93 elseif ($key == 'options') {
94 $val = str_replace(', ', ',', implode(' ', array_slice($words, 1)));
95 $info['options'] = explode(',', $val);
98 // Unrecognized annotation, but we'll duly add it to the info array
99 $val = implode(' ', array_slice($words, 1));
100 $info[$key] = strlen($val) ?
$val : TRUE;
104 $info['description'] = $line;
107 if (isset($info['comment'])) {
108 $info['comment'] .= "\n";
112 $info['comment'] = isset($info['comment']) ?
"{$info['comment']}\n$line" : $line;
115 if (isset($info['comment'])) {
116 $info['comment'] = trim($info['comment']);
122 * List all traits used by a class and its parents.
124 * @param object|string $class
127 public static function getTraits($class) {
129 // Get traits of this class + parent classes
131 $traits = array_merge(class_uses($class), $traits);
132 } while ($class = get_parent_class($class));
133 // Get traits of traits
134 foreach ($traits as $trait => $same) {
135 $traits = array_merge(class_uses($trait), $traits);