Completed
Push — master ( 78b032...4251cd )
by Robin
01:40
created

Exception::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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\Exception;
9
10
use Throwable;
11 2
12 2
/**
13 2
 * @psalm-consistent-constructor
14 2
 */
15
class Exception extends \Exception {
16
	public function __construct(string $message = "", int $code = 0, Throwable $previous = null) {
17 2
		parent::__construct($message, $code, $previous);
18
	}
19
20
	/**
21
	 * @param string|null $path
22
	 * @param string|int|null $error
23
	 * @return Exception
24
	 */
25
	public static function unknown(?string $path, $error): Exception {
26 74
		$message = 'Unknown error (' . (string)$error . ')';
27 74
		if ($path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $path 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...
28 74
			$message .= ' for ' . $path;
29 74
		}
30 36
31
		return new Exception($message, is_int($error) ? $error : 0);
32 38
	}
33
34
	/**
35 2
	 * @param array<int|string, class-string<Exception>> $exceptionMap
36
	 * @param string|int|null $error
37
	 * @param string|null $path
38
	 * @return Exception
39
	 */
40
	public static function fromMap(array $exceptionMap, $error, ?string $path): Exception {
41
		if (isset($exceptionMap[$error])) {
42
			$exceptionClass = $exceptionMap[$error];
43
			if (is_numeric($error)) {
44
				return new $exceptionClass($path, $error);
45
			} else {
46
				return new $exceptionClass($path);
47
			}
48
		} else {
49
			return Exception::unknown($path, $error);
50
		}
51
	}
52
}
53