Passed
Branch dev (a860f8)
by Alan
02:15
created

UnexpectedTypeException::onFileLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FigTree\Exceptions;
4
5
use Throwable;
6
use LogicException;
7
use FigTree\Exceptions\Contracts\{
8
	SevereExceptionInterface,
9
	LocatableExceptionInterface,
10
};
11
use FigTree\Exceptions\Concerns\HasSeverity;
12
13
/**
14
 * Exception thrown when a value is not of an expected type.
15
 */
16
class UnexpectedTypeException extends LogicException implements SevereExceptionInterface, LocatableExceptionInterface
17
{
18
	use HasSeverity;
19
20
	/**
21
	 * Exception thrown when a value is not of an expected type.
22
	 *
23
	 * @param mixed $value Actual value.
24
	 * @param string $expected The name of the expected type.
25
	 * @param int $code The Exception code.
26
	 * @param \Throwable $previous The previous throwable used for the exception chaining.
27
	 */
28
	public function __construct($value, string $expected, int $code = 0, Throwable $previous = null)
29
	{
30
		$message = sprintf(
31
			'Expected value of type %s; %s given.',
32
			$expected,
33
			(is_object($value) ? get_class($value) : gettype($value))
34
		);
35
36
		parent::__construct($message, $code, $previous);
37
	}
38
39
	/**
40
	 * If required, set the file and line where the Exception was thrown.
41
	 *
42
	 * @param string $file
43
	 * @param int $line
44
	 *
45
	 * @return $this
46
	 */
47
	public function onFileLine(string $file, int $line): LocatableExceptionInterface
48
	{
49
		if (file_exists($file)) {
50
			$this->file = $file;
51
			$this->line = max(0, $line);
52
		}
53
54
		return $this;
55
	}
56
}
57