Complex classes like SS_Backtrace 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 SS_Backtrace, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | */ |
||
| 12 | class Backtrace { |
||
| 13 | use Configurable; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var array Replaces all arguments with a '<filtered>' string, |
||
| 17 | * mostly for security reasons. Use string values for global functions, |
||
| 18 | * and array notation for class methods. |
||
| 19 | * PHP's debug_backtrace() doesn't allow to inspect the argument names, |
||
| 20 | * so all arguments of the provided functions will be filtered out. |
||
| 21 | */ |
||
| 22 | private static $ignore_function_args = array( |
||
| 23 | 'mysql_connect', |
||
| 24 | 'mssql_connect', |
||
| 25 | 'pg_connect', |
||
| 26 | array('PDO', '__construct'), |
||
| 27 | array('mysqli', 'mysqli'), |
||
| 28 | array('mysqli', 'select_db'), |
||
| 29 | array('SilverStripe\\ORM\\DB', 'connect'), |
||
| 30 | array('SilverStripe\\Security\\Security', 'check_default_admin'), |
||
| 31 | array('SilverStripe\\Security\\Security', 'encrypt_password'), |
||
| 32 | array('SilverStripe\\Security\\Security', 'setDefaultAdmin'), |
||
| 33 | array('SilverStripe\\ORM\\DB', 'createDatabase'), |
||
| 34 | array('SilverStripe\\Security\\Member', 'checkPassword'), |
||
| 35 | array('SilverStripe\\Security\\Member', 'changePassword'), |
||
| 36 | array('SilverStripe\\Security\\MemberPassword', 'checkPassword'), |
||
| 37 | array('SilverStripe\\Security\\PasswordValidator', 'validate'), |
||
| 38 | array('SilverStripe\\Security\\PasswordEncryptor_PHPHash', 'encrypt'), |
||
| 39 | array('SilverStripe\\Security\\PasswordEncryptor_PHPHash', 'salt'), |
||
| 40 | array('SilverStripe\\Security\\PasswordEncryptor_LegacyPHPHash', 'encrypt'), |
||
| 41 | array('SilverStripe\\Security\\PasswordEncryptor_LegacyPHPHash', 'salt'), |
||
| 42 | array('SilverStripe\\Security\\PasswordEncryptor_MySQLPassword', 'encrypt'), |
||
| 43 | array('SilverStripe\\Security\\PasswordEncryptor_MySQLPassword', 'salt'), |
||
| 44 | array('SilverStripe\\Security\\PasswordEncryptor_MySQLOldPassword', 'encrypt'), |
||
| 45 | array('SilverStripe\\Security\\PasswordEncryptor_MySQLOldPassword', 'salt'), |
||
| 46 | array('SilverStripe\\Security\\PasswordEncryptor_Blowfish', 'encrypt'), |
||
| 47 | array('SilverStripe\\Security\\PasswordEncryptor_Blowfish', 'salt'), |
||
| 48 | ); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Return debug_backtrace() results with functions filtered |
||
| 52 | * specific to the debugging system, and not the trace. |
||
| 53 | * |
||
| 54 | * @param null|array $ignoredFunctions If an array, filter these functions out of the trace |
||
| 55 | * @return array |
||
| 56 | */ |
||
| 57 | public static function filtered_backtrace($ignoredFunctions = null) { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Filter a backtrace so that it doesn't show the calls to the |
||
| 63 | * debugging system, which is useless information. |
||
| 64 | * |
||
| 65 | * @param array $bt Backtrace to filter |
||
| 66 | * @param null|array $ignoredFunctions List of extra functions to filter out |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | public static function filter_backtrace($bt, $ignoredFunctions = null) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Render or return a backtrace from the given scope. |
||
| 122 | * |
||
| 123 | * @param mixed $returnVal |
||
| 124 | * @param bool $ignoreAjax |
||
| 125 | * @param array $ignoredFunctions |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | public static function backtrace($returnVal = false, $ignoreAjax = false, $ignoredFunctions = null) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Return the full function name. If showArgs is set to true, a string representation of the arguments will be |
||
| 141 | * shown |
||
| 142 | * |
||
| 143 | * @param Object $item |
||
| 144 | * @param bool $showArgs |
||
| 145 | * @param int $argCharLimit |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public static function full_func_name($item, $showArgs = false, $argCharLimit = 10000) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Render a backtrace array into an appropriate plain-text or HTML string. |
||
| 173 | * |
||
| 174 | * @param array $bt The trace array, as returned by debug_backtrace() or Exception::getTrace() |
||
| 175 | * @param boolean $plainText Set to false for HTML output, or true for plain-text output |
||
| 176 | * @param array $ignoredFunctions List of functions that should be ignored. If not set, a default is provided |
||
| 177 | * @return string The rendered backtrace |
||
| 178 | */ |
||
| 179 | public static function get_rendered_backtrace($bt, $plainText = false, $ignoredFunctions = null) { |
||
| 207 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.