Passed
Push — master ( 59c59c...91bfb1 )
by Alan
02:13
created

UnexpectedValueException::onFileLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
namespace FigTree\Exceptions;
4
5
use Throwable;
6
use UnexpectedValueException as PHPUnexpectedValueException;
7
use FigTree\Exceptions\Concerns\HasSeverity;
8
use FigTree\Exceptions\Contracts\{
9
	SevereExceptionInterface,
10
	LocatableExceptionInterface,
11
};
12
13
/**
14
 * Exception thrown if a value does not match with a set of values.
15
 * Typically this happens when a function calls another function and expects the return value
16
 * to be of a certain type or value not including arithmetic or buffer related errors.
17
 */
18
class UnexpectedValueException extends PHPUnexpectedValueException implements SevereExceptionInterface, LocatableExceptionInterface
19
{
20
	use HasSeverity;
21
22
	/**
23
	 * Exception thrown if a value does not match with a set of values.
24
	 * Typically this happens when a function calls another function and expects the return value
25
	 * to be of a certain type or value not including arithmetic or buffer related errors.
26
	 *
27
	 * @param string $message The Exception message to throw.
28
	 * @param int $code The Exception code.
29
	 * @param \Throwable $previous The previous throwable used for the exception chaining.
30
	 */
31
	public function __construct(string $message = '', int $code = 0, Throwable $previous = null)
32
	{
33
		parent::__construct($message, $code, $previous);
34
35
		$this->severity = E_ERROR;
36
	}
37
38
	/**
39
	 * If required, set the file and line where the Exception was thrown.
40
	 *
41
	 * @param string $file
42
	 * @param int $line
43
	 *
44
	 * @return $this
45
	 */
46
	public function setLocation(string $file, int $line): LocatableExceptionInterface
47
	{
48
		if (file_exists($file)) {
49
			$this->file = $file;
50
			$this->line = max(0, $line);
51
		}
52
53
		return $this;
54
	}
55
}
56