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:32
created

Backup_Engine::get_backup_filename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
/**
6
 * The base Backup Engine
7
 *
8
 * Base Backup Engine types should extend this class and call parent::__construct in
9
 * there constructor.
10
 *
11
 * Defines base functionality shared across all types of backups
12
 */
13
abstract class Backup_Engine {
14
15
	/**
16
	 * An array of backup errors.
17
	 *
18
	 * @var array
19
	 */
20
	private $errors = array();
21
22
	/**
23
	 * An array of backup warnings.
24
	 *
25
	 * @var array
26
	 */
27
	private $warnings = array();
28
29
	public function __construct() {
30
31
		/**
32
		 * Raise the `memory_limit` and `max_execution time`
33
		 *
34
		 * Respects the WP_MAX_MEMORY_LIMIT Constant and the `admin_memory_limit`
35
		 * filter.
36
		 */
37
		@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
38
		@set_time_limit( 0 );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
39
40
		// Set a custom error handler so we can track errors
41
		set_error_handler( array( $this, 'error_handler' ) );
42
43
	}
44
45
	/**
46
	 * Backup Engine Types should always implement the `verify_backup` method.
47
	 *
48
	 * @return bool Whether the backup completed successfully or not.
49
	 */
50
	abstract public function verify_backup();
51
52
	/**
53
	 * Get the full filepath to the backup file.
54
	 *
55
	 * @return string The backup filepath.
56
	 */
57
	public function get_backup_filepath() {
58
		return trailingslashit( Path::get_path() ) . $this->get_backup_filename();
59
	}
60
61
	/**
62
	 * Get the filename of the backup.
63
	 *
64
	 * @return string The backup filename.
65
	 */
66
	public function get_backup_filename() {
67
		return $this->backup_filename;
0 ignored issues
show
Bug introduced by
The property backup_filename does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
68
	}
69
70
	/**
71
	 * Set the filename of the backup.
72
	 *
73
	 * @param string $filename The backup filename.
74
	 */
75
	public function set_backup_filename( $filename ) {
76
		$this->backup_filename = strtolower( sanitize_file_name( remove_accents( $filename ) ) );
77
	}
78
79
	/**
80
	 * Get the array of errors encountered during the backup process.
81
	 *
82
	 * @param  string $context The context for the error, usually the Backup
0 ignored issues
show
Documentation introduced by
Should the type for parameter $context not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
83
	 *                         Engine that encountered the error.
84
	 *
85
	 * @return array           The array of errors.
86
	 */
87 View Code Duplication
	public function get_errors( $context = null ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
89
		// Only return a specific contexts errors.
90
		if ( ! empty( $context ) ) {
91
			return isset( $this->errors[ $context ] ) ? $this->errors[ $context ] : array();
92
		}
93
94
		return $this->errors;
95
96
	}
97
98
	/**
99
	 * Add an error to the errors array.
100
	 *
101
	 * An error is always treat as fatal and should only be used for unrecoverable
102
	 * issues with the backup process.
103
	 *
104
	 * @param  string $context The context for the error.
105
	 * @param  string $error   The error that was encountered.
106
	 */
107 View Code Duplication
	public function error( $context, $error ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
109
		if ( empty( $context ) || empty( $error ) ) {
110
			return;
111
		}
112
113
		// Ensure we don't store duplicate errors by md5'ing the error as the key
114
		$this->errors[ $context ][ $_key = md5( implode( ':', (array) $error ) ) ] = $error;
115
116
	}
117
118
	/**
119
	 * Get the array of warnings encountered during the backup process.
120
	 *
121
	 * @param  string $context The context for the warning, usually the Backup
0 ignored issues
show
Documentation introduced by
Should the type for parameter $context not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
122
	 *                         Engine that encountered the warning.
123
	 *
124
	 * @return array           The array of warnings.
125
	 */
126 View Code Duplication
	public function get_warnings( $context = null ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
128
		// Only return a specific contexts errors.
129
		if ( ! empty( $context ) ) {
130
			return isset( $this->warnings[ $context ] ) ? $this->warnings[ $context ] : array();
131
		}
132
133
		return $this->warnings;
134
135
	}
136
137
	/**
138
	 * Add an warning to the errors warnings.
139
	 *
140
	 * A warning is always treat as non-fatal and should only be used for recoverable
141
	 * issues with the backup process.
142
	 *
143
	 * @param  string $context The context for the warning.
144
	 * @param  string $error   The warning that was encountered.
0 ignored issues
show
Bug introduced by
There is no parameter named $error. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
145
	 */
146 View Code Duplication
	public function warning( $context, $warning ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
148
		if ( empty( $context ) || empty( $warning ) ) {
149
			return;
150
		}
151
152
		// Ensure we don't store duplicate warnings by md5'ing the error as the key
153
		$this->warnings[ $context ][ $_key = md5( implode( ':', (array) $warning ) ) ] = $warning;
154
155
	}
156
157
	/**
158
	 * Hooked into `set_error_handler` to catch any PHP errors that happen during
159
	 * the backup process.
160
	 *
161
	 * PHP errors are always treat as warnings rather than errors.
162
	 *
163
	 * @param  int $type   The level of error raised
164
	 *
165
	 * @return false       Return false to pass the error back to PHP so it can
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
166
	 *                     be handled natively.
167
	 */
168
	public function error_handler( $type ) {
169
170
		// Skip strict & deprecated warnings
171
		if ( ( defined( 'E_DEPRECATED' ) && $type === E_DEPRECATED ) || ( defined( 'E_STRICT' ) && $type === E_STRICT ) || error_reporting() === 0 ) {
172
			return false;
173
		}
174
175
		/**
176
		 * Get the details of the error.
177
		 *
178
		 * These are:
179
		 *
180
		 * @param int    $errorno   The error level expressed as an integer/
181
		 * @param string $errstr    The error message.
182
		 * @param string $errfile   The file that the error raised in.
183
		 * @param string $errorline The line number the error was raised on.
184
		 */
185
		$args = func_get_args();
186
187
		// Strip the error level
188
		array_shift( $args );
189
190
		// Fire a warning for the PHP error passing the message, file and line number.
191
		$this->warning( 'php', implode( ', ', array_splice( $args, 0, 3 ) ) );
192
193
		return false;
194
195
	}
196
197
}
198