Loc::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
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\Text\Length;
11
use Hal\Component\Token\Tokenizer;
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
     * Tokenizer
22
     *
23
     * @var \Hal\Component\Token\Tokenizer
24
     */
25
    private $tokenizer;
26
27
    /**
28
     * Constructor
29
     *
30
     * @param Tokenizer $tokenizer
31
     */
32
    public function __construct(Tokenizer $tokenizer) {
33
        $this->tokenizer = $tokenizer;
34
    }
35
36
    /**
37
     * Calculates Lines of code
38
     *
39
     * @param string $filename
40
     * @return Result
41
     */
42
    public function calculate($filename)
43
    {
44
45
        $info = new Result;
46
47
        $tokens = $this->tokenizer->tokenize($filename);
48
        $content = file_get_contents($filename);
49
50
        $cloc = $lloc = 0;
51
        foreach($tokens as $token) {
52
53
            switch($token->getType()) {
54
                case T_STRING:
55
                    if(';' == $token->getValue()) {
56
                        $lloc++;
57
                    }
58
                    break;
59
                case T_COMMENT:
60
                    $cloc++;
61
                    break;
62
                case T_DOC_COMMENT:
63
                    $cloc += count(preg_split('/\r\n|\r|\n/', $token->getValue()));
64
                    break;
65
            }
66
        }
67
68
        $info
69
            ->setLoc(count(preg_split('/\r\n|\r|\n/', $content)) - 1)
70
            ->setCommentLoc($cloc)
71
            ->setLogicalLoc($lloc)
72
        ;
73
74
        return $info;
75
    }
76
}
77