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
Pull Request — master (#936)
by Tom
02:41
created

Excludes::get_default_excludes()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 21
rs 9.3143
cc 3
eloc 7
nc 2
nop 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
107
		return $excludes;
108
109
	}
110
111
	/**
112
	 * Get the user defined excludes.
113
	 *
114
	 * @return array The array of excludes.
115
	 */
116
	public function get_user_excludes() {
117
118
		$excludes = $this->excludes;
119
120
		// If path() is inside root(), exclude it
121
		if ( strpos( Path::get_path(), Path::get_root() ) !== false && Path::get_root() !== Path::get_path() ) {
122
			array_unshift( $excludes, trailingslashit( Path::get_path() ) );
123
		}
124
125
		return $this->normalize( $excludes );
126
	}
127
128
	/**
129
	 * Get the array of default excludes.
130
	 *
131
	 * @return array The array of excludes.
132
	 */
133
	public function get_default_excludes() {
134
135
		$excludes = array();
136
137
		// Back compat with the old Constant
138
		if ( defined( 'HMBKP_EXCLUDE' ) && HMBKP_EXCLUDE ) {
139
			$excludes = explode( ',', implode( ',', (array) HMBKP_EXCLUDE ) );
140
		}
141
142
		$excludes = array_merge( $this->default_excludes, $excludes );
143
144
		/**
145
		 * Allow the default excludes list to be modified.
146
		 *
147
		 * @param $excludes The array of exclude rules.
148
		 */
149
		$excludes = apply_filters( 'hmbkp_default_excludes', $excludes );
150
151
		return $this->normalize( $excludes );
152
153
	}
154
155
	/**
156
	 * normalise the exclude rules so they are ready to work with.
157
	 *
158
	 * @param  array $excludes The array of exclude rules to normalise.
159
	 *
160
	 * @return array           The array of normalised rules.
161
	 */
162
	public function normalize( $excludes ) {
163
164
		$excludes = array_map( 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
		}, $excludes );
177
178
		// Remove duplicate or empty rules
179
		$excludes = array_unique( $excludes );
180
		$excludes = array_filter( $excludes );
181
182
		return $excludes;
183
184
	}
185
186
}
187