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
Push — master ( 0a67c8...aec3a2 )
by Liuta
03:06
created

Xcloner_File_System::array_orderby()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 5
nop 0
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
use League\Flysystem\Config;
3
use League\Flysystem\Filesystem;
4
use League\Flysystem\Util;
5
use League\Flysystem\Adapter\Local;
6
7
class Xcloner_File_System{
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
	
9
	private $excluded_files 			= "";
10
	private $excluded_files_by_default	= array("administrator/backups", "wp-content/backups");
11
	private $included_files_handler 	= "backup_files.csv";
12
	private $temp_dir_handler 			= ".dir";
13
	public  $filesystem;
14
	public  $tmp_filesystem;
15
	public  $storage_filesystem;
16
	private $xcloner_settings_append;
0 ignored issues
show
Unused Code introduced by
The property $xcloner_settings_append is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
	private $logger;
18
	
19
	private $start_adapter;
20
	private $tmp_adapter;
21
	private $storage_adapter;
22
	private $xcloner_requirements;
23
	
24
	private $files_counter;
25
	private $files_size;
26
	private $last_logged_file;
27
	private $folders_to_process_per_session = 25;
28
	private $backup_archive_extensions = array("tar", "tgz", "tar.gz", "gz", "csv");
29
	private $backup_name_tags = array('[time]', '[hostname]', '[domain]');
30
	
31
	public function __construct($hash = "")
32
	{
33
		$this->logger = new XCloner_Logger('xcloner_file_system', $hash);
34
		$this->xcloner_requirements = new Xcloner_Requirements();
35
36
		$this->xcloner_settings 		= new Xcloner_Settings($hash);
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
37
38
		try{
39
			
40
			$this->start_adapter = new Local($this->xcloner_settings->get_xcloner_start_path(),LOCK_EX, 'SKIP_LINKS');
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
41
			$this->start_filesystem = new Filesystem($this->start_adapter, new Config([
0 ignored issues
show
Bug introduced by
The property start_filesystem does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
42
					'disable_asserts' => true,
43
				]));
44
			
45
			$this->tmp_adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(),LOCK_EX, 'SKIP_LINKS');
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
46
			$this->tmp_filesystem = new Filesystem($this->tmp_adapter, new Config([
47
					'disable_asserts' => true,
48
				]));
49
			$adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(),LOCK_EX|FILE_APPEND, 'SKIP_LINKS');
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
50
			$this->tmp_filesystem_append = new Filesystem($adapter, new Config([
0 ignored issues
show
Bug introduced by
The property tmp_filesystem_append does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
51
					'disable_asserts' => true,
52
				]));
53
54
			$adapter = new Local($this->xcloner_settings->get_xcloner_store_path(),LOCK_EX, 'SKIP_LINKS');
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
55
			$this->storage_filesystem = new Filesystem($adapter, new Config([
56
					'disable_asserts' => true,
57
				]));	
58
			
59
			$this->storage_adapter = new Local($this->xcloner_settings->get_xcloner_store_path(),FILE_APPEND, 'SKIP_LINKS');
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
60
			$this->storage_filesystem_append = new Filesystem($this->storage_adapter, new Config([
0 ignored issues
show
Bug introduced by
The property storage_filesystem_append does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
61
					'disable_asserts' => true,
62
				]));
63
		}catch(Exception $e){
64
			return false;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
65
		}
66
		
67
		
68
		if($value = get_option('xcloner_directories_to_scan_per_request'))
69
			$this->folders_to_process_per_session = $value;
70
71
	}
72
	
73
	public function set_hash($hash)
74
	{
75
		$this->xcloner_settings->set_hash($hash);
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
76
	}
77
	
78
	public function get_hash($hash)
0 ignored issues
show
Unused Code introduced by
The parameter $hash 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...
79
	{
80
		$this->xcloner_settings->get_hash();
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
81
	}
82
	
83
	public function get_tmp_filesystem()
84
	{
85
		return $this->tmp_filesystem;
86
	}
87
	
