parse_amendments()   F
last analyzed

Complexity

Conditions 46
Paths > 20000

Size

Total Lines 202
Code Lines 147

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 46
eloc 147
nc 107521
nop 0
dl 0
loc 202
rs 0
c 0
b 0
f 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
$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(['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 = []; # The bill, page by page, line by line
44
    $clauses = []; # 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 = [
62
    [
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;
74
    $f = file($f);
75
    $page = 1;
76
    $line = -1;
77
    $clause = 0;
78
    $subclause = 0;
79
    $subsubclause = 0;
80
    $intitle = true;
81
    $title = '';
82
    foreach ($f as $r) {
83
        if ($line < 1) {
84
            $line++;
85
            continue;
86
        }
87
        if ($r == "\x0c\n") {
88
            $page++;
89
            $line = -1;
90
            continue;
91
        }
92
        if ($r == "\n") {
93
            continue;
94
        }
95
        if ($intitle) {
96
            $title .= $r;
97
            if (preg_match('#as follows:--#', $r)) {
98
                $intitle = false;
99
            }
100
            continue;
101
        }
102
        if (substr($r, 0, 8) == 'Bill 141') {
103
            continue;
104
        }
105
        if (preg_match('#\s+([1-4]?[05])$#', $r, $m)) {
106
            if ($line != $m[1]) {
107
                print "ERROR! $line $m[1] $r";
108
                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...
109
            }
110
            $r = preg_replace('#\s+[1-4]?[05]$#', '', $r);
111
        }
112
        if (preg_match('#^(\d+)\s+#', $r, $m)) {
113
            $clause = $m[1];
114
            $subclause = 0;
115
            $subsubclause = 0;
116
            $clauses[$clause][$subclause][$subsubclause]['startL'] = $line;
117
            $clauses[$clause][$subclause][$subsubclause]['startP'] = $page;
118
            #$r = preg_replace('#^\d+\s+#', '', $r);
119
        }
120
        if (preg_match('#^\s+\((\d+)\)\s+#', $r, $m)) {
121
            $subclause = $m[1];
122
            $subsubclause = 0;
123
            $clauses[$clause][$subclause][$subsubclause]['startL'] = $line;
124
            $clauses[$clause][$subclause][$subsubclause]['startP'] = $page;
125
            #$r = preg_replace('#^  \(\d+\)\s+#', '', $r);
126
        }
127
        if (preg_match('#^\s+\(([a-h])\)\s+#', $r, $m)) {
128
            $subsubclause = $m[1];
129
            $clauses[$clause][$subclause][$subsubclause]['startL'] = $line;
130
            $clauses[$clause][$subclause][$subsubclause]['startP'] = $page;
131
            #$r = preg_replace('#^  \(\d+\)\s+#', '', $r);
132
        }
133
        $bill[$page][$line] = $r;
134
        $clauses[$clause][$subclause][$subsubclause]['endL'] = $line;
135
        $clauses[$clause][$subclause][$subsubclause]['endP'] = $page;
136
        $line++;
137
    }
138
}
139
140
function read_amendments($f) {
141
    $amendments = [];
142
    $f = file($f);
143
    $line = 1;
144
    $proposer = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $proposer is dead and can be removed.
Loading history...
145
    foreach ($f as $r) {
146
        if ($r == "\n") {
147
            continue;
148
        }
149
        if ($line < 1) {
150
            $line++;
151
            continue;
152
        }
153
        if ($r == "\x0c\n") {
154
            $line = -1;
155
            continue;
156
        }
157
        if (preg_match('#^\S#', $r)) {
158
            $proposer = $r;
159
        } elseif (preg_match('#^\s+(\d+)$#', $r, $m)) {
160
            $number = $m[1];
161
            $amendments[$number] = '';
162
        } elseif (preg_match('#To move the following Clause#', $r)) {
163
            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...
164
            $amendments[$number - 1] = preg_replace('#\n(.*?)$#', '', $amendments[$number - 1]);
165
            $amendments[$number] .= '*' . trim($m[1]) . "*\n$r";
166
        } else {
167
            $amendments[$number] .= $r;
168
        }
169
    }
170
    return $amendments;
171
}
172
173
function parse_amendments() {
174
    global $amendments, $bill, $clauses, $title;
175
    foreach ($amendments as $num => $amendment) {
176
        # Page 8, line 4 [Clause 13], leave out `21-day' and insert `30-day'
177
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) {
178
            $page = $m[1];
179
            $line = $m[2];
180
            $clause = $m[3];
181
            $delete = $m[4];
182
            $insert = $m[5];
183
            unset($amendments[$num]);
184
            $bill[$page][$line] = preg_replace("#(.*)$delete#", "$1<del title='$num'>$delete</del><ins title='$num'>$insert</ins>", $bill[$page][$line]);
185
        }
186
        # Page   2, line 32 [Clause 3], leave out from `make' to end of line 35 and insert `...'
187
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from `(.*?)\' to end of line (\d+) and insert(?:--)?\s+`(.*?)\'#s', $amendment, $m)) {
188
            $page = $m[1];
189
            $line = $m[2];
190
            $clause = $m[3];
191
            $from_text = $m[4];
192
            $end_line = $m[5];
193
            $insert = $m[6];
194
            unset($amendments[$num]);
195
            $bill[$page][$line] = str_replace($from_text, "$from_text <del title='$num'>", $bill[$page][$line]) . '</del><ins title="' . $num . '">' . $insert . '</ins>';
196
            for ($i = $line + 1; $i <= $end_line; $i++) {
197
                $bill[$page][$i] = '<del title="' . $num . '">' . $bill[$page][$i] . '</del>';
198
            }
199
        }
200
        # Page  4, line 9 [Clause 6], leave out from `under' to `creating' and insert `this Part making provision'
201
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from `(.*?)\' to `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) {
202
            $page = $m[1];
203
            $line = $m[2];
204
            $clause = $m[3];
205
            $from_text = $m[4];
206
            $to_text = $m[5];
207
            $insert = $m[6];
208
            unset($amendments[$num]);
209
            $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]);
