Passed
Push — master ( 74010f...cd30de )
by Tom
03:00
created

text_range()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 17
rs 9.9666
c 1
b 0
f 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
/**
6
 * update README.md with the usage/help information from source
7
 *
8
 * usage: php -f lib/build/usage.php
9
 */
10
11
$readmePath = __DIR__ . '/../../README.md';
12
13
/**
14
 * @param string $buffer
15
 * @param string $start
16
 * @param string $end
17
 *
18
 * @return array|false
19
 */
20
function text_range($buffer, $start, $end)
21
{
22
    $startPos = strpos($buffer, $start);
23
    if (false === $startPos) {
24
        return false;
25
    }
26
27
    $startInner = $startPos + strlen($start);
28
29
    $endPos = strpos($buffer, $end, $startInner);
30
    if (false === $endPos) {
31
        return false;
32
    }
33
34
    $inner = substr($buffer, $startInner, $endPos - $startInner);
35
36
    return array($startPos, $startInner, $endPos, $inner);
37
}
38
39
$buffer = file_get_contents($readmePath);
40
41
$helpStart = "<!-- help -->\n```\n";
42
$helpEnd = "```\n";
43
if (!$helpRange = text_range($buffer, $helpStart, $helpEnd)) {
44
    fwrite(STDERR, "usage.php: failed to find start/end position of help\n");
45
    exit(1);
46
}
47
48
$helpBuffer = file_get_contents(__DIR__ . '/../../src/Utility/Help.php');
49
$usage = text_range($helpBuffer, "<<<'EOD'\n", "\nEOD\n");
50
if (!$usage) {
51
    fwrite(STDERR, "usage.php: failed to find start/end position of usage\n");
52
    exit(1);
53
}
54
$help = text_range(substr($helpBuffer, $usage[1]), "<<<'EOD'\n", "\nEOD\n");
55
if (!$help) {
56
    fwrite(STDERR, "usage.php: failed to find start/end position of usage (2)\n");
57
    exit(1);
58
}
59
$usage = $usage[3] . $help[3];
60
61
$buffer = substr_replace(
62
    $buffer,
63
    $usage,
64
    $helpRange[1],
65
    $helpRange[2] - $helpRange[1]
66
);
67
68
file_put_contents($readmePath, $buffer);
69