Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 7 | class PHP_Autoloader { |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Registers the autoloader with PHP so that it can begin autoloading classes. |
||
| 11 | * |
||
| 12 | * @param Version_Loader $version_loader The class loader to use in the autoloader. |
||
| 13 | */ |
||
| 14 | public function register_autoloader( $version_loader ) { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Unregisters the active autoloader so that it will no longer autoload classes. |
||
| 28 | */ |
||
| 29 | public function unregister_autoloader() { |
||
| 30 | // Remove any v2 autoloader that we've already registered. |
||
| 31 | $autoload_chain = spl_autoload_functions(); |
||
| 32 | foreach ( $autoload_chain as $autoloader ) { |
||
| 33 | // We can identify a v2 autoloader using the namespace. |
||
| 34 | $namespace_check = null; |
||
|
|
|||
| 35 | |||
| 36 | // Functions are recorded as strings. |
||
| 37 | if ( is_string( $autoloader ) ) { |
||
| 38 | $namespace_check = $autoloader; |
||
| 39 | } elseif ( is_array( $autoloader ) && is_string( $autoloader[0] ) ) { |
||
| 40 | // Static method calls have the class as the first array element. |
||
| 41 | $namespace_check = $autoloader[0]; |
||
| 42 | } else { |
||
| 43 | // Since the autoloader has only ever been a function or a static method we don't currently need to check anything else. |
||
| 44 | continue; |
||
| 45 | } |
||
| 46 | |||
| 47 | // Check for the namespace without the generated suffix. |
||
| 48 | if ( 'Automattic\\Jetpack\\Autoloader\\jp' === substr( $namespace_check, 0, 32 ) ) { |
||
| 49 | spl_autoload_unregister( $autoloader ); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | // Clear the global now that the autoloader has been unregistered. |
||
| 54 | global $jetpack_autoloader_loader; |
||
| 55 | $jetpack_autoloader_loader = null; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Loads a class file if one could be found. |
||
| 60 | * |
||
| 61 | * Note: This function is static so that the autoloader can be easily unregistered. If |
||
| 62 | * it was a class method we would have to unwrap the object to check the namespace. |
||
| 63 | * |
||
| 64 | * @param string $class_name The name of the class to autoload. |
||
| 65 | * |
||
| 66 | * @return bool Indicates whether or not a class file was loaded. |
||
| 67 | */ |
||
| 68 | View Code Duplication | public static function load_class( $class_name ) { |
|
| 82 | } |
||
| 83 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.