Failed Conditions
Branch master (3ce7e2)
by Nick
14:43
created

parse_amendments()   F

Complexity

Conditions 49
Paths > 20000

Size

Total Lines 160
Code Lines 109

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 49
eloc 109
c 0
b 0
f 0
nc 483841
nop 0
dl 0
loc 160
rs 2

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
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 72 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
$this_page = 'bill_index';
4
include_once '../../../../includes/easyparliament/init.php';
5
$DATA->set_page_metadata($this_page, 'heading','Legislative and Regulatory Reform Bill');
6
$PAGE->page_start();
7
$PAGE->stripe_start();
8
$PAGE->block_start(array ('title'=>'House of Commons - Normal Run'));
9
?>
10
<ul>
11
12
<li><a href="http://www.theyworkforyou.com/debates/?id=2006-01-11a.305.4">First reading</a> was on 11th January 2006. <a
13
href="http://www.publications.parliament.uk/pa/cm200506/cmbills/111/2006111.htm">Legislative and Regulatory Reform Bill (111)</a>.</li>
14
15
<li>The Regulatory Reform Committee published a <a href="http://www.publications.parliament.uk/pa/cm200506/cmselect/cmdereg/878/87802.htm">Special Report</a> on this Bill, on 31st January.</li>
16
17
<li>The Bill had its <a
18
href="http://www.theyworkforyou.com/debates/?id=2006-02-09a.1048.0">second
19
reading</a> on 9th February.</li>
20
21
<li>It then went to <a
22
href="http://www.publications.parliament.uk/pa/cm/cmscleg.htm">Standing
23
Committee A</a>, which debated it on 28th February, and 2nd, 7th, and 9th March.</li>
24
25
<li>After not being amended by Standing Committee A, it was published again: <a
26
href="http://www.publications.parliament.uk/pa/cm200506/cmbills/141/2006141.htm">Legislative and
27
Regulatory Reform Bill (141)</a>.</li>
28
29
<li><a href="http://www.publications.parliament.uk/pa/cm200506/cmselect/cmdereg/1004/100402.htm">The Government's response to the Regulatory Reform Committee's Special Report</a>, 21st March.</li>
30
31
<li>The government proposed some <a href="http://www.cabinetoffice.gov.uk/regulation/documents/pdf/amendments.pdf">amendments to the Bill</a> on 4th May.
32
Unfortunately, this is only in the form of one big PDF with lots of cross-referencing needed to the original Bill 141. So here's TheyWorkForYou's helpful version, showing the differences from the current version to one with all the amendments applied. <del>This means something removed from the Bill</del>, <ins>this means something new added</ins>.
33
34
<style type="text/css">
35
ins { color: #009900; }
36
del { color: #990000; }
37
</style>
38
<?php
39
40
if (file_exists('diff.html')) {
41
    $out = file_get_contents('diff.html');
42
} else {
43
    $bill = array(); # The bill, page by page, line by line
44
    $clauses = array(); # The bill, clause by clause, sub-clause by sub-clause, paragraph by paragraph. Yuk.
45
    parse_bill('2006141.txt');
46
    $amendments = read_amendments('amendments.txt'); # The amendments, by number
47
    parse_amendments();
48
    $out = $title."\n\n";
49
    $out .= "Page,Line\n";
50
    foreach ($bill as $page_num => $page) {
51
        foreach ($page as $line_num => $line) {
52
            $page_num = substr(" $page_num", -2);
53
            $line_num = substr(" $line_num", -2);
54
            $out .= "$page_num,$line_num : $line<br>";
55
        }
56
    }
57
}
58
print "<pre>$out</pre>";
59
print '</ul>';
60
$PAGE->block_end();
61
$includes = array(
62
    array (
63
        'type' => 'include',
64
        'content' => 'bills_intro'
65
    ),
66
);
67
$PAGE->stripe_end($includes);
68
$PAGE->page_end();
69
70
# ---
71
72
function parse_bill($f) {
73
    global $bill, $clauses, $title;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
74
    $f = file($f);
75
    $page = 1; $line = -1;
76
    $clause = 0; $subclause = 0; $subsubclause = 0;
77
    $intitle = true;
78
    $title = '';
79
    foreach ($f as $r) {
80
        if ($line<1) {
81
            $line++;
82
            continue;
83
        }
84
        if ($r == "\x0c\n") {
85
            $page++;
86
            $line = -1;
87
            continue;
88
        }
89
        if ($r == "\n") {
90
            continue;
91
        }
92
        if ($intitle) {
93
            $title .= $r;
94
            if (preg_match('#as follows:--#', $r)) {
95
                $intitle = false;
96
            }
97
            continue;
98
        }
99
        if (substr($r, 0, 8)=='Bill 141') continue;
100
        if (preg_match('#\s+([1-4]?[05])$#', $r, $m)) {
101
            if ($line != $m[1]) {
102
                print "ERROR! $line $m[1] $r";
103
                exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
104
            }
105
            $r = preg_replace('#\s+[1-4]?[05]$#', '', $r);
106
        }
107
        if (preg_match('#^(\d+)\s+#', $r, $m)) {
108
            $clause = $m[1];
109
            $subclause = 0;
110
            $subsubclause = 0;
111
            $clauses[$clause][$subclause][$subsubclause]['startL'] = $line;
112
            $clauses[$clause][$subclause][$subsubclause]['startP'] = $page;
113
            #$r = preg_replace('#^\d+\s+#', '', $r);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
        }
115
        if (preg_match('#^\s+\((\d+)\)\s+#', $r, $m)) {
116
            $subclause = $m[1];
117
            $subsubclause = 0;
118
            $clauses[$clause][$subclause][$subsubclause]['startL'] = $line;
119
            $clauses[$clause][$subclause][$subsubclause]['startP'] = $page;
120
            #$r = preg_replace('#^  \(\d+\)\s+#', '', $r);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
121
        }
122
        if (preg_match('#^\s+\(([a-h])\)\s+#', $r, $m)) {
123
            $subsubclause = $m[1];
124
            $clauses[$clause][$subclause][$subsubclause]['startL'] = $line;
125
            $clauses[$clause][$subclause][$subsubclause]['startP'] = $page;
126
            #$r = preg_replace('#^  \(\d+\)\s+#', '', $r);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
127
        }
128
        $bill[$page][$line] = $r;
129
        $clauses[$clause][$subclause][$subsubclause]['endL'] = $line;
130
        $clauses[$clause][$subclause][$subsubclause]['endP'] = $page;
131
        $line++;
132
    }
133
}
134
135
function read_amendments($f) {
136
    $amendments = array();
137
    $f = file($f);
138
    $line = 1;
139
    $proposer = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $proposer is dead and can be removed.
Loading history...
140
    foreach ($f as $r) {
141
        if ($r == "\n") {
142
            continue;
143
        }
144
        if ($line<1) {
145
            $line++;
146
            continue;
147
        }
148
        if ($r == "\x0c\n") {
149
            $line = -1;
150
            continue;
151
        }
152
        if (preg_match('#^\S#', $r)) {
153
            $proposer = $r;
154
        } elseif (preg_match('#^\s+(\d+)$#', $r, $m)) {
155
            $number = $m[1];
156
            $amendments[$number] = '';
157
        } elseif (preg_match('#To move the following Clause#', $r)) {
158
            preg_match('#\n(.*?)$#', $amendments[$number-1], $m);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $number does not seem to be defined for all execution paths leading up to this point.
Loading history...
159
            $amendments[$number-1] = preg_replace('#\n(.*?)$#', '', $amendments[$number-1]);
160
            $amendments[$number] .= '*' . trim($m[1]) . "*\n$r";
161
        } else {
162
            $amendments[$number] .= $r;
163
        }
164
    }
165
    return $amendments;
166
}
167
168
function parse_amendments() {
169
    global $amendments, $bill, $clauses, $title;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
170
    foreach ($amendments as $num => $amendment) {
171
        # Page 8, line 4 [Clause 13], leave out `21-day' and insert `30-day'
172
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) {
173
            $page = $m[1]; $line = $m[2]; $clause = $m[3];
174
            $delete = $m[4]; $insert = $m[5];
175
            unset($amendments[$num]);
176
            $bill[$page][$line] = preg_replace("#(.*)$delete#", "$1<del title='$num'>$delete</del><ins title='$num'>$insert</ins>", $bill[$page][$line]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $1 seems to be never defined.
Loading history...
177
        }
178
        # Page   2, line 32 [Clause 3], leave out from `make' to end of line 35 and insert `...'
179
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from `(.*?)\' to end of line (\d+) and insert(?:--)?\s+`(.*?)\'#s', $amendment, $m)) {
180
            $page = $m[1]; $line = $m[2]; $clause = $m[3];
181
            $from_text = $m[4]; $end_line = $m[5]; $insert = $m[6];
182
            unset($amendments[$num]);
183
            $bill[$page][$line] = str_replace($from_text, "$from_text <del title='$num'>", $bill[$page][$line]) . '</del><ins title="'.$num.'">' . $insert . '</ins>';
184
            for ($i=$line+1; $i<=$end_line; $i++) {
185
                $bill[$page][$i] = '<del title="'.$num.'">' . $bill[$page][$i] . '</del>';
186
            }
187
        }
188
        # Page  4, line 9 [Clause 6], leave out from `under' to `creating' and insert `this Part making provision'
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
189
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from `(.*?)\' to `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) {
190
            $page = $m[1]; $line = $m[2]; $clause = $m[3];
191
            $from_text = $m[4]; $to_text = $m[5]; $insert = $m[6];
192
            unset($amendments[$num]);
193
            $bill[$page][$line] = preg_replace("#$from_text(.*?)$to_text#", "$from_text <del title='$num'>$1</del><ins title='$num'>$insert</ins> $to_text", $bill[$page][$line]);
194
        }
195
        # Page  7, line 1 [Clause 12], leave out from `of' to `the' in line 2 and insert `...'
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
196
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from `(.*?)\' to `(.*?)\' in line (\d+) and insert `(.*?)\'#s', $amendment, $m)) {
197
            $page = $m[1]; $from_line = $m[2]; $clause = $m[3];
198
            $from_text = $m[4]; $to_text = $m[5]; $to_line = $m[6];
199
            $insert = $m[7];
200
            unset($amendments[$num]);
201
            $bill[$page][$from_line] = str_replace($from_text, "$from_text <del title='$num'>", $bill[$page][$from_line]) . '</del>';
202
            for ($i=$from_line+1; $i<$to_line; $i++) {
203
                $bill[$page][$i] = '<del title="'.$num.'">' . $bill[$page][$i] . '</del>';
204
            }
205
            $bill[$page][$to_line] = '<del title="'.$num.'">' . str_replace($to_text, "</del><ins title='$num'>$insert</ins> $to_text", $bill[$page][$to_line]);
206
        }
