CopyPasteDetector   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 62
loc 62
rs 10
c 0
b 0
f 0
wmc 10
lcom 2
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 4 4 1
A getIgnoredArgument() 7 7 2
A getCommand() 7 7 1
B addIssuesFromXml() 26 26 6

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 Finder\Logic\Tools;
3
4
use Finder\Logic\AnalysisResult;
5
use Support\Helps\ArrayHelper;
6
use Sabre\Xml\Reader;
7
8
/**
9
 * Integration of CodeAnalyser with CopyPasteDetector.
10
 *
11
 * @see https://github.com/sebastianbergmann/phpcpd
12
 */
13 View Code Duplication
class CopyPasteDetector extends AbstractTool
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    /**
16
     * @inheritdoc
17
     */
18
    public function getDescription()
19
    {
20
        return 'CopyPasteDetector';
21
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function getIgnoredArgument()
27
    {
28
        if (!empty($this->ignoredPaths)) {
29
            return '--exclude={' . implode(',', $this->ignoredPaths) . '} ';
30
        }
31
        return '';
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function getCommand($targetPaths)
38
    {
39
        return $this->binariesPath . 'phpcpd '
40
            . implode(' ', $targetPaths) . ' '
41
            . $this->getIgnoredArgument() . '--log-pmd="'
42
            . $this->temporaryFilePath . '"';
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    protected function addIssuesFromXml(Reader $xml)
49
    {
50
        $xmlArray = $xml->parse();
51
52
        foreach ((array) $xmlArray['value'] as $duplicationTag) {
53
            if ($duplicationTag['name'] != '{}duplication'
54
                || empty($duplicationTag['value'])
55
            ) {
56
                continue;
57
            }
58
59
            foreach ((array) $duplicationTag['value'] as $fileTag) {
60
                if ($fileTag['name'] != '{}file') {
61
                    continue;
62
                }
63
64
                $fileName = $fileTag['attributes']['path'];
65
                $line = $fileTag['attributes']['line'];
66
                $tool = 'CopyPasteDetector';
67
                $type = 'duplication';
68
                $message = 'Duplicated code';
69
70
                $this->result->addIssue($fileName, $line, $tool, $type, $message);
71
            }
72
        }
73
    }
74
}
75