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 (494fb2)
by
unknown
03:13
created

Mysqldump_Database_Backup_Engine   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 15
c 6
b 1
f 0
lcom 1
cbo 3
dl 0
loc 145
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B backup() 0 39 6
B get_mysql_connection_args() 0 23 4
A __construct() 0 3 1
B get_mysqldump_executable_path() 0 46 4
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
	 * Perform the database backup.
83
	 *
84
	 * @return bool Whether the backup completed successfully or not.
85
	 */
86
	public function backup() {
87
88
		if ( ! $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...
89
			return false;
90
		}
91
92
		// Grab the database connections args
93
		$args = $this->get_mysql_connection_args();
94
95
		// Allow lock-tables to be overridden
96
		if ( defined( 'HMBKP_MYSQLDUMP_SINGLE_TRANSACTION' ) && HMBKP_MYSQLDUMP_SINGLE_TRANSACTION  ) {
97
			$args[] = '--single-transaction';
98
		}
99
100
		// Make sure binary data is exported properly
101
		$args[] = '--hex-blob';
102
103
		// The file we're saving too
104
		$args[] = '-r ' . escapeshellarg( $this->get_backup_filepath() );
105
106
		// The database we're dumping
107
		$args[] = escapeshellarg( $this->get_name() );
108
109
		$process = new Process( $this->get_mysqldump_executable_path() . ' ' . implode( ' ', $args ) );
110
		$process->setTimeout( HOUR_IN_SECONDS );
111
112
		try {
113
			$process->run();
114
		} catch ( \Exception $e ) {
115
			$this->error( __CLASS__, $e->getMessage() );
116
		}
117
118
		if ( ! $process->isSuccessful() ) {
119
			$this->error( __CLASS__, $process->getErrorOutput() );
120
		}
121
122
		return $this->verify_backup();
123
124
	}
125
126
	/**
127
	 * Get the connection args for the mysqldump command
128
	 *
129
	 * @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...
130
	 */
131
	public function get_mysql_connection_args() {
132
133
		$args = array();
134
135
		$args[] = '-u ' . escapeshellarg( $this->get_user() );
136
137
		if ( $this->get_password() ) {
138
			$args[] = '-p' . escapeshellarg( $this->get_password() );
139
		}
140
141
		$args[] = '-h ' . escapeshellarg( $this->get_host() );
142
143
		if ( $this->get_port() ) {
144
			$args[] = '-P ' . escapeshellarg( $this->get_port() );
145
		}
146
147
		if ( $this->get_socket() ) {
148
			$args[] = '--protocol=socket -S ' . escapeshellarg( $this->get_socket() );
149
		}
150
151
		return $args;
152
153
	}
154
}
155