207
        # Page  3, line 13 [Clause 4], leave out from beginning to `confer' and insert `An order under this Part may not make provision to'
208
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from beginning to `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) {
209
            $page = $m[1]; $line = $m[2]; $clause = $m[3];
210
            $to_text = $m[4]; $insert = $m[5];
211
            unset($amendments[$num]);
212
            $bill[$page][$line] = "<ins title='$num'>$insert</ins><del title='$num'>" . str_replace($to_text, "</del> $to_text", $bill[$page][$line]);
213
        }
214
        # Page   19, line 2 [Clause 34], leave out from `under' to the end of the line and insert `...'
215
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from `(.*?)\' to the end of the line and insert\s+`(.*?)\'#s', $amendment, $m)) {
216
            $page = $m[1]; $line = $m[2]; $clause = $m[3];
217
            $from_text = $m[4]; $insert = $m[5];
218
            unset($amendments[$num]);
219
            $bill[$page][$line] = str_replace($from_text, "$from_text <del title='$num'>", $bill[$page][$line]) . "</del><ins title='$num'>$insert</ins>";
220
        }
221
        # Page   4 [Clause 7], leave out line 26 and insert `An order under this Part may not make provision to--'
222
        if (preg_match('#Page\s+(\d+) \[Clause (\d+)\], leave out line (\d+)(?: and insert `(.*?)\')?#', $amendment, $m)) {
223
            $page = $m[1]; $line = $m[3]; $clause = $m[2];
224
            $insert = isset($m[4]) ? $m[4] : null;
225
            unset($amendments[$num]);
226
            $bill[$page][$line] = '<del title="'.$num.'">'.$bill[$page][$line].'</del>';
227
            if ($insert) $bill[$page][$line] .= '<ins title="'.$num.'">'.$insert.'</ins>';
228
        }
