DiffParser::doParse()   F
last analyzed

Complexity

Conditions 21
Paths > 20000

Size

Total Lines 112

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 112
rs 0
c 0
b 0
f 0
cc 21
nc 23297
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of Gitonomy.
5
 *
6
 * (c) Alexandre Salomé <[email protected]>
7
 * (c) Julien DIDIER <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
namespace Fabrica\Tools\Programs\Git\Parser;
13
14
use Fabrica\Tools\Programs\Git\Diff\File;
15
use Fabrica\Tools\Programs\Git\Diff\FileChange;
16
17
class DiffParser extends ParserBase
18
{
19
    public $files;
20
21
    protected function doParse()
22
    {
23
        $this->files = array();
24
25
        while (!$this->isFinished()) {
26
            // 1. title
27
            $vars = $this->consumeRegexp('/diff --git (a\/.*) (b\/.*)\n/');
28
            $oldName = $vars[1];
29
            $newName = $vars[2];
30
            $oldIndex = null;
31
            $newIndex = null;
32
            $oldMode = null;
33
            $newMode = null;
34
35
            // 2. mode
36
            if ($this->expects('new file mode ')) {
37
                $newMode = $this->consumeTo("\n");
38
                $this->consumeNewLine();
39
                $oldMode = null;
40
            }
41
            if ($this->expects('old mode ')) {
42
                $oldMode = $this->consumeTo("\n");
43
                $this->consumeNewLine();
44
                $this->consume('new mode ');
45
                $newMode = $this->consumeTo("\n");
46
                $this->consumeNewLine();
47
            }
48
            if ($this->expects('deleted file mode ')) {
49
                $oldMode = $this->consumeTo("\n");
50
                $newMode = null;
51
                $this->consumeNewLine();
52
            }
53
54
            if ($this->expects('similarity index ')) {
55
                $this->consumeRegexp('/\d{1,3}%\n/');
56
                $this->consume('rename from ');
57
                $this->consumeTo("\n");
58
                $this->consumeNewLine();
59
                $this->consume('rename to ');
60
                $this->consumeTo("\n");
61
                $this->consumeNewLine();
62
            }
63
64
            // 4. File informations
65
            $isBinary = false;
66
            if ($this->expects('index ')) {
67
                $oldIndex = $this->consumeShortHash();
68
                $this->consume('..');
69
                $newIndex = $this->consumeShortHash();
70
                if ($this->expects(' ')) {
71
                    $vars = $this->consumeRegexp('/\d{6}/');
72
                    $newMode = $oldMode = $vars[0];
73
                }
74
                $this->consumeNewLine();
75
76
                if ($this->expects('--- ')) {
77
                    $oldName = $this->consumeTo("\n");
78
                    $this->consumeNewLine();
79
                    $this->consume('+++ ');
80
                    $newName = $this->consumeTo("\n");
81
                    $this->consumeNewLine();
82
                } elseif ($this->expects('Binary files ')) {
83
                    $vars = $this->consumeRegexp('/(.*) and (.*) differ\n/');
84
                    $isBinary = true;
85
                    $oldName = $vars[1];
86
                    $newName = $vars[2];
87
                }
88
            }
89
90
            $oldName = $oldName === '/dev/null' ? null : substr($oldName, 2);
91
            $newName = $newName === '/dev/null' ? null : substr($newName, 2);
92
            $oldIndex = preg_match('/^0+$/', $oldIndex) ? null : $oldIndex;
93
            $newIndex = preg_match('/^0+$/', $newIndex) ? null : $newIndex;
94
            $file = new File($oldName, $newName, $oldMode, $newMode, $oldIndex, $newIndex, $isBinary);
95
96
            // 5. Diff
97
            while ($this->expects('@@ ')) {
98
                $vars = $this->consumeRegexp('/-(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))?/');
99
                $rangeOldStart = $vars[1];
100
                $rangeOldCount = $vars[2];
101
                $rangeNewStart = $vars[3];
102
                $rangeNewCount = isset($vars[4]) ? $vars[4] : $vars[2]; // @todo Ici, t'as pris un gros raccourci mon loulou
103
                $this->consume(' @@');
104
                $this->consumeTo("\n");
105
                $this->consumeNewLine();
106
107
                // 6. Lines
108
                $lines = array();
109
                while (true) {
110
                    if ($this->expects(' ')) {
111
                        $lines[] = array(FileChange::LINE_CONTEXT, $this->consumeTo("\n"));
112
                    } elseif ($this->expects('+')) {
113
                        $lines[] = array(FileChange::LINE_ADD, $this->consumeTo("\n"));
114
                    } elseif ($this->expects('-')) {
115
                        $lines[] = array(FileChange::LINE_REMOVE, $this->consumeTo("\n"));
116
                    } elseif ($this->expects("\ No newline at end of file")) {
117
                        // Ignore this case...
118
                    } else {
119
                        break;
120
                    }
121
122
                    $this->consumeNewLine();
123
                }
124
125
                $change = new FileChange($rangeOldStart, $rangeOldCount, $rangeNewStart, $rangeNewCount, $lines);
126
127
                $file->addChange($change);
128
            }
129
130
            $this->files[] = $file;
131
        }
132
    }
133
}
134