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 ) { |
|
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 ) { |
|
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 ) { |
||
62 | $file = wp_normalize_path( $file ); |
||
63 | |||
64 | if ( ! $this->is_absolute_path( $file ) ) { |
||
65 | $file = $this->find_absolute_plugin_path( $file, $directories_to_check ); |
||
66 | if ( ! isset( $file ) ) { |
||
67 | return false; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | // We need the real path for consistency with __DIR__ paths. |
||
72 | $file = $this->get_real_path( $file ); |
||
73 | |||
74 | // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged |
||
75 | $directory = @is_file( $file ) ? dirname( $file ) : $file; |
||
76 | if ( ! @is_file( $directory . '/vendor/composer/jetpack_autoload_classmap.php' ) ) { |
||
77 | return false; |
||
78 | } |
||
79 | // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged |
||
80 | |||
81 | return $directory; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Fetches an array of normalized paths keyed by the constant they came from. |
||
86 | * |
||
87 | * @return string[] The normalized paths keyed by the constant. |
||
88 | */ |
||
89 | private function get_normalized_constants() { |
||
112 | |||
113 | /** |
||
114 | * Indicates whether or not a path is absolute. |
||
115 | * |
||
116 | * @param string $path The path to check. |
||
117 | * @return bool True if the path is absolute, otherwise false. |
||
118 | */ |
||
119 | private function is_absolute_path( $path ) { |
||
132 | |||
133 | /** |
||
134 | * Given a file and a list of directories to check, this method will try to figure out |
||
135 | * the absolute path to the file in question. |
||
136 | * |
||
137 | * @param string $normalized_path The normalized path to the plugin or theme file to resolve. |
||
138 | * @param array $directories_to_check The directories we should check for the file if it isn't an absolute path. |
||
139 | * |
||
140 | * @return string|null The absolute path to the plugin file, otherwise null. |
||
141 | */ |
||
142 | private function find_absolute_plugin_path( $normalized_path, $directories_to_check ) { |
||
158 | |||
159 | /** |
||
160 | * Given a path this will figure out the real path that we should be using. |
||
161 | * |
||
162 | * @param string $path The path to resolve. |
||
163 | * |
||
164 | * @return string The resolved path. |
||
165 | */ |
||
166 | private function get_real_path( $path ) { |
||
182 | } |
||
183 |