88
	public function get_storage_filesystem()
89
	{
90
		return $this->storage_filesystem;
91
	}
92
	
93
	public function get_tmp_filesystem_adapter()
94
	{
95
		return $this->tmp_adapter;
96
	}
97
	
98
	public function get_tmp_filesystem_append()
99
	{
100
		return $this->tmp_filesystem_append;
0 ignored issues
show
Bug introduced by
The property tmp_filesystem_append does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
101
	}
102
	
103
	public function get_start_adapter()
104
	{
105
		return $this->start_adapter;
106
	}
107
	
108
	public function get_start_filesystem()
109
	{
110
		return $this->start_filesystem;
0 ignored issues
show
Bug introduced by
The property start_filesystem does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
111
	}
112
	
113
	public function get_logger()
114
	{
115
		return $this->logger;
116
	}
117
	
118
	public function get_start_path_file_info($file)
0 ignored issues
show
Unused Code introduced by
The parameter $file 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...
119
	{
120
		//$info= $this->getMetadataFull('start_adapter', $file);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
121
		return $this->start_filesystem->normalizeFileInfo($info);
0 ignored issues
show
Bug introduced by
The property start_filesystem does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The variable $info does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
122
	}
123
	
124
	public function get_storage_path_file_info($file)
125
	{
126
		return $this->getMetadataFull('storage_adapter', $file);
127
	}
128
	
129
	public function get_included_files_handler($metadata  = 0)
130
	{
131
		$path = $this->included_files_handler;
132
		if(!$metadata)
133
			return $path;
134
		
135
		$spl_info = $this->getMetadataFull('tmp_adapter', $path);
136
		return $spl_info;
137
		
138
	}
139
		
140
	public function get_temp_dir_handler()
141
	{
142
		return $this->temp_dir_handler;
143
	}
144
	
145
	public function get_latest_backup()
146
	{
147
		$files = $this->get_backup_archives_list();
148
		
149
		if(is_array($files))
150
			$this->sort_by($files, "timestamp","desc");
151
		
152
		$new_list = array();
153
		
154 View Code Duplication
		foreach($files as $key=>$file)
0 ignored issues
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...
155
			if(!isset($file['parent']))
156
				$new_list[] = ($files[$key]);
157
158
		if(isset($new_list[0]))
159
			return $new_list[0];
160
	}
161
	
162
	public function get_latest_backups()
163
	{
164
		$files = $this->get_backup_archives_list();
165
166
		if(is_array($files))
167
			$this->sort_by($files, "timestamp","desc");
168
		
169
		$new_list = array();
170
		
171 View Code Duplication
		foreach($files as $key=>$file)
0 ignored issues
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...
172
			if(!isset($file['parent']))
173
				$new_list[] = ($files[$key]);
174
175
		return $new_list;
176
	}
177
	
178
	public function get_storage_usage()
179
	{
180
		$files = $this->get_backup_archives_list();
181
		$total = 0;
182
		
183
		if(is_array($files))
184
			foreach($files as $file)
185
				$total += $file['size'];
186
				
187
		return $total;		
188
	}
189
	
190
	public function is_part($backup_name)
191
	{
192
		if(stristr($backup_name, "-part"))
193
			return true;
194
		
195
		return false;	
196
	}
197
	
198
	public function is_multipart($backup_name)
199
	{
200
		if(stristr($backup_name, "-multipart"))
201
			return true;
202
		
203
		return false;	
204
	}
205
	
206
	public function get_backup_size($backup_name)
207
	{
208
		$backup_size = $this->get_storage_filesystem()->getSize($backup_name);
209
		if($this->is_multipart($backup_name))
210
		{
211
			$backup_parts = $this->get_multipart_files($backup_name);
212
			foreach($backup_parts as $part_file)
213
				$backup_size += $this->get_storage_filesystem()->getSize($part_file);
214
		}
215
		
216
		return $backup_size;
217
	}
218
	
219
	public function get_multipart_files($backup_name)
