| Conditions | 21 |
| Paths | 14 |
| Total Lines | 109 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 135 | public static function hookObject(&$object, $prefix = '', $recursive = true) { |
||
| 136 | if ((object) $object === $object) { |
||
| 137 | $isString = false; |
||
| 138 | } else { |
||
| 139 | $isString = true; |
||
| 140 | } |
||
| 141 | |||
| 142 | // Make sure we don't take over the hook object, or we'll end up |
||
| 143 | // recursively calling ourself. Some system classes shouldn't be hooked. |
||
| 144 | $className = str_replace('\\', '_', $isString ? $object : get_class($object)); |
||
| 145 | global $_; |
||
| 146 | if (isset($_) && in_array($className, array('\SciActive\Hook', 'depend', 'config', 'info'))) { |
||
| 147 | return false; |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($recursive && !$isString) { |
||
| 151 | foreach ($object as $curName => &$curProperty) { |
||
| 152 | if ((object) $curProperty === $curProperty) { |
||
| 153 | Hook::hookObject($curProperty, $prefix.$curName.'->'); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | if (!class_exists("\SciActive\HookOverride_$className")) { |
||
| 159 | if ($isString) { |
||
| 160 | $reflection = new \ReflectionClass($object); |
||
| 161 | } else { |
||
| 162 | $reflection = new \ReflectionObject($object); |
||
| 163 | } |
||
| 164 | $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); |
||
| 165 | |||
| 166 | $code = ''; |
||
| 167 | foreach ($methods as &$curMethod) { |
||
| 168 | $fname = $curMethod->getName(); |
||
| 169 | if (in_array($fname, array('__construct', '__destruct', '__get', '__set', '__isset', '__unset', '__toString', '__invoke', '__set_state', '__clone', '__sleep', 'jsonSerialize'))) { |
||
| 170 | continue; |
||
| 171 | } |
||
| 172 | |||
| 173 | //$fprefix = $curMethod->isFinal() ? 'final ' : ''; |
||
| 174 | $fprefix = $curMethod->isStatic() ? 'static ' : ''; |
||
| 175 | $params = $curMethod->getParameters(); |
||
| 176 | $paramArray = $paramNameArray = array(); |
||
| 177 | foreach ($params as &$curParam) { |
||
| 178 | $paramName = $curParam->getName(); |
||
| 179 | $paramPrefix = $curParam->isVariadic() ? '...' : ''; |
||
| 180 | $paramPrefix .= $curParam->isPassedByReference() ? '&' : ''; |
||
| 181 | if ($curParam->isDefaultValueAvailable()) { |
||
| 182 | $paramSuffix = ' = '.var_export($curParam->getDefaultValue(), true); |
||
| 183 | } else { |
||
| 184 | $paramSuffix = ''; |
||
| 185 | } |
||
| 186 | $paramArray[] = "{$paramPrefix}\${$paramName}{$paramSuffix}"; |
||
| 187 | $paramNameArray[] = "{$paramPrefix}\${$paramName}"; |
||
| 188 | } |
||
| 189 | unset($curParam); |
||
| 190 | $code .= $fprefix."function $fname(".implode(', ', $paramArray).") {\n" |
||
| 191 | .(defined('HHVM_VERSION') ? |
||
| 192 | ( |
||
| 193 | // There is bad behavior in HHVM where debug_backtrace |
||
| 194 | // won't return arguments, but calling func_get_args |
||
| 195 | // somewhere in the function changes that behavior to be |
||
| 196 | // consistent with official PHP. However, it also |
||
| 197 | // returns arguments by value, instead of by reference. |
||
| 198 | // So, we must use a more direct method. |
||
| 199 | " \$_HOOK_arguments = array();\n" |
||
| 200 | .(count($paramNameArray) > 0 ? |
||
| 201 | " \$_HOOK_arguments[] = ".implode('; $_HOOK_arguments[] = ', $paramNameArray).";\n" : |
||
| 202 | '' |
||
| 203 | ) |
||
| 204 | ." \$_HOOK_real_arg_count = func_num_args();\n" |
||
| 205 | ." \$_HOOK_arg_count = count(\$_HOOK_arguments);\n" |
||
| 206 | ." if (\$_HOOK_real_arg_count > \$_HOOK_arg_count) {\n" |
||
| 207 | ." for (\$i = \$_HOOK_arg_count; \$i < \$_HOOK_real_arg_count; \$i++)\n" |
||
| 208 | ." \$_HOOK_arguments[] = func_get_arg(\$i);\n" |
||
| 209 | ." }\n" |
||
| 210 | ) : ( |
||
| 211 | // We must use a debug_backtrace, because that's the |
||
| 212 | // best way to get all the passed arguments, by |
||
| 213 | // reference. 5.4 and up lets us limit it to 1 frame. |
||
| 214 | (version_compare(PHP_VERSION, '5.4.0') >= 0 ? |
||
| 215 | " \$_HOOK_arguments = debug_backtrace(false, 1);\n" : |
||
| 216 | " \$_HOOK_arguments = debug_backtrace(false);\n" |
||
| 217 | ) |
||
| 218 | ." \$_HOOK_arguments = \$_HOOK_arguments[0]['args'];\n" |
||
| 219 | ) |
||
| 220 | ) |
||
| 221 | ." \$_HOOK_function = array(\$this->_hookObject, '$fname');\n" |
||
| 222 | ." \$_HOOK_data = array();\n" |
||
| 223 | ." \\SciActive\\Hook::runCallbacks(\$this->_hookPrefix.'$fname', \$_HOOK_arguments, 'before', \$this->_hookObject, \$_HOOK_function, \$_HOOK_data);\n" |
||
| 224 | ." if (\$_HOOK_arguments !== false) {\n" |
||
| 225 | ." \$_HOOK_return = call_user_func_array(\$_HOOK_function, \$_HOOK_arguments);\n" |
||
| 226 | ." if ((object) \$_HOOK_return === \$_HOOK_return && get_class(\$_HOOK_return) === '$className')\n" |
||
| 227 | ." \\SciActive\\Hook::hookObject(\$_HOOK_return, '$prefix', false);\n" |
||
| 228 | ." \$_HOOK_return = array(\$_HOOK_return);\n" |
||
| 229 | ." \\SciActive\\Hook::runCallbacks(\$this->_hookPrefix.'$fname', \$_HOOK_return, 'after', \$this->_hookObject, \$_HOOK_function, \$_HOOK_data);\n" |
||
| 230 | ." if ((array) \$_HOOK_return === \$_HOOK_return)\n" |
||
| 231 | ." return \$_HOOK_return[0];\n" |
||
| 232 | ." }\n" |
||
| 233 | ."}\n\n"; |
||
| 234 | } |
||
| 235 | unset($curMethod); |
||
| 236 | // Build a HookOverride class. |
||
| 237 | $include = str_replace(array('_NAMEHERE_', '//#CODEHERE#', '<?php', '?>'), array($className, $code, '', ''), Hook::$hookFile); |
||
| 238 | eval($include); |
||
| 239 | } |
||
| 240 | |||
| 241 | eval('$object = new \SciActive\HookOverride_'.$className.' ($object, $prefix);'); |
||
| 242 | return true; |
||
| 243 | } |
||
| 244 | |||
| 306 |