GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch master (cf5579)
by
unknown
02:50
created

Excludes::is_file_excluded()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
/**
6
 * Manages exclude rules
7
 */
8
class Excludes {
9
10
	/**
11
	 * The array of exclude rules.
12
	 *
13
	 * @var array
14
	 */
15
	private $excludes;
16
17
	/**
18
	 * The array of default exclude rules
19
	 *
20
	 * @var array
21
	 */
22
	private $default_excludes = array(
23
		'.svn',
24
		'_svn',
25
		'CVS',
26
		'_darcs',
27
		'.arch-params',
28
		'.monotone',
29
		'.bzr',
30
		'.git',
31
		'.hg',
32
		'backwpup-*',
33
		'updraft',
34
		'wp-snapshots',
35
		'backupbuddy_backups',
36
		'pb_backupbuddy',
37
		'backup-db',
38
		'Envato-backups',
39
		'managewp',
40
		'backupwordpress-*-backups',
41
	);
42
43
	public function __construct( $excludes = array() ) {
44
		$this->set_excludes( $excludes );
45
	}
46
47
	/**
48
	 * Set the exclude rules.
49
	 *
50
	 * Excludes rules should be a complete or partial directory or file path.
51
	 * Wildcards can be specified with the * character.
52
	 *
53
	 * @param string|array $excludes The list of exclude rules, accepts either
54
	 *                               a comma separated list or an array.
55
	 */
56
	public function set_excludes( $excludes ) {
57
58
		if ( is_string( $excludes ) ) {
59
			$excludes = explode( ',', $excludes );
60
		}
61
62
		$this->excludes = $excludes;
63
64
	}
65
66
	/**
67
	 * Get the excludes
68
	 *
69
	 * Returns any user set excludes as well as the default list.
70
	 *
71
	 * @return array The array of exclude rules.
72
	 */
73
	public function get_excludes() {
74
		return array_merge( $this->get_default_excludes(), $this->get_user_excludes() );
75
	}
76
77
	/**
78
	 * Get the excludes prepared for use with regex.
79
	 *
80
	 * The primary difference being that any wildcard (*) rules are converted to the regex
81
	 * fragment `[\s\S]*?`.
82
	 *
83
	 * @return array The array of exclude rules
84
	 */
85
	public function get_excludes_for_regex() {
86
87
		$excludes = $this->get_excludes();
88
89
		// Prepare the exclude rules
90
		foreach ( $excludes as &$exclude ) {
91
92
			if ( strpos( $exclude, '*' ) !== false ) {
93
94
				// Escape slashes
95
				$exclude = str_replace( '/', '\/', $exclude );
96
97
				// Convert WildCards to regex
98
				$exclude = str_replace( '*', '[\s\S]*?', $exclude );
99
100
				// Wrap in slashes
101
				$exclude = '/' . $exclude . '/';
102
103
			}
104
		}
105
106
		return $excludes;
107
108
	}
109
110
	/**
111
	 * Get the user defined excludes.
112
	 *
113
	 * @return array The array of excludes.
114
	 */
115
	public function get_user_excludes() {
116
117
		$excludes = $this->excludes;
118
119
		// If path() is inside root(), exclude it
120
		if ( strpos( Path::get_path(), Path::get_root() ) !== false && Path::get_root() !== Path::get_path() ) {
121
			array_unshift( $excludes, trailingslashit( Path::get_path() ) );
122
		}
123
124
		return $this->normalize( $excludes );
125
	}
126
127
	/**
128
	 * Get the array of default excludes.
129
	 *
130
	 * @return array The array of excludes.
131
	 */
132
	public function get_default_excludes() {
133
134
		$excludes = array();
135
136
		// Back compat with the old Constant
137
		if ( defined( 'HMBKP_EXCLUDE' ) && HMBKP_EXCLUDE ) {
138
			$excludes = explode( ',', implode( ',', (array) HMBKP_EXCLUDE ) );
139
		}
140
141
		$excludes = array_merge( $this->default_excludes, $excludes );
142
143
		/**
144
		 * Allow the default excludes list to be modified.
145
		 *
146
		 * @param $excludes The array of exclude rules.
147
		 */
148
		$excludes = apply_filters( 'hmbkp_default_excludes', $excludes );
149
150
		return $this->normalize( $excludes );
151
152
	}
153
154
	/**
155
	 * normalise the exclude rules so they are ready to work with.
156
	 *
157
	 * @param  array $excludes The array of exclude rules to normalise.
158
	 *
159
	 * @return array           The array of normalised rules.
160
	 */
161
	public function normalize( $excludes ) {
162
163
		$excludes = array_map(
164
			function( $exclude ) {
165
166
				// Convert absolute paths to relative
167
				$exclude = str_replace( PATH::get_root(), '', wp_normalize_path( $exclude ) );
168
169
				// Trim the slashes
170
				$exclude = trim( $exclude );
171
				$exclude = ltrim( $exclude, '/' );
172
				$exclude = untrailingslashit( $exclude );
173
174
				return $exclude;
175
176
			},
177
		$excludes );
178
179
		// Remove duplicate or empty rules
180
		$excludes = array_unique( $excludes );
181
		$excludes = array_filter( $excludes );
182
183
		return $excludes;
184
185
	}
186
187
	/**
188
	 * Check if a file is excluded.
189
	 * i.e. excluded directly or is in an excluded folder.
190
	 *
191
	 * @param \SplFileInfo $file File to check if it's excluded.
192
	 *
193
	 * @return bool|null         True if file is excluded, false otherwise.
194
	 *                           Null - if it's not a file.
195
	 */
196
	public function is_file_excluded( \SplFileInfo $file ) {
197
198
		$exclude_string    = implode( '|', $this->get_excludes_for_regex() );
199
		$file_path_no_root = str_ireplace( trailingslashit( Path::get_root() ), '', wp_normalize_path( $file->getPathname() ) );
200
201
		if ( $exclude_string && preg_match( '(' . $exclude_string . ')', $file_path_no_root ) ) {
202
			return true;
203
		}
204
205
		return false;
206
	}
207
}
208