220
	{
221
		$files = array();
222
		
223 View Code Duplication
		if($this->is_multipart($backup_name))
0 ignored issues
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...
224
		{
225
			$lines = explode(PHP_EOL, $this->get_storage_filesystem()->read($backup_name));
226
			foreach($lines as $line)
227
			{
228
				if($line)
229
				{
230
					$data = str_getcsv($line);
231
					$files[] = $data[0];
232
				}
233
			}
234
		}
235
		
236
		return $files;
237
	}
238
	
239
	public function delete_backup_by_name($backup_name)
240
	{
241 View Code Duplication
		if($this->is_multipart($backup_name))
0 ignored issues
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...
242
		{
243
			$lines = explode(PHP_EOL, $this->get_storage_filesystem()->read($backup_name));
244
			foreach($lines as $line)
245
			{
246
				if($line)
247
				{
248
					$data = str_getcsv($line);
249
					$this->get_storage_filesystem()->delete($data[0]);
250
				}
251
			}
252
		}
253
		
254
		if($this->get_storage_filesystem()->delete($backup_name))
255
			$return = true;
256
		else
257
			$return = false;
258
			
259
		return $return;	
260
	}
261
	
262
	public function getMetadataFull($adapter = "storage_adapter" , $path)
263
    {
264
        $location = $this->$adapter->applyPathPrefix($path);
265
        $spl_info = new SplFileInfo($location);
266
        
267
        return ($spl_info);
268
    }
269
	
270
	
271
	public function get_backup_archives_list()
272
	{
273
		$list = array();
274
		
275
		if(method_exists($this->get_storage_filesystem(), "listContents"))
276
			$list = $this->get_storage_filesystem()->listContents();
277
278
		
279
		$backup_files = array();
280
		$parents = array();
281
		
282
		foreach($list as $file_info)
283
		{
284
			$data = array();
0 ignored issues
show
Unused Code introduced by
$data 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...
285
			
286
			if(isset($file_info['extension']) and $file_info['extension'] == "csv")
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
287
			{
288
				$lines = explode(PHP_EOL, $this->get_storage_filesystem()->read($file_info['path']));
289
				foreach($lines as $line)
290
					if($line)
291
					{
292
						$data = str_getcsv($line);
293
						if(is_array($data)){
294
							$parents[$data[0]] = $file_info['path'];
295
							$file_info['childs'][] = $data;
296
							$file_info['size'] += $data[2];
297
						}
298
					}
299
						
300
			}
301
			
302 View Code Duplication
			if($file_info['type'] == 'file' and isset($file_info['extension']) and in_array($file_info['extension'], $this->backup_archive_extensions))
0 ignored issues
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...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
303
				$backup_files[$file_info['path']] = $file_info;
304
		}
305
		
306
		foreach($backup_files as $key=>$file_info)
307
		{
308
			if(isset($parents[$file_info['path']]))
309
				$backup_files[$key]['parent'] = $parents[$file_info['path']];
310
		}
311
		
312
		return $backup_files;
313
	}
314
	
315
	public function start_file_recursion($init = 0)
