Completed
Pull Request — master (#448)
by
unknown
08:31
created

LengthVisitor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Hal\Metric\Class_\Text;
3
4
use Hal\Metric\FunctionMetric;
5
use Hal\Metric\MetricNullException;
6
use Hal\Metric\Metrics;
7
use Hal\ShouldNotHappenException;
8
use PhpParser\Node;
9
use PhpParser\Node\Stmt;
10
use PhpParser\NodeVisitorAbstract;
11
use PhpParser\PrettyPrinter;
12
13
/**
14
 * @package Hal\Metric\Class_\Text
15
 */
16
class LengthVisitor extends NodeVisitorAbstract
17
{
18
19
    /**
20
     * @var Metrics
21
     */
22
    private $metrics;
23
24
    /**
25
     * @param Metrics $metrics
26
     */
27
    public function __construct(Metrics $metrics)
28
    {
29
        $this->metrics = $metrics;
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function leaveNode(Node $node)
36
    {
37
        if ($node instanceof Stmt\Class_ || $node instanceof Stmt\Function_ || $node instanceof Stmt\Trait_) {
38 View Code Duplication
            if ($node instanceof Stmt\Class_ || $node instanceof Stmt\Trait_) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
                $name = (string)(isset($node->namespacedName) ? $node->namespacedName : 'anonymous@' . spl_object_hash($node));
40
                $classOrFunction = $this->metrics->get($name);
41
            } else {
42
                $name = (string)$node->name;
43
                $classOrFunction = new FunctionMetric($name);
44
                $this->metrics->attach($classOrFunction);
45
            }
46
47
            if ($classOrFunction === null) {
48
                throw new MetricNullException($name, self::class);
49
            }
50
51
            $prettyPrinter = new PrettyPrinter\Standard();
52
            $code = $prettyPrinter->prettyPrintFile([$node]);
53
54
            // count all lines
55
            $allLines = preg_split('/\r\n|\r|\n/', $code);
56
            if ($allLines === false) {
57
                throw new ShouldNotHappenException('Count all lines return false');
58
            }
59
            $loc = count($allLines) - 1;
60
61
            // count and remove multi lines comments
62
            $cloc = 0;
63
            if (preg_match_all('!/\*.*?\*/!s', $code, $matches)) {
64
                foreach ($matches[0] as $match) {
65
                    $matched = preg_split('/\r\n|\r|\n/', $match);
66
                    if ($matched === false) {
67
                        throw new ShouldNotHappenException('Count and remove multi lines comments return false');
68
                    }
69
                    $cloc += max(1, count($matched));
70
                }
71
            }
72
            $code = preg_replace('!/\*.*?\*/!s', '', $code);
73
            if ($code === null) {
74
                throw new ShouldNotHappenException('Preg replace return null');
75
            }
76
77
            // count and remove single line comments
78
            $code = preg_replace_callback('!(\'[^\']*\'|"[^"]*")|((?:#|\/\/).*$)!m', function (array $matches) use (&$cloc) {
79
                if (isset($matches[2])) {
80
                    $cloc += 1;
81
                }
82
                return $matches[1];
83
            }, $code, -1);
84
            if ($code === null) {
85
                throw new ShouldNotHappenException('Count and remove single line comments return null');
86
            }
87
88
            // count and remove empty lines
89
            $emptyLines = preg_replace('!(^\s*[\r\n])!sm', '', $code);
90
            if ($emptyLines === null) {
91
                throw new ShouldNotHappenException('Remove empty lines return null');
92
            }
93
            $code = trim($emptyLines);
94
            $countCode = preg_split('/\r\n|\r|\n/', $code);
95
            if ($countCode === false) {
96
                throw new ShouldNotHappenException('Count empty lines return false');
97
            }
98
99
            $lloc = count($countCode);
100
101
            // save result
102
            $classOrFunction
103
                ->set('cloc', $cloc)
104
                ->set('loc', $loc)
105
                ->set('lloc', $lloc);
106
            $this->metrics->attach($classOrFunction);
107
        }
108
109
        return null;
110
    }
111
}
112