Completed
Push — master ( e8aa43...f91003 )
by personal
19s queued 13s
created

Loc::calculate()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.439
cc 6
eloc 21
nc 6
nop 2
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\Text\Length;
11
use Hal\Component\Token\TokenCollection;
12
13
/**
14
 * Calculates McCaybe measure
15
 *
16
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
17
 */
18
class Loc {
19
20
    /**
21
     * Calculates Lines of code
22
     *
23
     * @param string $filename
24
     * @param TokenCollection $tokens
25
     * @return Result
26
     */
27
    public function calculate($filename, $tokens)
28
    {
29
30
        $info = new Result;
31
32
        $cloc = $lloc = 0;
33
        foreach($tokens as $token) {
34
35
            switch($token->getType()) {
36
                case T_STRING:
37
                    if(';' == $token->getValue()) {
38
                        $lloc++;
39
                    }
40
                    break;
41
                case T_COMMENT:
42
                    $cloc++;
43
                    break;
44
                case T_DOC_COMMENT:
45
                    $cloc += count(preg_split('/\r\n|\r|\n/', $token->getValue()));
46
                    break;
47
            }
48
        }
49
50
        $content = file_get_contents($filename);
51
        $info
52
            ->setLoc(count(preg_split('/\r\n|\r|\n/', $content)) - 1)
53
            ->setCommentLoc($cloc)
54
            ->setLogicalLoc($lloc)
55
        ;
56
57
        return $info;
58
    }
59
}
60