316
	{
317
		if($init)
318
		{
319
			$this->logger->info(sprintf(__("Starting the filesystem scanner on root folder %s"), $this->xcloner_settings->get_xcloner_start_path()));
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
320
			$this->do_system_init();
321
		}
322
		
323
		if($this->tmp_filesystem->has($this->get_temp_dir_handler())){
324
		//.dir exists, we presume we have files to iterate	
325
			$content = $this->tmp_filesystem->read($this->get_temp_dir_handler());
326
			$files = array_filter(explode("\n", $content));
327
			$this->tmp_filesystem->delete($this->get_temp_dir_handler());
328
			
329
			$counter = 0;
330
			foreach($files as $file)
331
			{
332
				if($counter < $this->folders_to_process_per_session){
333
					$this->build_files_list($file);
334
					$counter++;
335
				}else{
336
					$this->tmp_filesystem_append->write($this->get_temp_dir_handler(), $file."\n");
0 ignored issues
show
Bug introduced by
The property tmp_filesystem_append does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
337
				}
338
			}
339
		}else{
340
			$this->build_files_list();
341
		}
342
		
343
		if($this->scan_finished())
344
		{
345
			$metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata("index.html");
346
			$this->store_file($metadata_dumpfile, 'tmp_filesystem');
347
			$this->files_counter++;
348
		
349
			//adding included dump file to the included files list
350
			if($this->get_tmp_filesystem()->has($this->get_included_files_handler()))
0 ignored issues
show
Bug introduced by
It seems like $this->get_included_files_handler() targeting Xcloner_File_System::get_included_files_handler() can also be of type object<SplFileInfo>; however, League\Flysystem\Filesystem::has() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
351
			{
352
				$metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata($this->get_included_files_handler());
0 ignored issues
show
Bug introduced by
It seems like $this->get_included_files_handler() targeting Xcloner_File_System::get_included_files_handler() can also be of type object<SplFileInfo>; however, League\Flysystem\Filesystem::getMetadata() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
353
				$this->store_file($metadata_dumpfile, 'tmp_filesystem');
354
				$this->files_counter++;
355
			}
356
		
357
			//adding a default index.html to the temp xcloner folder
358
			if(!$this->get_tmp_filesystem()->has("index.html"))
359
			{
360
				$this->get_tmp_filesystem()->write("index.html","");
361
			}
362
			
363
			//adding the default log file
364
			if($this->get_tmp_filesystem()->has($this->xcloner_settings->get_logger_filename(1)))
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
365
			{
366
				$metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata($this->xcloner_settings->get_logger_filename(1));
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
367
				$this->store_file($metadata_dumpfile, 'tmp_filesystem');
368
				$this->files_counter++;
369
			}
370
		
371
			return false;
372
		}	
373
	
374
		return true;	
375
	}
376
	
377
	public function get_backup_attachments()
378
	{
379
		$return = array();
380
		$return[] = $this->xcloner_settings->get_xcloner_tmp_path().DS.$this->get_included_files_handler();
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
381
		if($this->xcloner_settings->get_xcloner_option('xcloner_enable_log'))
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
382
			$return[] = $this->xcloner_settings->get_xcloner_tmp_path().DS.$this->xcloner_settings->get_logger_filename(1);
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
383
		
384
		return $return;
385
	}
386
	
387
	public function remove_tmp_filesystem()
388
	{
389
		//delete the temporary folder
390
		$this->logger->info(sprintf("Deleting the temporary storage folder %s", $this->xcloner_settings->get_xcloner_tmp_path()));
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
391
		
392
		$contents = $this->get_tmp_filesystem()->listContents();
393
	
394
		if(is_array($contents))
395
		foreach($contents as $file_info)
396
			$this->get_tmp_filesystem()->delete($file_info['path']);
397
			
398
		@rmdir($this->xcloner_settings->get_xcloner_tmp_path());
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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...
399
		
400
		return;
401
	}
402
	
403
	private function do_system_init()
404
	{
405
		$this->files_counter = 0;
406
		
407
		if(!$this->storage_filesystem->has("index.html"))	
408
			$this->storage_filesystem->write("index.html","");
409
		
410
		if(!$this->tmp_filesystem->has("index.html"))	
411
			$this->tmp_filesystem->write("index.html","");
412
			
413
		if($this->tmp_filesystem->has($this->get_included_files_handler()))
0 ignored issues
show
Bug introduced by
It seems like $this->get_included_files_handler() targeting Xcloner_File_System::get_included_files_handler() can also be of type object<SplFileInfo>; however, League\Flysystem\Filesystem::has() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
414
			$this->tmp_filesystem->delete($this->get_included_files_handler());
0 ignored issues
show
Bug introduced by
It seems like $this->get_included_files_handler() targeting Xcloner_File_System::get_included_files_handler() can also be of type object<SplFileInfo>; however, League\Flysystem\Filesystem::delete() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
415
		
416
		if($this->tmp_filesystem->has($this->get_temp_dir_handler()))	
417
			$this->tmp_filesystem->delete($this->get_temp_dir_handler());
418
	}
