| Total Complexity | 78 |
| Total Lines | 543 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PEAR often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PEAR, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 87 | class PEAR |
||
| 88 | { |
||
| 89 | // {{{ properties |
||
| 90 | /** |
||
| 91 | * Whether to enable internal debug messages. |
||
| 92 | * |
||
| 93 | * @var bool |
||
| 94 | */ |
||
| 95 | public $_debug = false; |
||
| 96 | /** |
||
| 97 | * Default error mode for this object. |
||
| 98 | * |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | public $_default_error_mode = null; |
||
| 102 | /** |
||
| 103 | * Default error options used for this object when error mode |
||
| 104 | * is PEAR_ERROR_TRIGGER. |
||
| 105 | * |
||
| 106 | * @var int |
||
| 107 | */ |
||
| 108 | public $_default_error_options = null; |
||
| 109 | /** |
||
| 110 | * Default error handler (callback) for this object, if error mode is |
||
| 111 | * PEAR_ERROR_CALLBACK. |
||
| 112 | * |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | public $_default_errorHandler = ''; |
||
| 116 | /** |
||
| 117 | * Which class to use for error objects. |
||
| 118 | * |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | public $_error_class = 'PEAR_Error'; |
||
| 122 | /** |
||
| 123 | * An array of expected errors. |
||
| 124 | * |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | public $_expected_errors = []; |
||
| 128 | // }}} |
||
| 129 | |||
| 130 | // {{{ constructor |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Constructor. Registers this object in |
||
| 134 | * $_PEAR_destructor_object_list for destructor emulation if a |
||
| 135 | * destructor object exists. |
||
| 136 | * |
||
| 137 | * @param null $error_class (optional) which class to use for |
||
| 138 | * error objects, defaults to PEAR_Error. |
||
| 139 | */ |
||
| 140 | public function __construct($error_class = null) |
||
| 141 | { |
||
| 142 | $classname = get_class($this); |
||
| 143 | if ($this->_debug) { |
||
| 144 | print "PEAR constructor called, class=$classname\n"; |
||
| 145 | } |
||
| 146 | if (null !== $error_class) { |
||
| 147 | $this->_error_class = $error_class; |
||
| 148 | } |
||
| 149 | while ($classname) { |
||
| 150 | $destructor = "_$classname"; |
||
| 151 | if (method_exists($this, $destructor)) { |
||
| 152 | global $_PEAR_destructor_object_list; |
||
| 153 | $_PEAR_destructor_object_list[] = &$this; |
||
| 154 | break; |
||
| 155 | } |
||
| 156 | $classname = get_parent_class($classname); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | // }}} |
||
| 161 | // {{{ destructor |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Destructor (the emulated type of...). Does nothing right now, |
||
| 165 | * but is included for forward compatibility, so subclass |
||
| 166 | * destructors should always call it. |
||
| 167 | * |
||
| 168 | * See the note in the class desciption about output from |
||
| 169 | * destructors. |
||
| 170 | */ |
||
| 171 | public function _PEAR(): void |
||
| 172 | { |
||
| 173 | if ($this->_debug) { |
||
| 174 | printf("PEAR destructor called, class=%s\n", get_class($this)); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | // }}} |
||
| 179 | // {{{ getStaticProperty() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * If you have a class that's mostly/entirely static, and you need static |
||
| 183 | * properties, you can use this method to simulate them. Eg. in your method(s) |
||
| 184 | * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar'); |
||
| 185 | * You MUST use a reference, or they will not persist! |
||
| 186 | * |
||
| 187 | * @param string $class The calling classname, to prevent clashes |
||
| 188 | * @param string $var The variable to retrieve. |
||
| 189 | * @return mixed A reference to the variable. If not set it will be |
||
| 190 | * auto initialised to NULL. |
||
| 191 | */ |
||
| 192 | public function &getStaticProperty(string $class, string $var) |
||
| 193 | { |
||
| 194 | static $properties; |
||
| 195 | |||
| 196 | return $properties[$class][$var]; |
||
| 197 | } |
||
| 198 | |||
| 199 | // }}} |
||
| 200 | // {{{ registerShutdownFunc() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Use this function to register a shutdown method for static |
||
| 204 | * classes. |
||
| 205 | * |
||
| 206 | * @param mixed $func The function name (or array of class/method) to call |
||
| 207 | * @param mixed $args The arguments to pass to the function |
||
| 208 | */ |
||
| 209 | public function registerShutdownFunc($func, $args = []): void |
||
| 210 | { |
||
| 211 | $GLOBALS['_PEAR_shutdown_funcs'][] = [$func, $args]; |
||
| 212 | } |
||
| 213 | |||
| 214 | // }}} |
||
| 215 | // {{{ isError() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Tell whether a value is a PEAR error. |
||
| 219 | * |
||
| 220 | * @param mixed $data the value to test |
||
| 221 | * @param null $code if $data is an error object, return true |
||
| 222 | * only if $code is a string and |
||
| 223 | * $obj->getMessage() == $code or |
||
| 224 | * $code is an integer and $obj->getCode() == $code |
||
| 225 | * @return bool true if parameter is an error |
||
| 226 | */ |
||
| 227 | public function isError($data, $code = null): bool |
||
| 228 | { |
||
| 229 | if (is_a($data, 'PEAR_Error')) { |
||
| 230 | if (null === $code) { |
||
| 231 | return true; |
||
| 232 | } elseif (is_string($code)) { |
||
| 233 | return $data->getMessage() == $code; |
||
| 234 | } |
||
| 235 | |||
| 236 | return $data->getCode() == $code; |
||
| 237 | } |
||
| 238 | |||
| 239 | return false; |
||
| 240 | } |
||
| 241 | |||
| 242 | // }}} |
||
| 243 | // {{{ setErrorHandling() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Sets how errors generated by this object should be handled. |
||
| 247 | * Can be invoked both in objects and statically. If called |
||
| 248 | * statically, setErrorHandling sets the default behaviour for all |
||
| 249 | * PEAR objects. If called in an object, setErrorHandling sets |
||
| 250 | * the default behaviour for that object. |
||
| 251 | * |
||
| 252 | * @param null $mode |
||
| 253 | * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, |
||
| 254 | * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE, |
||
| 255 | * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION. |
||
| 256 | * |
||
| 257 | * @param mixed $options |
||
| 258 | * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one |
||
| 259 | * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). |
||
| 260 | * |
||
| 261 | * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected |
||
| 262 | * to be the callback function or method. A callback |
||
| 263 | * function is a string with the name of the function, a |
||
| 264 | * callback method is an array of two elements: the element |
||
| 265 | * at index 0 is the object, and the element at index 1 is |
||
| 266 | * the name of the method to call in the object. |
||
| 267 | * |
||
| 268 | * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is |
||
| 269 | * a printf format string used when printing the error |
||
| 270 | * message. |
||
| 271 | * |
||
| 272 | * @see PEAR_ERROR_RETURN |
||
| 273 | * @see PEAR_ERROR_PRINT |
||
| 274 | * @see PEAR_ERROR_TRIGGER |
||
| 275 | * @see PEAR_ERROR_DIE |
||
| 276 | * @see PEAR_ERROR_CALLBACK |
||
| 277 | * @see PEAR_ERROR_EXCEPTION |
||
| 278 | * |
||
| 279 | * @since PHP 4.0.5 |
||
| 280 | */ |
||
| 281 | public function setErrorHandling($mode = null, $options = null): void |
||
| 282 | { |
||
| 283 | if (isset($this) && $this instanceof self) { |
||
| 284 | $setmode = &$this->_default_error_mode; |
||
| 285 | $setoptions = &$this->_default_error_options; |
||
| 286 | } else { |
||
| 287 | $setmode = &$GLOBALS['_PEAR_default_error_mode']; |
||
| 288 | $setoptions = &$GLOBALS['_PEAR_default_error_options']; |
||
| 289 | } |
||
| 290 | |||
| 291 | switch ($mode) { |
||
| 292 | case PEAR_ERROR_EXCEPTION: |
||
| 293 | case PEAR_ERROR_RETURN: |
||
| 294 | case PEAR_ERROR_PRINT: |
||
| 295 | case PEAR_ERROR_TRIGGER: |
||
| 296 | case PEAR_ERROR_DIE: |
||
| 297 | case null: |
||
| 298 | $setmode = $mode; |
||
| 299 | $setoptions = $options; |
||
| 300 | break; |
||
| 301 | case PEAR_ERROR_CALLBACK: |
||
| 302 | $setmode = $mode; |
||
| 303 | // class/object method callback |
||
| 304 | if (is_callable($options)) { |
||
| 305 | $setoptions = $options; |
||
| 306 | } else { |
||
| 307 | trigger_error('invalid error callback', E_USER_WARNING); |
||
| 308 | } |
||
| 309 | break; |
||
| 310 | default: |
||
| 311 | trigger_error('invalid error mode', E_USER_WARNING); |
||
| 312 | break; |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | // }}} |
||
| 317 | // {{{ expectError() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * This method is used to tell which errors you expect to get. |
||
| 321 | * Expected errors are always returned with error mode |
||
| 322 | * PEAR_ERROR_RETURN. Expected error codes are stored in a stack, |
||
| 323 | * and this method pushes a new element onto it. The list of |
||
| 324 | * expected errors are in effect until they are popped off the |
||
| 325 | * stack with the popExpect() method. |
||
| 326 | * |
||
| 327 | * Note that this method can not be called statically |
||
| 328 | * |
||
| 329 | * @param mixed $code a single error code or an array of error codes to expect |
||
| 330 | * |
||
| 331 | * @return int the new depth of the "expected errors" stack |
||
| 332 | */ |
||
| 333 | public function expectError($code = '*'): int |
||
| 334 | { |
||
| 335 | if (is_array($code)) { |
||
| 336 | $this->_expected_errors[] = $code; |
||
| 337 | } else { |
||
| 338 | $this->_expected_errors[] = [$code]; |
||
| 339 | } |
||
| 340 | |||
| 341 | return count($this->_expected_errors); |
||
| 342 | } |
||
| 343 | |||
| 344 | // }}} |
||
| 345 | // {{{ popExpect() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * This method pops one element off the expected error codes |
||
| 349 | * stack. |
||
| 350 | * |
||
| 351 | * @return array the list of error codes that were popped |
||
| 352 | */ |
||
| 353 | public function popExpect(): array |
||
| 354 | { |
||
| 355 | return array_pop($this->_expected_errors); |
||
| 356 | } |
||
| 357 | |||
| 358 | // }}} |
||
| 359 | // {{{ _checkDelExpect() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * This method checks unsets an error code if available |
||
| 363 | * |
||
| 364 | * @param mixed $error_code |
||
| 365 | * @return bool true if the error code was unset, false otherwise |
||
| 366 | * @since PHP 4.3.0 |
||
| 367 | */ |
||
| 368 | public function _checkDelExpect($error_code): bool |
||
| 369 | { |
||
| 370 | $deleted = false; |
||
| 371 | |||
| 372 | foreach ($this->_expected_errors as $key => $error_array) { |
||
| 373 | if (in_array($error_code, $error_array, true)) { |
||
| 374 | unset($this->_expected_errors[$key][array_search($error_code, $error_array, true)]); |
||
| 375 | $deleted = true; |
||
| 376 | } |
||
| 377 | |||
| 378 | // clean up empty arrays |
||
| 379 | if (0 == count($this->_expected_errors[$key])) { |
||
| 380 | unset($this->_expected_errors[$key]); |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | return $deleted; |
||
| 385 | } |
||
| 386 | |||
| 387 | // }}} |
||
| 388 | // {{{ delExpect() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * This method deletes all occurences of the specified element from |
||
| 392 | * the expected error codes stack. |
||
| 393 | * |
||
| 394 | * @param mixed $error_code error code that should be deleted |
||
| 395 | * @return bool|\PEAR_Error list of error codes that were deleted or error |
||
| 396 | * @since PHP 4.3.0 |
||
| 397 | */ |
||
| 398 | public function delExpect($error_code) |
||
| 425 | } |
||
| 426 | |||
| 427 | // }}} |
||
| 428 | // {{{ raiseError() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * This method is a wrapper that returns an instance of the |
||
| 432 | * configured error class with this object's default error |
||
| 433 | * handling applied. If the $mode and $options parameters are not |
||
| 434 | * specified, the object's defaults are used. |
||
| 435 | * |
||
| 436 | * @param mixed $message a text error message or a PEAR error object |
||
| 437 | * |
||
| 438 | * @param null $code a numeric error code (it is up to your class |
||
| 439 | * to define these if you want to use codes) |
||
| 440 | * |
||
| 441 | * @param null $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, |
||
| 442 | * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE, |
||
| 443 | * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION. |
||
| 444 | * |
||
| 445 | * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter |
||
| 446 | * specifies the PHP-internal error level (one of |
||
| 447 | * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). |
||
| 448 | * If $mode is PEAR_ERROR_CALLBACK, this |
||
| 449 | * parameter specifies the callback function or |
||
| 450 | * method. In other error modes this parameter |
||
| 451 | * is ignored. |
||
| 452 | * |
||
| 453 | * @param null $userinfo If you need to pass along for example debug |
||
| 454 | * information, this parameter is meant for that. |
||
| 455 | * |
||
| 456 | * @param null $error_class The returned error object will be |
||
| 457 | * instantiated from this class, if specified. |
||
| 458 | * |
||
| 459 | * @param bool $skipmsg If true, raiseError will only pass error codes, |
||
| 460 | * the error message parameter will be dropped. |
||
| 461 | * |
||
| 462 | * @return PEAR_Error a PEAR error object |
||
| 463 | * @see PEAR::setErrorHandling |
||
| 464 | * @since PHP 4.0.5 |
||
| 465 | */ |
||
| 466 | public function raiseError( |
||
| 510 | } |
||
| 511 | |||
| 512 | // }}} |
||
| 513 | // {{{ throwError() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Simpler form of raiseError with fewer options. In most cases |
||
| 517 | * message, code and userinfo are enough. |
||
| 518 | * |
||
| 519 | * @param null $message |
||
| 520 | * |
||
| 521 | * @param null $code |
||
| 522 | * @param null $userinfo |
||
| 523 | * @return object |
||
| 524 | */ |
||
| 525 | public function throwError( |
||
| 526 | $message = null, $code = null, $userinfo = null |
||
| 527 | ) { |
||
| 528 | if (isset($this) && $this instanceof self) { |
||
| 529 | return $this->raiseError($message, $code, null, null, $userinfo); |
||
| 530 | } |
||
| 531 | |||
| 532 | return self::raiseError($message, $code, null, null, $userinfo); |
||
| 533 | } |
||
| 534 | |||
| 535 | // }}} |
||
| 536 | // {{{ pushErrorHandling() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Push a new error handler on top of the error handler options stack. With this |
||
| 540 | * you can easily override the actual error handler for some code and restore |
||
| 541 | * it later with popErrorHandling. |
||
| 542 | * |
||
| 543 | * @param mixed $mode (same as setErrorHandling) |
||
| 544 | * @param mixed $options (same as setErrorHandling) |
||
| 545 | * |
||
| 546 | * @return bool Always true |
||
| 547 | * |
||
| 548 | * @see PEAR::setErrorHandling |
||
| 549 | */ |
||
| 550 | public function pushErrorHandling($mode, $options = null): bool |
||
| 570 | } |
||
| 571 | |||
| 572 | // }}} |
||
| 573 | // {{{ popErrorHandling() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Pop the last error handler used |
||
| 577 | * |
||
| 578 | * @return bool Always true |
||
| 579 | * |
||
| 580 | * @see PEAR::pushErrorHandling |
||
| 581 | */ |
||
| 582 | public function popErrorHandling(): bool |
||
| 595 | } |
||
| 596 | |||
| 597 | // }}} |
||
| 598 | // {{{ loadExtension() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * OS independant PHP extension load. Remember to take care |
||
| 602 | * on the correct extension name for case sensitive OSes. |
||
| 603 | * |
||
| 604 | * @param string $ext The extension name |
||
| 605 | * @return bool Success or not on the dl() call |
||
| 606 | */ |
||
| 607 | public function loadExtension(string $ext): bool |
||
| 630 | } |
||
| 631 | // }}} |
||
| 632 | } |
||
| 936 |