Completed
Push — master ( 90fc03...4b579b )
by Daniel
02:38
created

FilesystemEntity::setSSHItentityFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * An entity on the filesystem, used as a base class for Webroot and SSPakFile
5
 */
6
class FilesystemEntity {
7
	protected $server;
8
	protected $path;
9
	protected $executor;
10
	protected $identity = null;
11
12
	function __construct($path, $executor) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
		$this->executor = $executor;
14
15
		if(strpos($path,':') !== false) {
16
			list($this->server,$this->path) = explode(':', $path, 2);
17
		} else {
18
			$this->server = null;
19
			$this->path = $path;
20
		}
21
	}
22
23
	function isLocal() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
		return $this->server == null;
25
	}
26
	function getPath() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
		return $this->path;
28
	}
29
	function getServer() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
		return $this->server;
31
	}
32
	function setSSHItentityFile($filename) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
		$this->identity = $filename;
34
	}
35
36
	/**
37
	 * Execute a command on the relevant server
38
	 * @param  string $command Shell command, either a fully escaped string or an array
39
	 */
40
	function exec($command, $options = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
41
		return $this->createProcess($command, $options)->exec();
42
	}
43
44
	/**
45
	 * Create a process for later exection
46
	 * @param  string $command Shell command, either a fully escaped string or an array
47
	 * @return Process
48
	 */
49
	function createProcess($command, $options = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
50
		if($this->server) {
51
			if ($this->identity && !isset($options['identity'])) {
52
				$options['identity'] = $this->identity;
53
			}
54
			return $this->executor->createRemote($this->server, $command, $options);
55
		}
56
57
		return $this->executor->createLocal($command, $options);
58
	}
59
60
	/**
61
	 * Upload a file to the given destination on the server
62
	 * @param string $file The file to upload
0 ignored issues
show
Bug introduced by
There is no parameter named $file. Was it maybe removed?

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

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

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

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

Loading history...
63
	 * @param string $dest The remote filename/dir to upload to
64
	 */
65 View Code Duplication
	function upload($source, $dest) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
66
		if($this->server) {
67
			$this->executor->execLocal(array("scp", $source, "$this->server:$dest"));
68
		} else {
69
			$this->executor->execLocal(array("cp", $source, $dest));
70
		}
71
	}
72
73
	/**
74
	 * Create a file with the given content at the given destination on the server
75
	 * @param string $content The content of the file
76
	 * @param string $dest The remote filename/dir to upload to
77
	 */
78
	function uploadContent($content, $dest) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
79
		$this->exec("echo " . escapeshellarg($content) . " > " . escapeshellarg($dest));
80
	}
81
82
	/**
83
	 * Download a file from the given source on the server to the given file
84
	 * @param string $source The remote filename to download
85
	 * @param string $dest The local filename/dir to download to
86
	 */
87 View Code Duplication
	function download($source, $dest) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
88
		if($this->server) {
89
			$this->executor->execLocal(array("scp", "$this->server:$source", $dest));
90
		} else {
91
			$this->executor->execLocal(array("cp", $file, $dest));
0 ignored issues
show
Bug introduced by
The variable $file 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...
92
		}
93
	}
94
95
	/**
96
	 * Returns true if the given file or directory exists
97
	 * @param string $file The file/dir to look for
98
	 * @return boolean
99
	 */
100
	function exists($file = null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
101
		if(!$file) $file = $this->path;
0 ignored issues
show
Bug Best Practice introduced by
The expression $file of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null 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...
102
		if($file == '@self') return true;
103
104
		if($this->server) {
105
			$result = $this->exec("if [ -e " . escapeshellarg($file) . " ]; then echo yes; fi");
106
			return (trim($result['output']) == 'yes');
107
108
		} else {
109
			return file_exists($file);
110
		}
111
	}
112
113
	/**
114
	 * Create the given file with the given content
115
	 */
116
	function writeFile($file, $content) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
117
		if($this->server) {
118
			$this->exec("echo " . escapeshellarg($content) . " > " . escapeshellarg($file));
119
120
		} else {
121
			file_put_contents($file, $content);
122
		}
123
	}
124
125
	/**
126
	 * Remove a file or folder from the webroot's server
127
	 *
128
	 * @param string $file The file to remove
129
	 */
130
	function unlink($file) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
131
		if(!$file || $file == '/' || $file == '.') throw new Exception("Can't unlink file '$file'");
132
		$this->exec(array('rm', '-rf', $file));
0 ignored issues
show
Documentation introduced by
array('rm', '-rf', $file) is of type array<integer,string,{"0..."string","2":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
133
		return true;
134
	}
135
}
136
137