Completed
Push — master ( e8aa43...f91003 )
by personal
11s
created

Myer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Metrics\Complexity\Component\Myer;
11
12
use Hal\Component\Token\TokenCollection;
13
use Hal\Metrics\Complexity\Component\McCabe\McCabe;
14
15
/**
16
 * Calculates myer's interval (extension of McCabe cyclomatic complexity)
17
 *
18
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
19
 */
20
class Myer {
21
22
    /**
23
     * Calculates Myer's interval
24
     *
25
     *      Cyclomatic complexity : Cyclomatic complexity + L
26
     *      where L is the number of logical operators
27
     *
28
     * @param TokenCollection $tokens
29
     * @return Result
30
     */
31
    public function calculate($tokens)
32
    {
33
        $mcCabe = new McCabe();
34
        $result = new Result;
35
36
        // Cyclomatic complexity
37
        $cc = $mcCabe->calculate($tokens);
38
39
        // Number of operator
40
        $L = 0;
41
        $logicalOperators = array(
42
            T_BOOLEAN_AND => T_BOOLEAN_AND
43
            , T_LOGICAL_AND => T_LOGICAL_AND
44
            , T_BOOLEAN_OR => T_BOOLEAN_OR
45
        , T_LOGICAL_OR => T_LOGICAL_OR
46
        );
47
        foreach($tokens as $token) {
48
            if(isset($logicalOperators[$token->getType()])) {
49
                $L++;
50
            }
51
        }
52
53
        $result
54
            ->setNumberOfOperators($L)
55
            ->setMcCabe($cc);
56
57
        return $result;
58
    }
59
}
60