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

PhanTextLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parseLines() 0 13 3
A getErrorsOnLine() 9 9 2
A handleNotFoundFile() 0 4 1
A getDescription() 0 4 1
A checkForFile() 0 4 1
A addError() 0 6 1

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
/**
5
 * Class PhanTextLoader
6
 * Used for parsing phan text output
7
 * @package exussum12\CoverageChecker
8
 */
9
class PhanTextLoader implements FileChecker
10
{
11
    protected $lineMatch = '#(?:\./)?(?P<fileName>.*?):(?P<lineNumber>[0-9]+)(?P<message>.*)#';
12
13
    /**
14
     * @var string
15
     */
16
    protected $file;
17
18
    /**
19
     * @var array
20
     */
21
    protected $errors = [];
22
23
    /**
24
     * PhanJsonLoader constructor.
25
     * @param string $file the path to the file containing phan output
26
     */
27
    public function __construct($file)
28
    {
29
        $this->file = $file;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function parseLines()
36
    {
37
        $handle = fopen($this->file, 'r');
38
        while (($line = fgets($handle)) !== false) {
39
            if (!$this->checkForFile($line)) {
40
                continue;
41
            }
42
43
            $this->addError($line);
44
        }
45
46
        return array_keys($this->errors);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 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...
53
    {
54
        $errors = [];
55
        if (isset($this->errors[$file][$lineNumber])) {
56
            $errors = $this->errors[$file][$lineNumber];
57
        }
58
59
        return $errors;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function handleNotFoundFile()
66
    {
67
        return true;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public static function getDescription()
74
    {
75
        return 'Parse the default phan(static analysis) output';
76
    }
77
78
    private function checkForFile($line)
79
    {
80
        return preg_match($this->lineMatch, $line);
81
    }
82
83
    private function addError($line)
84
    {
85
        $matches = [];
86
        preg_match($this->lineMatch, $line, $matches);
87
        $this->errors[$matches['fileName']][$matches['lineNumber']][] = trim($matches['message']);
88
    }
89
}
90