Instance   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getDescription() 0 4 1
A execute() 0 13 3
A createFailMessage() 0 6 1
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
}