Complex classes like Crypt 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 Crypt, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Crypt { |
||
| 13 | use \PHPDaemon\Traits\ClassWatchdog; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Generate keccak hash for string with salt |
||
| 17 | * @param string $str Data |
||
| 18 | * @param string $salt Salt |
||
| 19 | * @param boolean $plain Is plain text? |
||
| 20 | * @return string |
||
| 21 | */ |
||
| 22 | public static function hash($str, $salt = '', $plain = false) { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Returns string of pseudo random characters |
||
| 53 | * @param integer $len Length of desired string |
||
|
|
|||
| 54 | * @param string $chars String of allowed characters |
||
| 55 | * @param callable $cb Callback |
||
| 56 | * @param integer $pri Priority of EIO operation |
||
| 57 | * @param boolean $hang If true, we shall use /dev/random instead of /dev/urandom and it may cause a delay |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public static function randomString($len = null, $chars = null, $cb = null, $pri = 0, $hang = false) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns the character at index $idx in $str in constant time |
||
| 112 | * @param string $str String |
||
| 113 | * @param integer $idx Index |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public static function stringIdx($str, $idx) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns string of pseudo random bytes |
||
| 134 | * @param integer $len Length of desired string |
||
| 135 | * @param callable $cb Callback |
||
| 136 | * @param integer $pri Priority of EIO operation |
||
| 137 | * @param boolean $hang If true, we shall use /dev/random instead of /dev/urandom and it may cause a delay |
||
| 138 | * @return integer |
||
| 139 | */ |
||
| 140 | public static function randomBytes($len, $cb, $pri = 0, $hang = false) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns array of pseudo random integers of machine-dependent size |
||
| 155 | * @param integer $numInts Number of integers |
||
| 156 | * @param callable $cb Callback |
||
| 157 | * @param integer $pri Priority of EIO operation |
||
| 158 | * @param boolean $hang If true, we shall use /dev/random instead of /dev/urandom and it may cause a delay |
||
| 159 | * @return integer |
||
| 160 | */ |
||
| 161 | public static function randomInts($numInts, $cb, $pri = 0, $hang = false) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns array of pseudo random 32-bit integers |
||
| 183 | * @param integer $numInts Number of integers |
||
| 184 | * @param callable $cb Callback |
||
| 185 | * @param integer $pri Priority of EIO operation |
||
| 186 | * @param boolean $hang If true, we shall use /dev/random instead of /dev/urandom and it may cause a delay |
||
| 187 | * @return integer |
||
| 188 | */ |
||
| 189 | public static function randomInts32($numInts, $cb, $pri = 0, $hang = false) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns the smallest bit mask of all 1s such that ($toRepresent & mask) = $toRepresent |
||
| 211 | * @param integer $toRepresent must be an integer greater than or equal to 1 |
||
| 212 | * @return integer |
||
| 213 | */ |
||
| 214 | protected static function getMinimalBitMask($toRepresent) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Compare strings |
||
| 227 | * @param string $a String 1 |
||
| 228 | * @param string $b String 2 |
||
| 229 | * @return boolean Equal? |
||
| 230 | */ |
||
| 231 | public static function compareStrings($a, $b) { |
||
| 243 | } |
||
| 244 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.