210
        }
211
        # Page  7, line 1 [Clause 12], leave out from `of' to `the' in line 2 and insert `...'
212
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from `(.*?)\' to `(.*?)\' in line (\d+) and insert `(.*?)\'#s', $amendment, $m)) {
213
            $page = $m[1];
214
            $from_line = $m[2];
215
            $clause = $m[3];
216
            $from_text = $m[4];
217
            $to_text = $m[5];
218
            $to_line = $m[6];
219
            $insert = $m[7];
220
            unset($amendments[$num]);
221
            $bill[$page][$from_line] = str_replace($from_text, "$from_text <del title='$num'>", $bill[$page][$from_line]) . '</del>';
222
            for ($i = $from_line + 1; $i < $to_line; $i++) {
223
                $bill[$page][$i] = '<del title="' . $num . '">' . $bill[$page][$i] . '</del>';
224
            }
225
            $bill[$page][$to_line] = '<del title="' . $num . '">' . str_replace($to_text, "</del><ins title='$num'>$insert</ins> $to_text", $bill[$page][$to_line]);
226
        }
227
        # Page  3, line 13 [Clause 4], leave out from beginning to `confer' and insert `An order under this Part may not make provision to'
228
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out from beginning to `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) {
229
            $page = $m[1];
230
            $line = $m[2];
231
            $clause = $m[3];
232
            $to_text = $m[4];
233
            $insert = $m[5];
234
            unset($amendments[$num]);
235
            $bill[$page][$line] = "<ins title='$num'>$insert</ins><del title='$num'>" . str_replace($to_text, "</del> $to_text", $bill[$page][$line]);
236
        }
237
        # Page   19, line 2 [Clause 34], leave out from `under' to the end of the line and insert `...'
238
        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)) {
239
            $page = $m[1];
240
            $line = $m[2];
241
            $clause = $m[3];
242
            $from_text = $m[4];
243
            $insert = $m[5];
244
            unset($amendments[$num]);
245
            $bill[$page][$line] = str_replace($from_text, "$from_text <del title='$num'>", $bill[$page][$line]) . "</del><ins title='$num'>$insert</ins>";
246
        }
247
        # Page   4 [Clause 7], leave out line 26 and insert `An order under this Part may not make provision to--'
248
        if (preg_match('#Page\s+(\d+) \[Clause (\d+)\], leave out line (\d+)(?: and insert `(.*?)\')?#', $amendment, $m)) {
249
            $page = $m[1];
250
            $line = $m[3];
251
            $clause = $m[2];
252
            $insert = $m[4] ?? null;
253
            unset($amendments[$num]);
254
            $bill[$page][$line] = '<del title="' . $num . '">' . $bill[$page][$line] . '</del>';
255
            if ($insert) {
256
                $bill[$page][$line] .= '<ins title="' . $num . '">' . $insert . '</ins>';
257
            }
258
        }
259
        # Page 8, line 24 [Clause 14], at end insert-- `...'
260
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], (?:at end|after subsection \(\d+\)) insert--\s+`(.*?)\'#s', $amendment, $m)) {
261
            $page = $m[1];
262
            $line = $m[2];
263
            $clause = $m[3];
264
            $insert = $m[4];
265
            unset($amendments[$num]);
266
            $bill[$page][$line] .= '<ins title="' . $num . '">' . $insert . '</ins>';
267
        }
268
        # Title, line    1, leave out `reforming legislation' and insert `...'
269
        if (preg_match('#Title, line.*?, leave out `(.*?)\' and insert `(.*?)\'#s', $amendment, $m)) {
270
            $delete = $m[1];
271
            $insert = $m[2];
272
            unset($amendments[$num]);
273
            $title = str_replace($delete, "<del title='$num'>$delete</del><ins title='$num'>$insert</ins>", $title);
274
        }
