AsDefaultValue::getDescription()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\TheParameter\Have;
12
13
use Gabrieljmj\Should\Assert\TheParameter\AbstractParameterAssert;
14
15
class AsDefaultValue extends AbstractParameterAssert
16
{
17
    /**
18
     * @var mixed
19
     */
20
    private $value;
21
    
22
    public function __construct($class, $method, $parameter, $value)
23
    {
24
        parent::__construct($class, $method, $parameter);
25
        $this->value = $value;
26
    }
27
28
    /**
29
     * Executes the assert
30
     *
31
     * @return boolean
32
     */
33
    public function execute()
34
    {
35
        $ref = new \ReflectionClass($this->class);
36
        $method = $ref->getMethod($this->method);
37
        $params = $method->getParameters();
38
39
        foreach ($params as $param) {
40
            if ($param->getName() === $this->parameter) {
41
                if ($param->isDefaultValueAvailable()) {
42
                    return $this->value === $param->getDefaultValue();
43
                }
44
            }
45
        }
46
47
        return false;
48
    }
49
50
    /**
51
     * Creates the fail message
52
     *
53
     * @return string
54
     */
55
    protected function createFailMessage()
56
    {
57
        $class = $this->classToStr($this->class);
58
        return 'The default value of the parameter ' . $this->parameter . ' of the ' . $class . '::' . $this->method . ' is not equal to ' . print_r($this->value, true);
59
    }
60
61
    /**
62
     * Returns the assert description
63
     *
64
     * @return string
65
     */
66
    public function getDescription()
67
    {
68
        return 'Tests if certain parameter of a method has the default value equals determined one';
69
    }
70
}