229
        # Page 8, line 24 [Clause 14], at end insert-- `...'
230
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], (?:at end|after subsection \(\d+\)) insert--\s+`(.*?)\'#s', $amendment, $m)) {
231
            $page = $m[1]; $line = $m[2]; $clause = $m[3];
232
            $insert = $m[4];
233
            unset($amendments[$num]);
234
            $bill[$page][$line] .= '<ins title="'.$num.'">'.$insert.'</ins>';
235
        }
236
        # Title, line    1, leave out `reforming legislation' and insert `...'
237
        if (preg_match('#Title, line.*?, leave out `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) {
238
            $delete = $m[1]; $insert = $m[2];
239
            unset($amendments[$num]);
240
            $title = str_replace($delete, "<del title='$num'>$delete</del><ins title='$num'>$insert</ins>", $title);
241
        }
242
        # Page 4, line 23 [Clause 6], leave out paragraph (b)
243
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out paragraph \((.*?)\)(?: and insert-- `(.*?)\')?#', $amendment, $m)) {
244
            $page = $m[1]; $line = $m[2]; $clause = $m[3];
245
            $paragraph = $m[4];
0 ignored issues
show
Unused Code introduced by
The assignment to $paragraph is dead and can be removed.
Loading history...
246
            $insert = isset($m[5]) ? $m[5] : null;
247
            foreach ($clauses[$clause] as $subclause_num => $subclause) {
248
                foreach ($subclause as $subsubclause_num => $subsubclause) {
249
                    $startP = $subsubclause['startP'];
250
                    if ($startP==$page && $subsubclause['startL']==$line) {
251
                        if ($startP == $subsubclause['endP']) {
252
                            unset($amendments[$num]);
253
                            for ($i = $subsubclause['startL']; $i<=$subsubclause['endL']; $i++) {
254
                                $bill[$page][$i] = '<del title="'.$num.'">' . $bill[$page][$i] . '</del>';
255
                            }
256
                            if ($insert) {
257
                                $bill[$page][$i-1] .= "<ins title='$num'>$insert</ins>";
258
                            }
259
                        }
260
                    }
261
                }
262
            }
263
        }
264
        # Page 6, line 40 [Clause 12], leave out subsection (3)
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
265
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out subsection \((.*?)\)(?: and insert-- `(.*?)\')?#', $amendment, $m)) {
266
            $page = $m[1]; $line = $m[2]; $clause = $m[3];
267
            $subsection = $m[4];
0 ignored issues
show
Unused Code introduced by
The assignment to $subsection is dead and can be removed.
Loading history...
268
            $insert = isset($m[5]) ? $m[5] : null;
269
            $finished = false;
270
            foreach ($clauses[$clause] as $subclause_num => $subclause) {
271
                foreach ($subclause as $subsubclause_num => $subsubclause) {
272
                    $startP = $subsubclause['startP'];
273
                    if ($startP==$page && $subsubclause['startL']==$line) {
274
                        if ($startP == $subsubclause['endP']) {
275
                            unset($amendments[$num]);
276
                            $finished = true;
277
                        }
278
                    }
279
                    if ($finished) {
280
                        for ($i = $subsubclause['startL']; $i<=$subsubclause['endL']; $i++) {
281
                            $bill[$page][$i] = '<del title="'.$num.'">' . $bill[$page][$i] . '</del>';
282
                        }
283
                    }
284
                }
285
                if ($finished) {
286
                    if ($insert) {
287
                        $bill[$page][$i-1] .= "<ins title='$num'>$insert</ins>";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $i does not seem to be defined for all execution paths leading up to this point.
Loading history...
288
                    }
289
                    break;
290
                }
291
            }
292
        }
