ArrayCountToken   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 67
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A scoreArgument() 0 4 3
A isLast() 0 4 1
A __toString() 0 4 1
A isCountable() 0 4 2
A hasProperCount() 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
/**
15
 * Array elements count token.
16
 *
17
 * @author Boris Mikhaylov <[email protected]>
18
 */
19
20
class ArrayCountToken implements TokenInterface
21
{
22
    private $count;
23
24
    /**
25
     * @param integer $value
26
     */
27
    public function __construct($value)
28
    {
29
        $this->count = $value;
30
    }
31
32
    /**
33
     * Scores 6 when argument has preset number of elements.
34
     *
35
     * @param $argument
36
     *
37
     * @return bool|int
38
     */
39
    public function scoreArgument($argument)
40
    {
41
        return $this->isCountable($argument) && $this->hasProperCount($argument) ? 6 : false;
42
    }
43
44
    /**
45
     * Returns false.
46
     *
47
     * @return boolean
48
     */
49
    public function isLast()
50
    {
51
        return false;
52
    }
53
54
    /**
55
     * Returns string representation for token.
56
     *
57
     * @return string
58
     */
59
    public function __toString()
60
    {
61
        return sprintf('count(%s)', $this->count);
62
    }
63
64
    /**
65
     * Returns true if object is either array or instance of \Countable
66
     *
67
     * @param $argument
68
     * @return bool
69
     */
70
    private function isCountable($argument)
71
    {
72
        return (is_array($argument) || $argument instanceof \Countable);
73
    }
74
75
    /**
76
     * Returns true if $argument has expected number of elements
77
     *
78
     * @param array|\Countable $argument
79
     *
80
     * @return bool
81
     */
82
    private function hasProperCount($argument)
83
    {
84
        return $this->count === count($argument);
85
    }
86
}
87