Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of sebastian/lines-of-code.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianBergmann\LinesOfCode;
11
 
12
use function array_merge;
13
use function array_unique;
14
use function count;
15
use PhpParser\Comment;
16
use PhpParser\Node;
17
use PhpParser\Node\Expr;
18
use PhpParser\NodeVisitorAbstract;
19
 
20
final class LineCountingVisitor extends NodeVisitorAbstract
21
{
22
    /**
23
     * @var int
24
     */
25
    private $linesOfCode;
26
 
27
    /**
28
     * @var Comment[]
29
     */
30
    private $comments = [];
31
 
32
    /**
33
     * @var int[]
34
     */
35
    private $linesWithStatements = [];
36
 
37
    public function __construct(int $linesOfCode)
38
    {
39
        $this->linesOfCode = $linesOfCode;
40
    }
41
 
42
    public function enterNode(Node $node): void
43
    {
44
        $this->comments = array_merge($this->comments, $node->getComments());
45
 
46
        if (!$node instanceof Expr) {
47
            return;
48
        }
49
 
50
        $this->linesWithStatements[] = $node->getStartLine();
51
    }
52
 
53
    public function result(): LinesOfCode
54
    {
55
        $commentLinesOfCode = 0;
56
 
57
        foreach ($this->comments() as $comment) {
58
            $commentLinesOfCode += ($comment->getEndLine() - $comment->getStartLine() + 1);
59
        }
60
 
61
        return new LinesOfCode(
62
            $this->linesOfCode,
63
            $commentLinesOfCode,
64
            $this->linesOfCode - $commentLinesOfCode,
65
            count(array_unique($this->linesWithStatements))
66
        );
67
    }
68
 
69
    /**
70
     * @return Comment[]
71
     */
72
    private function comments(): array
73
    {
74
        $comments = [];
75
 
76
        foreach ($this->comments as $comment) {
77
            $comments[$comment->getStartLine() . '_' . $comment->getStartTokenPos() . '_' . $comment->getEndLine() . '_' . $comment->getEndTokenPos()] = $comment;
78
        }
79
 
80
        return $comments;
81
    }
82
}