1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Ensurance; |
4
|
|
|
|
5
|
|
|
use Dgame\Ensurance\Exception\EnsuranceException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ObjectEnsurance |
9
|
|
|
* @package Dgame\Ensurance |
10
|
|
|
*/ |
11
|
|
|
final class ObjectEnsurance extends ClassEnsurance |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var object |
15
|
|
|
*/ |
16
|
|
|
private $object; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ObjectEnsurance constructor. |
20
|
|
|
* |
21
|
|
|
* @param $object |
22
|
|
|
* |
23
|
|
|
* @throws EnsuranceException |
24
|
|
|
*/ |
25
|
|
|
public function __construct($object) |
26
|
|
|
{ |
27
|
|
|
if (!is_object($object)) { |
28
|
|
|
throw new EnsuranceException('That is not an object'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
parent::__construct(get_class($object)); |
32
|
|
|
|
33
|
|
|
$this->object = $object; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $class |
38
|
|
|
* |
39
|
|
|
* @return ObjectEnsurance |
40
|
|
|
*/ |
41
|
|
|
public function isSome(string $class): ObjectEnsurance |
42
|
|
|
{ |
43
|
|
|
$this->enforce(is_a($this->object, $class))->orThrow('"%s" is not "%s"', get_class($this->object), $class); |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $class |
50
|
|
|
* |
51
|
|
|
* @return ObjectEnsurance |
52
|
|
|
*/ |
53
|
|
|
public function isNotSome(string $class): ObjectEnsurance |
54
|
|
|
{ |
55
|
|
|
$this->enforce(!is_a($this->object, $class))->orThrow('"%s" is "%s"', get_class($this->object), $class); |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $class |
62
|
|
|
* |
63
|
|
|
* @return ObjectEnsurance |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function isInstanceOf($class): ObjectEnsurance |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$class = is_object($class) ? get_class($class) : $class; |
68
|
|
|
$this->enforce($this->object instanceof $class)->orThrow('"%s" is not an instance of "%s"', get_class($this->object), $class); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $class |
75
|
|
|
* |
76
|
|
|
* @return ObjectEnsurance |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function isNotInstanceOf($class): ObjectEnsurance |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$class = is_object($class) ? get_class($class) : $class; |
81
|
|
|
$this->enforce(!($this->object instanceof $class))->orThrow('"%s" is an instance of "%s"', get_class($this->object), $class); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.