275
        # Page 4, line 23 [Clause 6], leave out paragraph (b)
276
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out paragraph \((.*?)\)(?: and insert-- `(.*?)\')?#', $amendment, $m)) {
277
            $page = $m[1];
278
            $line = $m[2];
279
            $clause = $m[3];
280
            $paragraph = $m[4];
0 ignored issues
show
Unused Code introduced by
The assignment to $paragraph is dead and can be removed.
Loading history...
281
            $insert = $m[5] ?? null;
282
            foreach ($clauses[$clause] as $subclause_num => $subclause) {
283
                foreach ($subclause as $subsubclause_num => $subsubclause) {
284
                    $startP = $subsubclause['startP'];
285
                    if ($startP == $page && $subsubclause['startL'] == $line) {
286
                        if ($startP == $subsubclause['endP']) {
287
                            unset($amendments[$num]);
288
                            for ($i = $subsubclause['startL']; $i <= $subsubclause['endL']; $i++) {
289
                                $bill[$page][$i] = '<del title="' . $num . '">' . $bill[$page][$i] . '</del>';
290
                            }
291
                            if ($insert) {
292
                                $bill[$page][$i - 1] .= "<ins title='$num'>$insert</ins>";
293
                            }
294
                        }
295
                    }
296
                }
297
            }
298
        }
299
        # Page 6, line 40 [Clause 12], leave out subsection (3)
300
        if (preg_match('#Page\s+(\d+), line (\d+) \[Clause (\d+)\], leave out subsection \((.*?)\)(?: and insert-- `(.*?)\')?#', $amendment, $m)) {
301
            $page = $m[1];
302
            $line = $m[2];
303
            $clause = $m[3];
304
            $subsection = $m[4];
0 ignored issues
show
Unused Code introduced by
The assignment to $subsection is dead and can be removed.
Loading history...
305
            $insert = $m[5] ?? null;
306
            $finished = false;
307
            foreach ($clauses[$clause] as $subclause_num => $subclause) {
308
                foreach ($subclause as $subsubclause_num => $subsubclause) {
309
                    $startP = $subsubclause['startP'];
310
                    if ($startP == $page && $subsubclause['startL'] == $line) {
311
                        if ($startP == $subsubclause['endP']) {
312
                            unset($amendments[$num]);
313
                            $finished = true;
314
                        }
315
                    }
316
                    if ($finished) {
317
                        for ($i = $subsubclause['startL']; $i <= $subsubclause['endL']; $i++) {
318
                            $bill[$page][$i] = '<del title="' . $num . '">' . $bill[$page][$i] . '</del>';
319
                        }
320
                    }
321
                }
322
                if ($finished) {
323
                    if ($insert) {
324
                        $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...
325
                    }
326
                    break;
327
                }
328
            }
329
        }
330
        # Page 12, line 17, leave out clause 24
331
        if (preg_match('#Page\s+(\d+), line (\d+), leave out clause (\d+)#', $amendment, $m)) {
332
            $page = $m[1];
333
            $line = $m[2];
334
            $clause = $m[3];
335
            $finished = false;
336
            foreach ($clauses[$clause] as $subclause_num => $subclause) {
337
                foreach ($subclause as $subsubclause_num => $subsubclause) {
338
                    if ($subsubclause['startP'] == $page && $subsubclause['startL'] == $line) {
339
                        unset($amendments[$num]);
340
                        $finished = true;
341
                    }
342
                    if ($finished) {
343
                        for ($p = $subsubclause['startP']; $p <= $subsubclause['endP']; $p++) {
344
                            if ($p > $subsubclause['startP']) {
345
                                $starti = 1;
346
                            } else {
347
                                $starti = $subsubclause['startL'];
348
                            }
349
                            for ($i = $starti; $i <= $subsubclause['endL']; $i++) { # XXX Doesn't really work spanning pages
350
                                $bill[$p][$i] = '<del title="' . $num . '">' . $bill[$p][$i] . '</del>';
351
                            }
352
                        }
353
                    }
354
                }
355
            }
356
        }
357
358
        # New clause
359
        if (preg_match('#^\*(.*?)\*\s+(.*?)$#s', $amendment, $m)) {
360
            unset($amendments[$num]);
361
            $page = 0;
362
            $line = 0;
363
            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...
364
                foreach ($subclause as $subsubclause_num => $subsubclause) {
365
                    if ($subsubclause['endP'] > $page) {
366
                        $page = $subsubclause['endP'];
367
                        $line = 0;
368
                    }
369
                    if ($subsubclause['endL'] > $line) {
370
                        $line = $subsubclause['endL'];
371
                    }
372
                }
373
            }
374
            $bill[$page][$line] .= "<ins title='$num'>$amendment</ins>\n\n";
375
        }
376
    }
377
}
378