TypeMatcher::doMatch()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace Peridot\Leo\Matcher;
4
5
use Peridot\Leo\Matcher\Template\ArrayTemplate;
6
use Peridot\Leo\Matcher\Template\TemplateInterface;
7
8
/**
9
 * TypeMatcher determines if an actual value has the same type as the expected type.
10
 *
11
 * @package Peridot\Leo\Matcher
12
 */
13
class TypeMatcher extends AbstractMatcher
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $type;
19
20
    /**
21
     * {@inheritdoc}
22
     *
23
     * @param $actual
24
     * @return Match
25
     */
26
    public function match($actual = '')
27
    {
28
        $match = parent::match($actual);
29
30
        return $match->setActual($this->type);
31
    }
32
33
    /**
34
     * Determine if the actual value has the same type as the expected value. Uses the native gettype()
35
     * function to compare.
36
     *
37
     * @param $actual
38
     * @return bool
39
     */
40
    public function doMatch($actual)
41
    {
42
        $this->type = gettype($actual);
43
44
        return $this->expected === $this->type;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @return TemplateInterface
51
     */
52
    public function getDefaultTemplate()
53
    {
54
        return new ArrayTemplate([
55
            'default' => 'Expected {{expected}}, got {{actual}}',
56
            'negated' => 'Expected a type other than {{expected}}',
57
        ]);
58
    }
59
}
60