Completed
Push — master ( 4da4bd...5ab290 )
by Scott
14s
created

Phpcpd   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 86
Duplicated Lines 10.47 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 9
loc 86
rs 10
c 1
b 0
f 0
wmc 17
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parseLines() 0 19 4
A getErrorsOnLine() 9 9 2
A handleNotFoundFile() 0 4 1
A getDescription() 0 4 1
A startOfBlock() 0 4 1
A hasFileName() 0 4 1
A addFoundBlock() 0 6 1
B handleEndOfBlock() 0 16 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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