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 Path_Processor { |
||
| 8 | /** |
||
| 9 | * Given a path this will replace any of the path constants with a token to represent it. |
||
| 10 | * |
||
| 11 | * @param string $path The path we want to process. |
||
| 12 | * @return string The tokenized path. |
||
| 13 | */ |
||
| 14 | View Code Duplication | public function tokenize_path_constants( $path ) { |
|
| 30 | |||
| 31 | /** |
||
| 32 | * Given a path this will replace any of the path constant tokens with the expanded path. |
||
| 33 | * |
||
| 34 | * @param string $path The path we want to process. |
||
| 35 | * @return string The expanded path. |
||
| 36 | */ |
||
| 37 | View Code Duplication | public function untokenize_path_constants( $path ) { |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Given a file and an array of places it might be, this will find the absolute path and return it. |
||
| 56 | * |
||
| 57 | * @param string $file The plugin or theme file to resolve. |
||
| 58 | * @param array $directories_to_check The directories we should check for the file if it isn't an absolute path. |
||
| 59 | * @return string|false Returns the absolute path to the directory, otherwise false. |
||
| 60 | */ |
||
| 61 | public function find_directory_with_autoloader( $file, $directories_to_check ) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Fetches an array of paths keyed by the constant they came from. |
||
| 85 | * |
||
| 86 | * @return string[] The paths keyed by the constant. |
||
| 87 | */ |
||
| 88 | private static function get_path_constants() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Given a file and a list of directories to check, this method will try to figure out |
||
| 115 | * the absolute path to the file in question. |
||
| 116 | * |
||
| 117 | * @param string $file The plugin or theme file to resolve. |
||
| 118 | * @param array $directories_to_check The directories we should check for the file if it isn't an absolute path. |
||
| 119 | * @return string|null The absolute path to the plugin directory, otherwise null. |
||
| 120 | */ |
||
| 121 | private function find_plugin_directory( $file, $directories_to_check ) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Normalizes the given path or paths so that it can be consistently |
||
| 141 | * handled throughout the autoloader. |
||
| 142 | * |
||
| 143 | * @param string|string[] $paths The path or paths we want to normalize. |
||
| 144 | * @return string|string[] The normalized path or paths. |
||
| 145 | */ |
||
| 146 | private static function normalize( $paths ) { |
||
| 150 | } |
||
| 151 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.