Equal   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 53
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 16 3
A getDescription() 0 4 1
A createFailMessage() 0 5 2
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\TheProperty\Be;
12
13
use Gabrieljmj\Should\Assert\TheProperty\AbstractPropertyAssert;
14
15
class Equal extends AbstractPropertyAssert
16
{
17
    private $value;
18
19
    public function __construct($class, $property, $value)
20
    {
21
        parent::__construct($class, $property);
22
        $this->value = $value;
23
    }
24
25
    /**
26
     * Executes the assert
27
     *
28
     * @return boolean
29
     */
30
    public function execute()
31
    {
32
        $ref = new \ReflectionClass($this->class);
33
34
        if ($ref->hasProperty($this->property)) {
35
            if (!$ref->getProperty($this->property)->isPublic()) {
36
                $properties = $ref->getDefaultProperties();
37
38
                return $this->value == $properties[$this->property];
39
            }
40
41
            return $this->value == $ref->getProperty($this->property)->getValue();
42
        }
43
44
        return false;
45
    }
46
47
    /**
48
     * Returns the assert description
49
     *
50
     * @return string
51
     */
52
    public function getDescription()
53
    {
54
        return 'Tests if a class has a property with a certain value. If the property is not public, the default value will be used.';
55
    }
56
57
    /**
58
     * Creates the fail message
59
     *
60
     * @return string
61
     */
62
    protected function createFailMessage()
63
    {
64
        $class = is_object($this->class) ? get_class($this->class) : $this->class;
65
        return 'The property ' . $this->property . ' of the class ' . $class . ' is not equal to ' . print_r($this->value, true);
66
    }
67
}