@@ -151,7 +151,7 @@ |
||
| 151 | 151 | * |
| 152 | 152 | * @since 0.1.0 |
| 153 | 153 | * |
| 154 | - * @return array Array of function names. |
|
| 154 | + * @return string[] Array of function names. |
|
| 155 | 155 | */ |
| 156 | 156 | protected function getIgnoredFunctions() |
| 157 | 157 | { |
@@ -23,154 +23,154 @@ |
||
| 23 | 23 | trait NamespaceBacktracerTrait |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Get the caller from debug_backtrace() info. |
|
| 28 | - * |
|
| 29 | - * This traverses the call stack until it finds the first function that is |
|
| 30 | - * not a method of an ignored class/interface. |
|
| 31 | - * You should pass in the output of |
|
| 32 | - * `debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)`. |
|
| 33 | - * |
|
| 34 | - * @since 0.1.0 |
|
| 35 | - * |
|
| 36 | - * @param array|null $debugInfo Optional. Output of `debug_backtrace()` function. |
|
| 37 | - * |
|
| 38 | - * @return string Fully qualified name of the calling object/function. |
|
| 39 | - */ |
|
| 40 | - protected function getCaller($debugInfo = null) |
|
| 41 | - { |
|
| 42 | - // Fetch the debugInfo if none was passed in. |
|
| 43 | - if ($debugInfo === null) { |
|
| 44 | - $debugInfo = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $ignoredInterfaces = $this->getIgnoredInterfaces(); |
|
| 48 | - $ignoredFunctions = $this->getIgnoredFunctions(); |
|
| 49 | - |
|
| 50 | - foreach ($debugInfo as $entry) { |
|
| 51 | - $found = false; |
|
| 52 | - |
|
| 53 | - // Are we dealing with a class method or a function? |
|
| 54 | - if (isset($entry['class']) && ! empty($entry['class'])) { |
|
| 55 | - $class = $entry['class']; |
|
| 56 | - |
|
| 57 | - $ignored = false; |
|
| 58 | - foreach ($ignoredInterfaces as $ignoredInterface) { |
|
| 59 | - if ($class === $ignoredInterface) { |
|
| 60 | - $ignored = true; |
|
| 61 | - break; |
|
| 62 | - } |
|
| 63 | - if (is_subclass_of($class, $ignoredInterface)) { |
|
| 64 | - $ignored = true; |
|
| 65 | - break; |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - if (! $ignored) { |
|
| 69 | - $found = $class; |
|
| 70 | - } |
|
| 71 | - } else { |
|
| 72 | - $function = $entry['function']; |
|
| 73 | - if (! in_array($function, $ignoredFunctions, true)) { |
|
| 74 | - $found = $function; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - if (false !== $found) { |
|
| 79 | - return $found; |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return ''; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Get the caller's namespace from debug_backtrace() info. |
|
| 88 | - * |
|
| 89 | - * This traverses the call stack until it finds the first function that is |
|
| 90 | - * not a method of an ignored class/interface. |
|
| 91 | - * You should pass in the output of |
|
| 92 | - * `debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)`. |
|
| 93 | - * |
|
| 94 | - * @since 0.1.0 |
|
| 95 | - * |
|
| 96 | - * @param array|null $debugInfo Optional. Output of `debug_backtrace()` function. |
|
| 97 | - * |
|
| 98 | - * @return string Namespace of the calling object. |
|
| 99 | - */ |
|
| 100 | - protected function getCallingNamespace($debugInfo = null) |
|
| 101 | - { |
|
| 102 | - // Fetch the debugInfo if none was passed in. |
|
| 103 | - if ($debugInfo === null) { |
|
| 104 | - $debugInfo = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $namespace = $this->extractNamespace($this->getCaller($debugInfo)); |
|
| 108 | - |
|
| 109 | - return '' !== $namespace |
|
| 110 | - ? $namespace |
|
| 111 | - : $this->getGlobalNamespace(); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Extract the namespace from a fully qualified class name. |
|
| 116 | - * |
|
| 117 | - * @since 0.1.0 |
|
| 118 | - * |
|
| 119 | - * @param string $class Fully qualified class name. |
|
| 120 | - * |
|
| 121 | - * @return string Namespace of the class. Empty string if none. |
|
| 122 | - */ |
|
| 123 | - protected function extractNamespace($class) |
|
| 124 | - { |
|
| 125 | - $pos = strrpos($class, '\\'); |
|
| 126 | - if (false === $pos) { |
|
| 127 | - return ''; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - return substr($class, 0, $pos); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Get an array of interfaces/classes to ignore while fetching a namespace. |
|
| 135 | - * |
|
| 136 | - * Override this method to adapt the output to your specific environment. |
|
| 137 | - * |
|
| 138 | - * @since 0.1.0 |
|
| 139 | - * |
|
| 140 | - * @return array Array of interface/class names. |
|
| 141 | - */ |
|
| 142 | - protected function getIgnoredInterfaces() |
|
| 143 | - { |
|
| 144 | - return []; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Get an array of functions to ignore while fetching a namespace. |
|
| 149 | - * |
|
| 150 | - * Override this method to adapt the output to your specific environment. |
|
| 151 | - * |
|
| 152 | - * @since 0.1.0 |
|
| 153 | - * |
|
| 154 | - * @return array Array of function names. |
|
| 155 | - */ |
|
| 156 | - protected function getIgnoredFunctions() |
|
| 157 | - { |
|
| 158 | - return [ |
|
| 159 | - 'call_user_func', |
|
| 160 | - ]; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Get the string that is returned when the global namespace was hit. |
|
| 165 | - * |
|
| 166 | - * Override this method to adapt the output to your specific environment. |
|
| 167 | - * |
|
| 168 | - * @since 0.1.0 |
|
| 169 | - * |
|
| 170 | - * @return string String that represents the global namespace. |
|
| 171 | - */ |
|
| 172 | - protected function getGlobalNamespace() |
|
| 173 | - { |
|
| 174 | - return '(global)'; |
|
| 175 | - } |
|
| 26 | + /** |
|
| 27 | + * Get the caller from debug_backtrace() info. |
|
| 28 | + * |
|
| 29 | + * This traverses the call stack until it finds the first function that is |
|
| 30 | + * not a method of an ignored class/interface. |
|
| 31 | + * You should pass in the output of |
|
| 32 | + * `debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)`. |
|
| 33 | + * |
|
| 34 | + * @since 0.1.0 |
|
| 35 | + * |
|
| 36 | + * @param array|null $debugInfo Optional. Output of `debug_backtrace()` function. |
|
| 37 | + * |
|
| 38 | + * @return string Fully qualified name of the calling object/function. |
|
| 39 | + */ |
|
| 40 | + protected function getCaller($debugInfo = null) |
|
| 41 | + { |
|
| 42 | + // Fetch the debugInfo if none was passed in. |
|
| 43 | + if ($debugInfo === null) { |
|
| 44 | + $debugInfo = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $ignoredInterfaces = $this->getIgnoredInterfaces(); |
|
| 48 | + $ignoredFunctions = $this->getIgnoredFunctions(); |
|
| 49 | + |
|
| 50 | + foreach ($debugInfo as $entry) { |
|
| 51 | + $found = false; |
|
| 52 | + |
|
| 53 | + // Are we dealing with a class method or a function? |
|
| 54 | + if (isset($entry['class']) && ! empty($entry['class'])) { |
|
| 55 | + $class = $entry['class']; |
|
| 56 | + |
|
| 57 | + $ignored = false; |
|
| 58 | + foreach ($ignoredInterfaces as $ignoredInterface) { |
|
| 59 | + if ($class === $ignoredInterface) { |
|
| 60 | + $ignored = true; |
|
| 61 | + break; |
|
| 62 | + } |
|
| 63 | + if (is_subclass_of($class, $ignoredInterface)) { |
|
| 64 | + $ignored = true; |
|
| 65 | + break; |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + if (! $ignored) { |
|
| 69 | + $found = $class; |
|
| 70 | + } |
|
| 71 | + } else { |
|
| 72 | + $function = $entry['function']; |
|
| 73 | + if (! in_array($function, $ignoredFunctions, true)) { |
|
| 74 | + $found = $function; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + if (false !== $found) { |
|
| 79 | + return $found; |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + return ''; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Get the caller's namespace from debug_backtrace() info. |
|
| 88 | + * |
|
| 89 | + * This traverses the call stack until it finds the first function that is |
|
| 90 | + * not a method of an ignored class/interface. |
|
| 91 | + * You should pass in the output of |
|
| 92 | + * `debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)`. |
|
| 93 | + * |
|
| 94 | + * @since 0.1.0 |
|
| 95 | + * |
|
| 96 | + * @param array|null $debugInfo Optional. Output of `debug_backtrace()` function. |
|
| 97 | + * |
|
| 98 | + * @return string Namespace of the calling object. |
|
| 99 | + */ |
|
| 100 | + protected function getCallingNamespace($debugInfo = null) |
|
| 101 | + { |
|
| 102 | + // Fetch the debugInfo if none was passed in. |
|
| 103 | + if ($debugInfo === null) { |
|
| 104 | + $debugInfo = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $namespace = $this->extractNamespace($this->getCaller($debugInfo)); |
|
| 108 | + |
|
| 109 | + return '' !== $namespace |
|
| 110 | + ? $namespace |
|
| 111 | + : $this->getGlobalNamespace(); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Extract the namespace from a fully qualified class name. |
|
| 116 | + * |
|
| 117 | + * @since 0.1.0 |
|
| 118 | + * |
|
| 119 | + * @param string $class Fully qualified class name. |
|
| 120 | + * |
|
| 121 | + * @return string Namespace of the class. Empty string if none. |
|
| 122 | + */ |
|
| 123 | + protected function extractNamespace($class) |
|
| 124 | + { |
|
| 125 | + $pos = strrpos($class, '\\'); |
|
| 126 | + if (false === $pos) { |
|
| 127 | + return ''; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + return substr($class, 0, $pos); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Get an array of interfaces/classes to ignore while fetching a namespace. |
|
| 135 | + * |
|
| 136 | + * Override this method to adapt the output to your specific environment. |
|
| 137 | + * |
|
| 138 | + * @since 0.1.0 |
|
| 139 | + * |
|
| 140 | + * @return array Array of interface/class names. |
|
| 141 | + */ |
|
| 142 | + protected function getIgnoredInterfaces() |
|
| 143 | + { |
|
| 144 | + return []; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Get an array of functions to ignore while fetching a namespace. |
|
| 149 | + * |
|
| 150 | + * Override this method to adapt the output to your specific environment. |
|
| 151 | + * |
|
| 152 | + * @since 0.1.0 |
|
| 153 | + * |
|
| 154 | + * @return array Array of function names. |
|
| 155 | + */ |
|
| 156 | + protected function getIgnoredFunctions() |
|
| 157 | + { |
|
| 158 | + return [ |
|
| 159 | + 'call_user_func', |
|
| 160 | + ]; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Get the string that is returned when the global namespace was hit. |
|
| 165 | + * |
|
| 166 | + * Override this method to adapt the output to your specific environment. |
|
| 167 | + * |
|
| 168 | + * @since 0.1.0 |
|
| 169 | + * |
|
| 170 | + * @return string String that represents the global namespace. |
|
| 171 | + */ |
|
| 172 | + protected function getGlobalNamespace() |
|
| 173 | + { |
|
| 174 | + return '(global)'; |
|
| 175 | + } |
|
| 176 | 176 | } |
@@ -23,7 +23,7 @@ |
||
| 23 | 23 | * Return the configuration with a vendor/package prefix. |
| 24 | 24 | */ |
| 25 | 25 | return [ |
| 26 | - 'BrightNucleus' => [ |
|
| 27 | - 'NamespaceBacktracer' => $configuration, |
|
| 28 | - ], |
|
| 26 | + 'BrightNucleus' => [ |
|
| 27 | + 'NamespaceBacktracer' => $configuration, |
|
| 28 | + ], |
|
| 29 | 29 | ]; |
@@ -23,7 +23,7 @@ |
||
| 23 | 23 | abstract class NamespaceBacktracerClass |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - // Abstract class uses trait to keep code DRY. |
|
| 27 | - // Use trait directly when you need to override an existing class. |
|
| 28 | - use NamespaceBacktracerTrait; |
|
| 26 | + // Abstract class uses trait to keep code DRY. |
|
| 27 | + // Use trait directly when you need to override an existing class. |
|
| 28 | + use NamespaceBacktracerTrait; |
|
| 29 | 29 | } |