Completed
Push — add/autoloader-path-cache ( 9c201d...c8c9a1 )
by
unknown
09:15
created

Path_Processor   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 151
Duplicated Lines 23.18 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 35
loc 151
rs 10
c 0
b 0
f 0
wmc 25
lcom 0
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A tokenize_path_constants() 15 15 3
A untokenize_path_constants() 17 17 3
A find_directory_with_autoloader() 0 22 5
A get_normalized_constants() 0 23 4
A is_absolute_path() 0 13 5
A find_plugin_directory() 3 16 5

How to fix   Duplicated Code   

Duplicated Code

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
2
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * This class handles dealing with paths for the autoloader.
6
 */
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 ) {
62
		$file = wp_normalize_path( $file );
63
64
		// We need an absolute path to the plugin directory to check for an autoloader.
65
		if ( $this->is_absolute_path( $file ) ) {
66
			// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
67
			$directory = @is_file( $file ) ? dirname( $file ) : $file;
68
		} else {
69
			array_walk( $directories_to_check, 'wp_normalize_path' );
70
			$directory = $this->find_plugin_directory( $file, $directories_to_check );
71
			if ( ! isset( $directory ) ) {
72
				return false;
73
			}
74
		}
75
76
		// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
77
		if ( ! @is_file( $directory . '/vendor/composer/jetpack_autoload_classmap.php' ) ) {
78
			return false;
79
		}
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() {
90
		$raw_constants = array(
91
			// Order the constants from most-specific to least-specific.
92
			'WP_PLUGIN_DIR',
93
			'WPMU_PLUGIN_DIR',
94
			'WP_CONTENT_DIR',
95
			'ABSPATH',
96
		);
97
98
		$constants = array();
99
		foreach ( $raw_constants as $raw ) {
100
			if ( ! defined( $raw ) ) {
101
				continue;
102
			}
103
104
			$path = wp_normalize_path( constant( $raw ) );
105
			if ( isset( $path ) ) {
106
				$constants[ $raw ] = $path;
107
			}
108
		}
109
110
		return $constants;
111
	}
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 ) {
120
		if ( 0 === strlen( $path ) || '.' === $path[0] ) {
121
			return false;
122
		}
123
124
		// Absolute paths on Windows may begin with a drive letter.
125
		if ( preg_match( '/^[a-zA-Z]:[\/\\\\]/', $path ) ) {
126
			return true;
127
		}
128
129
		// A path starting with / or \ is absolute; anything else is relative.
130
		return ( '/' === $path[0] || '\\' === $path[0] );
131
	}
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 $file 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
	 * @return string|null The absolute path to the plugin directory, otherwise null.
140
	 */
141
	private function find_plugin_directory( $file, $directories_to_check ) {
142
		// We're only able to find the absolute path for plugin/theme PHP files.
143 View Code Duplication
		if ( ! is_string( $file ) || '.php' !== substr( $file, -4 ) ) {
144
			return null;
145
		}
146
147
		foreach ( $directories_to_check as $check_dir ) {
148
			$check = dirname( trailingslashit( $check_dir ) . $file );
149
			// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
150
			if ( @is_dir( $check ) ) {
151
				return $check;
152
			}
153
		}
154
155
		return null;
156
	}
157
}
158