Passed
Pull Request — master (#26)
by Scott
02:11
created

Phpcpd::getErrorsOnLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
class Phpcpd implements FileChecker
5
{
6
    protected $file;
7
8
    protected $duplicateCode = [];
9
10
    public function __construct($file)
11
    {
12
        $this->file = fopen($file, 'r');
13
    }
14
15
    public function parseLines()
16
    {
17
        $block = [];
18
        $this->duplicateCode = [];
19
        while (($line = fgets($this->file)) !== false) {
20
            if (!$this->hasFileName($line)) {
21
                continue;
22
            }
23
24
            if ($this->startOfBlock($line)) {
25
                $this->handleEndOfBlock($block);
26
                $block = [];
27
            }
28
29
            $block += $this->addFoundBlock($line);
30
        }
31
32
        return array_keys($this->duplicateCode);
33
    }
34
35
36 View Code Duplication
    public function getErrorsOnLine($file, $lineNumber)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
37
    {
38
        $errors = [];
39
        if (isset($this->duplicateCode[$file][$lineNumber])) {
40
            $errors = $this->duplicateCode[$file][$lineNumber];
41
        }
42
43
        return $errors;
44
    }
45
46
    public function handleNotFoundFile()
47
    {
48
        return true;
49
    }
50
51
    public static function getDescription()
52
    {
53
        return "Parses the text output from phpcpd (Copy Paste Detect)";
54
    }
55
56
    private function startOfBlock($line)
57
    {
58
        return preg_match('/^\s+-/', $line);
59
    }
60
61
    private function hasFileName($line)
62
    {
63
        return preg_match('/:\d+-\d+/', $line);
64
    }
65
66
    private function addFoundBlock($line)
67
    {
68
        $matches = [];
69
        preg_match('/\s+(?:- )?(?<fileName>.*?):(?<startLine>\d+)-(?<endLine>\d+)$/', $line, $matches);
70
        return [$matches['fileName'] => range($matches['startLine'], $matches['endLine'])];
71
    }
72
73
    private function handleEndOfBlock($block)
74
    {
75
        foreach ($block as $filename => $lines) {
76
            foreach ($lines as $lineNumber) {
77
                foreach ($block as $duplicate => $dupeLines) {
78
                    if ($filename == $duplicate) {
79
                        continue;
80
                    }
81
                    $start = reset($dupeLines);
82
                    $end = end($dupeLines);
83
                    $message = "Duplicate of " . $duplicate . ':' . $start . '-' . $end;
84
                    $this->duplicateCode[$filename][$lineNumber][] = $message;
85
                }
86
            }
87
        }
88
    }
89
}
90