293
        # Page 12, line 17, leave out clause 24
294
        if (preg_match('#Page\s+(\d+), line (\d+), leave out clause (\d+)#', $amendment, $m)) {
295
            $page = $m[1]; $line = $m[2]; $clause = $m[3];
296
            $finished = false;
297
            foreach ($clauses[$clause] as $subclause_num => $subclause) {
298
                foreach ($subclause as $subsubclause_num => $subsubclause) {
299
                    if ($subsubclause['startP']==$page && $subsubclause['startL']==$line) {
300
                        unset($amendments[$num]);
301
                        $finished = true;
302
                    }
303
                    if ($finished) {
304
                        for ($p = $subsubclause['startP']; $p<=$subsubclause['endP']; $p++) {
305
                            if ($p>$subsubclause['startP']) $starti = 1;
306
                            else $starti = $subsubclause['startL'];
307
                            for ($i = $starti; $i<=$subsubclause['endL']; $i++) { # XXX Doesn't really work spanning pages
308
                                $bill[$p][$i] = '<del title="'.$num.'">' . $bill[$p][$i] . '</del>';
309
                            }
310
                        }
311
                    }
312
                }
313
            }
314
        }
315
316
        # New clause
317
        if (preg_match('#^\*(.*?)\*\s+(.*?)$#s', $amendment, $m)) {
318
            unset($amendments[$num]);
319
            $page = 0;
320
            $line = 0;
321
            foreach ($clauses[$clause] as $subclause_num => $subclause) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $clause does not seem to be defined for all execution paths leading up to this point.
Loading history...
322
                foreach ($subclause as $subsubclause_num => $subsubclause) {
323
                    if ($subsubclause['endP'] > $page) { $page = $subsubclause['endP']; $line = 0; }
324
                    if ($subsubclause['endL'] > $line) $line = $subsubclause['endL'];
325
                }
326
            }
327
            $bill[$page][$line] .= "<ins title='$num'>$amendment</ins>\n\n";
328
        }
329
    }
330
}
331