Completed
Pull Request — master (#88)
by Julius
02:39
created

Server::listShares()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 6.0023

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 24
cts 25
cp 0.96
rs 8.6737
c 0
b 0
f 0
cc 6
nc 17
nop 0
crap 6.0023
1
<?php
2
/**
3
 * Copyright (c) 2014 Robin Appelman <[email protected]>
4
 * This file is licensed under the Licensed under the MIT license:
5
 * http://opensource.org/licenses/MIT
6
 */
7
8
namespace Icewind\SMB\Wrapped;
9
10
use Icewind\SMB\AbstractServer;
11
use Icewind\SMB\Exception\AuthenticationException;
12
use Icewind\SMB\Exception\ConnectException;
13
use Icewind\SMB\Exception\ConnectionException;
14
use Icewind\SMB\Exception\InvalidHostException;
15
use Icewind\SMB\IShare;
16
use Icewind\SMB\ISystem;
17
18
class Server extends AbstractServer {
19
	/**
20
	 * Check if the smbclient php extension is available
21
	 *
22
	 * @param ISystem $system
23
	 * @return bool
24
	 */
25 8
	public static function available(ISystem $system) {
26 8
		return $system->getSmbclientPath();
0 ignored issues
show
Bug Compatibility introduced by
The expression $system->getSmbclientPath(); of type string|boolean adds the type string to the return on line 26 which is incompatible with the return type declared by the interface Icewind\SMB\IServer::available of type boolean.
Loading history...
27
	}
28
29 20
	private function getAuthFileArgument() {
30 20
		if ($this->getAuth()->getUsername()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getAuth()->getUsername() of type string|null is loosely compared to true; 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...
31 20
			return '--authentication-file=' . $this->system->getFD(3);
32
		} else {
33
			return '';
34
		}
35
	}
36
37
	/**
38
	 * @return IShare[]
39
	 *
40
	 * @throws AuthenticationException
41
	 * @throws InvalidHostException
42
	 * @throws ConnectException
43
	 */
44 20
	public function listShares() {
45 20
		$command = sprintf(
46 20
			'%s %s %s -L %s',
47 20
			$this->system->getSmbclientPath(),
48 20
			$this->getAuthFileArgument(),
49 20
			$this->getAuth()->getExtraCommandLineArguments(),
50 20
			escapeshellarg('//' . $this->getHost())
51
		);
52 20
		$connection = new RawConnection($command);
53 20
		$connection->writeAuthentication($this->getAuth()->getUsername(), $this->getAuth()->getPassword());
54 20
		$connection->connect();
55 20
		if (!$connection->isValid()) {
56
			throw new ConnectionException($connection->readLine());
57
		}
58
59 20
		$parser = new Parser($this->timezoneProvider);
0 ignored issues
show
Documentation introduced by
$this->timezoneProvider is of type object<Icewind\SMB\TimeZoneProvider>, 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...
60
61 20
		$output = $connection->readAll();
62 20
		if (isset($output[0])) {
63 12
			$parser->checkConnectionError($output[0]);
64
		}
65
66
		// sometimes we get an empty line first
67 8
		if (count($output) < 2) {
68 8
			$output = $connection->readAll();
69
		}
70
71 8
		if (isset($output[0])) {
72 8
			$parser->checkConnectionError($output[0]);
73
		}
74
75 8
		$shareNames = $parser->parseListShares($output);
76
77 8
		$shares = [];
78 8
		foreach ($shareNames as $name => $description) {
79 8
			$shares[] = $this->getShare($name);
80
		}
81 8
		return $shares;
82
	}
83
84
	/**
85
	 * @param string $name
86
	 * @return IShare
87
	 */
88 1028
	public function getShare($name) {
89 1028
		return new Share($this, $name, $this->system);
90
	}
91
}
92