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

UnexpectedTypeException   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 39
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A onFileLine() 0 8 2
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