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 (#999)
by Tom
02:15
created

Pclzip_File_Backup_Engine::load_pclzip()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
/**
6
 * Perform a file backup using the PclZip PHP fallback library
7
 */
8
class Pclzip_File_Backup_Engine extends File_Backup_Engine {
9
10
	public function __construct() {
11
		parent::__construct();
12
	}
13
14
	public function load_pclzip() {
15
16
		if ( ! defined( 'PCLZIP_TEMPORARY_DIR' ) ) {
17
			define( 'PCLZIP_TEMPORARY_DIR', trailingslashit( PATH::get_path() ) );
18
		}
19
20
		require_once( ABSPATH . 'wp-admin/includes/class-pclzip.php' );
21
22
	}
23
24
	public function backup() {
1 ignored issue
show
Coding Style introduced by
backup uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
25
26
		$this->load_pclzip();
27
28
		if ( ! class_exists( 'PclZip' ) ) {
29
			return false;
30
		}
31
32
		global $_hmbkp_exclude_string;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
33
		$_hmbkp_exclude_string = $this->get_excludes_regex();
34
35
		$zip = new \PclZip( $this->get_backup_filepath() );
36
37
		$result = $zip->create( Path::get_root(), \PCLZIP_OPT_REMOVE_PATH, Path::get_root(), \PCLZIP_CB_PRE_ADD, 'HM\BackUpWordPress\hmbkp_pclzip_callback' );
38
39
		if ( 0 === $result ) {
40
			$this->error( __CLASS__, $zip->errorInfo( true ) );
41
		}
42
43
		unset( $GLOBALS['_hmbkp_exclude_string'] );
44
45
		return $this->verify_backup();
46
47
	}
48
49
	/**
50
	 * Generate the exclude params for pclzip
51
	 *
52
	 * Takes the exclude rules and formats them as regex
53
	 *
54
	 * @return string
55
	 */
56
	public function get_excludes_regex() {
57
58
		$wildcard  = '([\s\S]*?)';
59
		$separator = '|';
60
61
		$excludes = $this->excludes->get_excludes();
0 ignored issues
show
Bug introduced by
The method get_excludes cannot be called on $this->excludes (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
62
63
		foreach ( $excludes as $key => &$rule ) {
64
65
			$file = $absolute = $fragment = false;
0 ignored issues
show
Unused Code introduced by
$fragment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
67
			// Files don't end with /
68 View Code Duplication
			if ( ! in_array( substr( $rule, - 1 ), array( '\\', '/' ) ) ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
69
				$file = true;
0 ignored issues
show
Unused Code introduced by
$file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
			} // If rule starts with a / then treat as absolute path
71
			elseif ( in_array( substr( $rule, 0, 1 ), array( '\\', '/' ) ) ) {
72
				$absolute = true;
73
			} // Otherwise treat as dir fragment
74
			else {
75
				$fragment = true;
0 ignored issues
show
Unused Code introduced by
$fragment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
			}
77
78
			// Strip $this->root and conform
79
			$rule = str_ireplace( Path::get_root(), '', untrailingslashit( wp_normalize_path( $rule ) ) );
80
81
			// Strip the preceeding slash
82
			if ( in_array( substr( $rule, 0, 1 ), array( '\\', '/' ) ) ) {
83
				$rule = substr( $rule, 1 );
84
			}
85
86
			$rule = str_replace( '.', '\.', $rule );
87
88
			// Convert any existing wildcards
89
			if ( '*' !== $wildcard && false !== strpos( $rule, '*' ) ) {
90
				$rule = str_replace( '*', $wildcard, $rule );
91
			}
92
93
			// Add a start carrot to absolute urls for pclzip
94
			if ( $absolute ) {
95
				$rule = '^' . $rule;
96
			}
97
		}
98
99
		return implode( $separator, $excludes );
100
	}
101
}
102
103
function hmbkp_pclzip_callback( $event, $file ) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
105
	global $_hmbkp_exclude_string;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
106
107
	// Don't try to add unreadable files.
108
	if ( ! is_readable( $file['filename'] ) || ! file_exists( $file['filename'] ) ) {
109
		return false;
110
	}
111
112
	// Match everything else past the exclude list
113
	if ( $_hmbkp_exclude_string && preg_match( '(' . $_hmbkp_exclude_string . ')', $file['filename'] ) ) {
1 ignored issue
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($_hmbkp_exclude...', $file['filename']));.
Loading history...
114
		return false;
115
	}
116
117
	return true;
118
119
}
120