CallbackToken   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A scoreArgument() 0 4 2
A isLast() 0 4 1
A __toString() 0 4 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
 * Callback-verified token.
18
 *
19
 * @author Konstantin Kudryashov <[email protected]>
20
 */
21
class CallbackToken implements TokenInterface
22
{
23
    private $callback;
24
25
    /**
26
     * Initializes token.
27
     *
28
     * @param callable $callback
29
     *
30
     * @throws \Prophecy\Exception\InvalidArgumentException
31
     */
32
    public function __construct($callback)
33
    {
34
        if (!is_callable($callback)) {
35
            throw new InvalidArgumentException(sprintf(
36
                'Callable expected as an argument to CallbackToken, but got %s.',
37
                gettype($callback)
38
            ));
39
        }
40
41
        $this->callback = $callback;
42
    }
43
44
    /**
45
     * Scores 7 if callback returns true, false otherwise.
46
     *
47
     * @param $argument
48
     *
49
     * @return bool|int
50
     */
51
    public function scoreArgument($argument)
52
    {
53
        return call_user_func($this->callback, $argument) ? 7 : false;
54
    }
55
56
    /**
57
     * Returns false.
58
     *
59
     * @return bool
60
     */
61
    public function isLast()
62
    {
63
        return false;
64
    }
65
66
    /**
67
     * Returns string representation for token.
68
     *
69
     * @return string
70
     */
71
    public function __toString()
72
    {
73
        return 'callback()';
74
    }
75
}
76