TimeZoneProvider::get()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
ccs 16
cts 16
cp 1
rs 8.5706
cc 7
nc 7
nop 1
crap 7
1
<?php
2
/**
3
 * Copyright (c) 2015 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;
9
10
class TimeZoneProvider implements ITimeZoneProvider {
11
	/**
12
	 * @var string[]
13
	 */
14
	private $timeZones = [];
15
16
	/**
17
	 * @var ISystem
18
	 */
19
	private $system;
20
21
	/**
22
	 * @param ISystem $system
23
	 */
24 1010
	public function __construct(ISystem $system) {
25 1010
		$this->system = $system;
26 1010
	}
27
28 504
	public function get(string $host): string {
29 504
		if (!isset($this->timeZones[$host])) {
30 504
			$timeZone = null;
31 504
			$net = $this->system->getNetPath();
32
			// for local domain names we can assume same timezone
33 504
			if ($net && $host && strpos($host, '.') !== false) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $net 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...
34 4
				$command = sprintf(
35 4
					'%s time zone -S %s',
36
					$net,
37 4
					escapeshellarg($host)
38
				);
39 4
				$timeZone = exec($command);
40
			}
41
42 504
			if (!$timeZone) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $timeZone 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...
43 500
				$date = $this->system->getDatePath();
44 500
				if ($date) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date 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...
45 496
					$timeZone = exec($date . " +%z");
46
				} else {
47 4
					$timeZone = date_default_timezone_get();
48
				}
49
			}
50 504
			$this->timeZones[$host] = $timeZone;
51
		}
52 504
		return $this->timeZones[$host];
53
	}
54
}
55