419
	
420
	public function get_scanned_files_num()
421
	{
422
		return $this->files_counter;
423
	}
424
	
425
	public function get_scanned_files_total_size()
426
	{
427
		return $this->files_size;
428
	}
429
	
430
	public function last_logged_file()
431
	{
432
		return $this->last_logged_file;
433
	}
434
	
435
	public function set_excluded_files($excluded_files = array())
436
	{
437
		if(!is_array($excluded_files))
438
			$excluded_files = array();
439
			
440
		$this->excluded_files = array_merge($excluded_files, $this->excluded_files_by_default);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($excluded_fi...luded_files_by_default) of type array is incompatible with the declared type string of property $excluded_files.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
441
		
442
		return $this->excluded_files;
443
	}
444
	
445
	public function get_excluded_files()
446
	{
447
		return $this->excluded_files_by_default;
448
	}
449
	
450
	public function list_directory($path)
451
	{
452
		return $this->start_filesystem->listContents($path);
0 ignored issues
show
Bug introduced by
The property start_filesystem does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
453
	}
454
	
455
	public function build_files_list($folder = "")
456
	{
457
		$this->logger->debug(sprintf(("Building the files system list")));
458
		
459
		//if we start with the root folder(empty value), we initializa the file system
460
		if(!$folder){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
461
			
462
		}
463
			
464
		try{
465
			
466
			$files = $this->start_filesystem->listContents($folder);
0 ignored issues
show
Bug introduced by
The property start_filesystem does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
467
			foreach($files as $file)
468
			{
469
				if(!is_readable($this->xcloner_settings->get_xcloner_start_path().DS.$file['path']))
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
470
				{
471
					$this->logger->info(sprintf(__("Excluding %s from the filesystem list, file not readable"), $file['path']), array("FILESYSTEM SCAN","NOT READABLE"));
472
				}
473
				elseif(!$matching_pattern = $this->is_excluded($file) ){
474
					$this->logger->info(sprintf(__("Adding %s to the filesystem list"), $file['path']), array("FILESYSTEM SCAN","INCLUDE"));
475
					$file['visibility'] = $this->start_filesystem->getVisibility($file['path']);
0 ignored issues
show
Bug introduced by
The property start_filesystem does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
476
					$this->store_file($file);
477
					$this->files_counter++;
478
					if(isset($file['size']))
479
						$this->files_size += $file['size'];
480
					
481
				}else{
482
					$this->logger->info(sprintf(__("Excluding %s from the filesystem list, matching pattern %s"), $file['path'], $matching_pattern), array("FILESYSTEM SCAN","EXCLUDE"));
483
					}
484
			}
485
			
486
		}catch(Exception $e){
487
			
488
			$this->logger->error($e->getMessage());
489
		
490
		}
491
			
492
	}
493
	
494
	public function estimate_read_write_time()
495
	{
496
		$tmp_file = ".xcloner".substr(md5(time()), 0, 5);
497
				
498
		$start_time = microtime();
499
		
500
		$data = str_repeat(rand(0,9), 1024*1024); //write 1MB data
501
		
502
		try{
503
			$this->tmp_filesystem->write($tmp_file, $data);
504
			
505
			$end_time = microtime() - $start_time;
506
		
507
			$return['writing_time'] = $end_time;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
508
		
509
			$return['reading_time']	= $this->estimate_reading_time($tmp_file);
510
		
511
			$this->tmp_filesystem->delete($tmp_file);
512
		
513
		}catch(Exception $e){
514
			
515
			$this->logger->error($e->getMessage());
516
			
517
		}
518
		
519
		return $return;
0 ignored issues
show
Bug introduced by
The variable $return does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
520
	}
521
	
522
	public function backup_storage_cleanup()
