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 (#1001)
by Paul
02:37
created

Mysqldump_Database_Backup_Engine::backup()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 36
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 36
rs 8.439
cc 6
eloc 15
nc 5
nop 0
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
use Symfony\Component\Process\Process as Process;
6
7
/**
8
 * Perform a database backup using the mysqldump cli command
9
 */
10
class Mysqldump_Database_Backup_Engine extends Database_Backup_Engine {
11
12
	/**
13
	 * The path to the mysqldump executable
14
	 *
15
	 * @var string
16
	 */
17
	private $mysqldump_executable_path = '';
18
19
	public function __construct() {
20
		parent::__construct();
21
	}
22
23
	/**
24
	 * Calculate the path to the mysqldump executable.
25
	 *
26
	 * The executable path can be overridden using either the `HMBKP_MYSQLDUMP_PATH`
27
	 * Constant or the `hmbkp_mysqldump_executable_path` filter.
28
	 *
29
	 * If neither of those are set then we fallback to checking a number of
30
	 * common locations.
31
	 *
32
	 * @return string|false The path to the executable or false.
33
	 */
34
	public function get_mysqldump_executable_path() {
35
36
		// Return now if it's set in a Constant
37
		if ( defined( 'HMBKP_MYSQLDUMP_PATH' ) && HMBKP_MYSQLDUMP_PATH ) {
38
			$this->mysqldump_executable_path = HMBKP_MYSQLDUMP_PATH;
39
		}
40
41
		/**
42
		 * Allow the executable path to be set via a filter
43
		 *
44
		 * @param string The path to the mysqldump executable
45
		 */
46
		$this->mysqldump_executable_path = apply_filters( 'hmbkp_mysqldump_executable_path', '' );
47
48
		if ( ! $this->mysqldump_executable_path ) {
49
50
			// List of possible mysqldump locations
51
			$paths = array(
52
				'mysqldump',
53
				'/usr/local/bin/mysqldump',
54
				'/usr/local/mysql/bin/mysqldump',
55
				'/usr/mysql/bin/mysqldump',
56
				'/usr/bin/mysqldump',
57
				'/opt/local/lib/mysql6/bin/mysqldump',
58
				'/opt/local/lib/mysql5/bin/mysqldump',
59
				'/opt/local/lib/mysql4/bin/mysqldump',
60
				'/xampp/mysql/bin/mysqldump',
61
				'/Program Files/xampp/mysql/bin/mysqldump',
62
				'/Program Files/MySQL/MySQL Server 6.0/bin/mysqldump',
63
				'/Program Files/MySQL/MySQL Server 5.7/bin/mysqldump',
64
				'/Program Files/MySQL/MySQL Server 5.6/bin/mysqldump',
65
				'/Program Files/MySQL/MySQL Server 5.5/bin/mysqldump',
66
				'/Program Files/MySQL/MySQL Server 5.4/bin/mysqldump',
67
				'/Program Files/MySQL/MySQL Server 5.1/bin/mysqldump',
68
				'/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump',
69
				'/Program Files/MySQL/MySQL Server 4.1/bin/mysqldump',
70
				'/opt/local/bin/mysqldump',
71
			);
72
73
			$this->mysqldump_executable_path = Backup_Utilities::get_executable_path( $paths );
0 ignored issues
show
Documentation Bug introduced by
It seems like \HM\BackUpWordPress\Back...executable_path($paths) can also be of type false. However, the property $mysqldump_executable_path is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
74
75
		}
76
77
		return $this->mysqldump_executable_path;
78
79
	}
80
81
	/**
82
	 * Check whether it's possible to connect to the database with the
83
	 * credentials we have.
84
	 *
85
	 * @return bool Whether the database connection was successful.
86
	 */
87
	public function check_user_can_connect_to_database_via_cli() {
88
89
		if ( ! function_exists( 'proc_open' ) ) {
90
			return false;
91
		}
92
93
		$args = $this->get_mysql_connection_args();
94
		$args[] = escapeshellarg( $this->get_name() );
95
96
		// Quit immediately as we're only interesting in testing the connection
97
		$args[] = '--execute="quit"';
98
99
		$process = new Process( 'mysql ' . implode( ' ', $args ) );
100
		$process->run();
101
102
		if ( ! $process->isSuccessful() ) {
103
			$this->error( __CLASS__, $process->getErrorOutput() );
104
			return false;
105
		}
106
107
		return true;
108
109
	}
110
111
	/**
112
	 * Perform the database backup.
113
	 *
114
	 * @return bool Whether the backup completed successfully or not.
115
	 */
116
	public function backup() {
117
118
		if ( ! $this->check_user_can_connect_to_database_via_cli() || ! $this->get_mysqldump_executable_path() ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->get_mysqldump_executable_path() of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
119
			return false;
120
		}
121
122
		// Grab the database connections args
123
		$args = $this->get_mysql_connection_args();
124
125
		// We don't want to create a new DB
126
		$args[] = '--no-create-db';
127
128
		// Allow lock-tables to be overridden
129
		if ( defined( 'HMBKP_MYSQLDUMP_SINGLE_TRANSACTION' ) && HMBKP_MYSQLDUMP_SINGLE_TRANSACTION  ) {
130
			$args[] = '--single-transaction';
131
		}
132
133
		// Make sure binary data is exported properly
134
		$args[] = '--hex-blob';
135
136
		// The file we're saving too
137
		$args[] = '-r ' . escapeshellarg( $this->get_backup_filepath() );
138
139
		// The database we're dumping
140
		$args[] = escapeshellarg( $this->get_name() );
141
142
		$process = new Process( $this->get_mysqldump_executable_path() . ' ' . implode( ' ', $args ) );
143
		$process->run();
144
145
		if ( ! $process->isSuccessful() ) {
146
			$this->error( __CLASS__, $process->getErrorOutput() );
147
		}
148
149
		return $this->verify_backup();
150
151
	}
152
153
	/**
154
	 * Get the connection args for the mysqldump command
155
	 *
156
	 * @return array The array of connection args
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
157
	 */
158
	public function get_mysql_connection_args() {
159
160
		$args = array();
161
162
		$args[] = '-u ' . escapeshellarg( $this->get_user() );
163
164
		if ( $this->get_password() ) {
165
			$args[] = '-p' . escapeshellarg( $this->get_password() );
166
		}
167
168
		$args[] = '-h ' . escapeshellarg( $this->get_host() );
169
170
		if ( $this->get_port() ) {
171
			$args[] = '-P ' . escapeshellarg( $this->get_port() );
172
		}
173
174
		if ( $this->get_socket() ) {
175
			$args[] = '--protocol=socket -S ' . escapeshellarg( $this->get_socket() );
176
		}
177
178
		return $args;
179
180
	}
181
}
182