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 ) { |
|
| 15 | $path = wp_normalize_path( $path ); |
||
| 16 | |||
| 17 | $constants = $this->get_normalized_constants(); |
||
| 18 | foreach ( $constants as $constant => $constant_path ) { |
||
| 19 | $len = strlen( $constant_path ); |
||
| 20 | if ( substr( $path, 0, $len ) !== $constant_path ) { |
||
| 21 | continue; |
||
| 22 | } |
||
| 23 | |||
| 24 | return substr_replace( $path, '{{' . $constant . '}}', 0, $len ); |
||
| 25 | } |
||
| 26 | |||
| 27 | return $path; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Given a path this will replace any of the path constant tokens with the expanded path. |
||
| 32 | * |
||
| 33 | * @param string $tokenized_path The path we want to process. |
||
| 34 | * @return string The expanded path. |
||
| 35 | */ |
||
| 36 | View Code Duplication | public function untokenize_path_constants( $tokenized_path ) { |
|
| 37 | $tokenized_path = wp_normalize_path( $tokenized_path ); |
||
| 38 | |||
| 39 | $constants = $this->get_normalized_constants(); |
||
| 40 | foreach ( $constants as $constant => $constant_path ) { |
||
| 41 | $constant = '{{' . $constant . '}}'; |
||
| 42 | |||
| 43 | $len = strlen( $constant ); |
||
| 44 | if ( substr( $tokenized_path, 0, $len ) !== $constant ) { |
||
| 45 | continue; |
||
| 46 | } |
||
| 47 | |||
| 48 | return substr_replace( $tokenized_path, $constant_path, 0, $len ); |
||
| 49 | } |
||
| 50 | |||
| 51 | return $tokenized_path; |
||
| 52 | } |
||
| 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 normalized paths keyed by the constant they came from. |
||
| 85 | * |
||
| 86 | * @return string[] The normalized paths keyed by the constant. |
||
| 87 | */ |
||
| 88 | private function get_normalized_constants() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Indicates whether or not a path is absolute. |
||
| 114 | * |
||
| 115 | * @param string $path The path to check. |
||
| 116 | * @return bool True if the path is absolute, otherwise false. |
||
| 117 | */ |
||
| 118 | private function is_absolute_path( $path ) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Given a file and a list of directories to check, this method will try to figure out |
||
| 134 | * the absolute path to the file in question. |
||
| 135 | * |
||
| 136 | * @param string $normalized_path The normalized path to the plugin or theme file to resolve. |
||
| 137 | * @param array $directories_to_check The directories we should check for the file if it isn't an absolute path. |
||
| 138 | * |
||
| 139 | * @return string|null The absolute path to the plugin directory, otherwise null. |
||
| 140 | */ |
||
| 141 | private function find_plugin_directory( $normalized_path, $directories_to_check ) { |
||
| 157 | } |
||
| 158 |