TypeToken::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 11

Duplication

Lines 5
Ratio 45.45 %

Importance

Changes 0
Metric Value
dl 5
loc 11
rs 9.9
c 0
b 0
f 0
cc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Prophecy.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *     Marcello Duarte <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Prophecy\Argument\Token;
13
14
use Prophecy\Exception\InvalidArgumentException;
15
16
/**
17
 * Value type token.
18
 *
19
 * @author Konstantin Kudryashov <[email protected]>
20
 */
21
class TypeToken implements TokenInterface
22
{
23
    private $type;
24
25
    /**
26
     * @param string $type
27
     */
28
    public function __construct($type)
29
    {
30
        $checker = "is_{$type}";
31 View Code Duplication
        if (!function_exists($checker) && !interface_exists($type) && !class_exists($type)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
            throw new InvalidArgumentException(sprintf(
33
                'Type or class name expected as an argument to TypeToken, but got %s.', $type
34
            ));
35
        }
36
37
        $this->type = $type;
38
    }
39
40
    /**
41
     * Scores 5 if argument has the same type this token was constructed with.
42
     *
43
     * @param $argument
44
     *
45
     * @return bool|int
46
     */
47
    public function scoreArgument($argument)
48
    {
49
        $checker = "is_{$this->type}";
50
        if (function_exists($checker)) {
51
            return call_user_func($checker, $argument) ? 5 : false;
52
        }
53
54
        return $argument instanceof $this->type ? 5 : false;
55
    }
56
57
    /**
58
     * Returns false.
59
     *
60
     * @return bool
61
     */
62
    public function isLast()
63
    {
64
        return false;
65
    }
66
67
    /**
68
     * Returns string representation for token.
69
     *
70
     * @return string
71
     */
72
    public function __toString()
73
    {
74
        return sprintf('type(%s)', $this->type);
75
    }
76
}
77