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

Exception   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 38
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A unknown() 0 8 3
A fromMap() 0 12 3
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