Instance::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
/**
3
 * ShouldPHP
4
 *
5
 * @author  Gabriel Jacinto <[email protected]>
6
 * @status  dev
7
 * @link    https://github.com/GabrielJMJ/ShouldPHP
8
 * @license MIT
9
 */
10
 
11
namespace Gabrieljmj\Should\Assert\TheClass\Be;
12
13
use Gabrieljmj\Should\Assert\TheClass\AbstractClassAssert;
14
15
class Instance extends AbstractClassAssert
16
{
17
    private $arg2;
18
    
19
    public function __construct($class, $arg2)
20
    {
21
        parent::__construct($class);
22
        $this->arg2 = $arg2;
23
    }
24
25
    /**
26
     * Returns the assert description
27
     *
28
     * @return string
29
     */
30
    public function getDescription()
31
    {
32
        return 'Tests if the object is of the a class or has this class as one of its parents.';
33
    }
34
35
    /**
36
     * Executes the assert
37
     *
38
     * @return boolean
39
     */
40
    public function execute()
41
    {
42
        $ref = new \ReflectionClass($this->class);
43
        $arg2Ref = new \ReflectionClass($this->arg2);
44
45
        if ($arg2Ref->isInterface()) {
46
            return $ref->implementsInterface($this->arg2);
47
        } elseif ($ref->isSubclassOf($this->arg2)) {
48
            return true;
49
        } else {
50
            return $this->classToStr($this->class) === $this->classToStr($this->arg2);
51
        }
52
    }
53
54
    /**
55
     * Creates the fail message
56
     *
57
     * @return string
58
     */
59
    protected function createFailMessage()
60
    {
61
        $class = $this->classToStr($this->class);
62
        $arg2 = $this->classToStr($this->arg2);
63
        return $class . ' is not instance of ' . $arg2;
64
    }
65
}