Myer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 59
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B calculate() 0 30 3
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\Tokenizer;
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
     * Tokenizer
24
     *
25
     * @var \Hal\Component\Token\Tokenizer
26
     */
27
    private $tokenizer;
28
29
    /**
30
     * Constructor
31
     *
32
     * @param Tokenizer $tokenizer
33
     */
34
    public function __construct(Tokenizer $tokenizer) {
35
        $this->tokenizer = $tokenizer;
36
37
    }
38
39
    /**
40
     * Calculates Myer's interval
41
     *
42
     *      Cyclomatic complexity : Cyclomatic complexity + L
43
     *      where L is the number of logical operators
44
     *
45
     * @param string $filename
46
     * @return Result
47
     */
48
    public function calculate($filename)
49
    {
50
        $mcCabe = new McCabe($this->tokenizer);
51
        $result = new Result;
52
53
        $tokens = $this->tokenizer->tokenize($filename);
54
55
        // Cyclomatic complexity
56
        $cc = $mcCabe->calculate($filename);
57
58
        // Number of operator
59
        $L = 0;
60
        $logicalOperators = array(
61
            T_BOOLEAN_AND => T_BOOLEAN_AND
62
            , T_LOGICAL_AND => T_LOGICAL_AND
63
            , T_BOOLEAN_OR => T_BOOLEAN_OR
64
        , T_LOGICAL_OR => T_LOGICAL_OR
65
        );
66
        foreach($tokens as $token) {
67
            if(isset($logicalOperators[$token->getType()])) {
68
                $L++;
69
            }
70
        }
71
72
        $result
73
            ->setNumberOfOperators($L)
74
            ->setMcCabe($cc);
75
76
        return $result;
77
    }
78
}