523
	{
524
		$this->logger->info(sprintf(("Cleaning the backup storage on matching rules")));
525
		
526
		$_storage_size = 0;
527
		$_backup_files_list = array();
528
		
529
		//rule date limit
530
		$current_timestamp = strtotime("-".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days')." days");
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
531
		
532
		$files = $this->storage_filesystem->listContents();
533
		
534
		if(is_array($files))
535
			foreach($files as $file)
536
			{
537 View Code Duplication
				if(isset($file['extension']) and in_array($file['extension'], $this->backup_archive_extensions))
0 ignored issues
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...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
538
				{
539
					$_storage_size += $file['size']; //bytes
540
					$_backup_files_list[] = $file;
541
				}
542
			}
543
		
544
		
545
		$this->sort_by($_backup_files_list, "timestamp","asc");
546
		
547
		$_backups_counter = sizeof($_backup_files_list);
548
				
549
		foreach($_backup_files_list as $file)
550
		{
551
			//processing rule folder capacity
552
			if($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit') &&
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
553
			$_storage_size >= ($set_storage_limit = 1024*1024*$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit')))	//bytes	
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
554
			{
555
				$this->storage_filesystem->delete($file['path']);
556
				$_storage_size -= $file['size'];
557
				$this->logger->info("Deleting backup ".$file['path']." matching rule", array("STORAGE SIZE LIMIT", $_storage_size." >= ".$set_storage_limit));
558
			}
559
			
560
			//processing rule days limit
561 View Code Duplication
			if($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days') && $current_timestamp >= $file['timestamp'])
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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...
562
			{
563
				$this->storage_filesystem->delete($file['path']);
564
				$this->logger->info("Deleting backup ".$file['path']." matching rule", array("RETENTION LIMIT TIMESTAMP", $file['timestamp']." =< ".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days')));
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
565
			}
566
			
567
			//processing backup countert limit
568 View Code Duplication
			if($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives') && $_backups_counter >= $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives'))
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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...
569
			{
570
				$this->storage_filesystem->delete($file['path']);
571
				$_backups_counter--;
572
				$this->logger->info("Deleting backup ".$file['path']." matching rule", array("BACKUP QUANTITY LIMIT", $_backups_counter." >= ".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives')));
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
573
			}
574
			
575
				
576
		}
577
		
578
	}
579
	
580
	public function estimate_reading_time($tmp_file)
581
	{
582
		$this->logger->debug(sprintf(("Estimating file system reading time")));
583
		
584
		$start_time = microtime();
585
		
586
		$data = $this->tmp_filesystem->read($tmp_file);
0 ignored issues
show
Unused Code introduced by
$data 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...
587
		
588
		$end_time = microtime() - $start_time;
589
		
590
		return $end_time;
591
	
592
	}
593
	
594
	public function process_backup_name($name = "", $max_length=100)
595
	{
596
		if(!$name)
597
			$name = $this->xcloner_settings->get_default_backup_name();
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
598
		
599
		foreach($this->backup_name_tags as $tag)
600
		{
601
			if($tag == '[time]')
602
				$name = str_replace($tag, date("Y-m-d_H-i"),$name);
603
			elseif($tag == '[hostname]')
604
				$name = str_replace($tag, gethostname() ,$name);	
605
			elseif($tag == '[domain]')
606
			{
607
				$domain = parse_url(admin_url(), PHP_URL_HOST);
608
				$name = str_replace($tag, $domain ,$name);	
609
			}
610
		}
611
		
612
		if($max_length)
613
			$name = substr($name, 0, $max_length);
614
			
615
		return $name;	
616
	}
617
	
618
	public function sort_by( &$array, $field, $direction = 'asc')
619
	{
620
		if(strtolower($direction) == "desc" || $direction == SORT_DESC)
621
			$direction = SORT_DESC;
622
		else
623
			$direction = SORT_ASC;
624
					
625
	   $array = $this->array_orderby($array, $field, $direction);
626
	    
627
	   return true;
628
	}
629
	
630
	private function array_orderby()
631
	{
632
	    $args = func_get_args();
633
	    $data = array_shift($args);
634
	    
635
	    foreach ($args as $n => $field) {
636
	        if (is_string($field)) {
637
	            $tmp = array();
638
	            foreach ($data as $key => $row)
639
	            {
640
					if(is_array($row))
641
						$tmp[$key] = $row[$field];
642
					else
643
						$tmp[$key] = $row->$field;
644
				}
645
	            $args[$n] = $tmp;
646
	            }
647
	    }
648
	    $args[] = &$data;
649
	    
650
	    call_user_func_array('array_multisort', $args);
651
	    
652
	    return array_pop($args);
653
	}
654
	
655
	public function is_excluded($file)
656
	{
657
		$this->logger->debug(sprintf(("Checking if %s is excluded"), $file['path']));
658
		
659
		if($xcloner_exclude_files_larger_than_mb = $this->xcloner_settings->get_xcloner_option('xcloner_exclude_files_larger_than_mb'))
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
660
		{
661
			if(isset($file['size']) and $file['size'] > $this->calc_to_bytes($xcloner_exclude_files_larger_than_mb))
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
662
				return "> ".$xcloner_exclude_files_larger_than_mb."MB";
663
		}
664
		
665
		if(!sizeof($this->excluded_files))
666
			$this->set_excluded_files();
667
				
668
		if(is_array($this->excluded_files))
669
		foreach($this->excluded_files as $excluded_file_pattern)
670
		{
671
			if($excluded_file_pattern == "/")
672
				$needle = "$";
673
			else
674
				$needle = "$".$excluded_file_pattern;
675
				
676
			if(strstr("$".$file['path'], $needle)){
677
				return $excluded_file_pattern;
678
			}
679
		}
680
		
681
		if( $regex = $this->is_excluded_regex($file))
682
			return $regex;
683
		
684
		return false;
685
	}
686
	
687
	/*REGEX examples
688
	 * 
689
	* exclude all except .php file
690
	* PATTERN: ^(.*)\.(.+)$(?<!(php))
691
	* 
692
	* exclude all except .php and .txt
693
	* PATTERN: ^(.*)\.(.+)$(?<!(php|txt))";
694
	* 
695
	* exclude all .svn and .git
696
	* PATTERN: ^(.*)\.(svn|git)(.*)$";
697
	* 
698
	* exclude root directory /test
699
	* PATTERN: "^\/test(.*)$";
700
	* 
701
	* exclude the wp-admin folder
702
	* PATTERN: ^(\/wp-admin)(.*)$";
703
	* 
704
	* exclude the wp-admin, wp-includes and wp-config.php
705
	* PATTERN: ^\/(wp-admin|wp-includes|wp-config.php)(.*)$";
706
	* 
707
	* exclude all .avi files
708
	* PATTERN: ^(.*)$(?<=(avi))";
709
	* 
710
	* exclude all .jpg and gif files
711
	* PATTERN: ^(.*)$(?<=(gif|jpg))";
712
	* 
713
	* exclude all cache folders from wp-content/
714
	* PATTERN: ^\/wp-content(.*)\/cache($|\/)(.*)";
715
	* 
716
	* exclude the backup folders
717
	* PATTERN: (^|^\/)(wp-content\/backups|administrator\/backups)(.*)$";
718
	*/
719
	private function is_excluded_regex($file)
720
	{
721
		//$this->logger->debug(sprintf(("Checking if %s is excluded"), $file['path']));
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
722
		
723
		$regex_patterns = explode(PHP_EOL, $this->xcloner_settings->get_xcloner_option('xcloner_regex_exclude'));
0 ignored issues
show
Bug introduced by
The property xcloner_settings does not seem to exist. Did you mean xcloner_settings_append?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
724
		
725
		//print_r($regex_patterns);exit;
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
726
		
727
		if(is_array($regex_patterns))
728
		{
729
			//$this->excluded_files = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
730
			//$this->excluded_files[] ="(.*)\.(git)(.*)$";
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
731
			//$this->excluded_files[] ="wp-content\/backups(.*)$";
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
732
			
733
			foreach($regex_patterns as $excluded_file_pattern)
734
			{
735
				
736
				if( substr($excluded_file_pattern, strlen($excluded_file_pattern)-1, strlen($excluded_file_pattern)) == "\r")
737
					$excluded_file_pattern = substr($excluded_file_pattern, 0, strlen($excluded_file_pattern)-1);
738
					
739
				if($file['path'] == "/")
740
					$needle = "/";
741
				else
742
					$needle = "/".$file['path'];
743
				//echo $needle."---".$excluded_file_pattern."---\n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
744
				
745
				if(@preg_match("/(^|^\/)".$excluded_file_pattern."/i", $needle)){
746
					return $excluded_file_pattern;
747
				}
748
			}
749
		}
750
		
751
		return false;
752
	}
753
	
754
	public function store_file($file, $storage = 'start_filesystem')
755
	{
756
		$this->logger->debug(sprintf("Storing %s in the backup list", $file['path']));
757
		
758
		if(!isset($file['size']))
759
			$file['size'] = 0;
760
		if(!isset($file['visibility']))	
761
			$file['visibility'] = "private";
762
		
763
		$line = '"'.addslashes($file['path']).'","'.$file['timestamp'].'","'.$file['size'].'","'.$file['visibility'].'","'.$storage.'"'.PHP_EOL;
764
		
765
		$this->last_logged_file = $file['path'];
766
		
767
		try{
768
			$this->tmp_filesystem_append->write($this->get_included_files_handler(), $line);
0 ignored issues
show
Bug introduced by
The property tmp_filesystem_append does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
769
		
770
		}catch(Exception $e){
771
		
772
			$this->logger->error($e->getMessage());	
773
		}
774
		
775
		if($file['type'] == "dir"){
776
			try{
777
				$this->tmp_filesystem_append->write($this->get_temp_dir_handler(), $file['path']."\n");
0 ignored issues
show
Bug introduced by
The property tmp_filesystem_append does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
778
			}catch(Exception $e){
779
				$this->logger->error($e->getMessage());	
780
			}
781
		}
782
	}
783
	
784
	public function get_fileystem_handler()
785
	{
786
		return $this;
787
	}
788
	
789
	public function get_filesystem($system = "")
790
	{
791
		if($system == "storage_filesystem_append")
792
			return $this->storage_filesystem_append;
0 ignored issues
show
Bug introduced by
The property storage_filesystem_append does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
793
		elseif($system == "tmp_filesystem_append")
794
			return $this->tmp_filesystem_append;
0 ignored issues
show
Bug introduced by
The property tmp_filesystem_append does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
795
		elseif($system == "tmp_filesystem")
796
			return $this->tmp_filesystem;
797
		elseif($system == "storage_filesystem")
798
			return $this->storage_filesystem;
799
		else
800
			return $this->start_filesystem;	
0 ignored issues
show
Bug introduced by
The property start_filesystem does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
801
	}
802
	
803
	public function get_adapter($system)
804
	{
805
		if($system == "tmp_filesystem")
806
			return $this->tmp_adapter;
807
		elseif($system == "storage_filesystem")
808
			return $this->storage_adapter;
809
		else
810
			return $this->start_adapter;	
811
	}
812
	
813
	private function scan_finished()
814
	{
815
		if($this->tmp_filesystem_append->has($this->get_temp_dir_handler()) && $this->tmp_filesystem_append->getSize($this->get_temp_dir_handler()))
0 ignored issues
show
Bug introduced by
The property tmp_filesystem_append does not seem to exist. Did you mean filesystem?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
816
			return false;
817
		
818
		if($this->tmp_filesystem->has($this->get_temp_dir_handler()))
819
			$this->tmp_filesystem->delete($this->get_temp_dir_handler());
820
		
821
		$this->logger->debug(sprintf(("File scan finished")));
822
			
823
		return true;
824
	}
825
	
826
	private function calc_to_bytes($mb_size)
827
	{
828
		return $mb_size*(1024*1024);
829
	}
830
	
831
}
832