|
5 | 5 | class PHPBuilder { |
6 | 6 |
|
7 | 7 | static function build () { |
8 | | - return new PHPBuilder() ; |
| 8 | + return new PHPBuilder(); |
9 | 9 | } |
10 | 10 |
|
11 | 11 | function __construct() { |
12 | | - $this->lines = [] ; |
| 12 | + $this->lines = []; |
13 | 13 | } |
14 | 14 |
|
15 | 15 | function l() { |
16 | | - foreach ( \func_get_args() as $lines ) { |
17 | | - if ( !$lines ) continue ; |
| 16 | + foreach (\func_get_args() as $lines) { |
| 17 | + if (!$lines) continue; |
18 | 18 |
|
19 | | - if ( is_string( $lines ) ) $lines = preg_split( '/\r\n|\r|\n/', $lines ) ; |
20 | | - if ( !$lines ) continue ; |
| 19 | + if (is_string($lines)) $lines = preg_split('/\r\n|\r|\n/', $lines); |
| 20 | + if (!$lines) continue; |
21 | 21 |
|
22 | | - if ( $lines instanceof PHPBuilder ) $lines = $lines->lines ; |
23 | | - else $lines = \array_map( 'ltrim', $lines ) ; |
24 | | - if ( !$lines ) continue ; |
| 22 | + if ($lines instanceof PHPBuilder) $lines = $lines->lines; |
| 23 | + else $lines = \array_map('ltrim', $lines); |
| 24 | + if (!$lines) continue; |
25 | 25 |
|
26 | | - $this->lines = \array_merge( $this->lines, $lines ) ; |
| 26 | + $this->lines = \array_merge($this->lines, $lines); |
27 | 27 | } |
28 | | - return $this ; |
| 28 | + return $this; |
29 | 29 | } |
30 | 30 |
|
31 | 31 | function b() { |
32 | | - $args = \func_get_args() ; |
33 | | - $entry = \array_shift( $args ) ; |
| 32 | + $args = \func_get_args(); |
| 33 | + $entry = \array_shift($args); |
34 | 34 |
|
35 | | - $block = new PHPBuilder() ; |
36 | | - call_user_func_array( array( $block, 'l' ), $args ) ; |
| 35 | + $block = new PHPBuilder(); |
| 36 | + call_user_func_array(array($block, 'l'), $args); |
37 | 37 |
|
38 | | - $this->lines[] = array( $entry, $block->lines ) ; |
| 38 | + $this->lines[] = array($entry, $block->lines); |
39 | 39 |
|
40 | | - return $this ; |
| 40 | + return $this; |
41 | 41 | } |
42 | 42 |
|
43 | | - function replace( $replacements, &$array = \null ) { |
44 | | - if ( $array === \null ) { |
45 | | - unset( $array ) ; |
46 | | - $array =& $this->lines ; |
| 43 | + function replace($replacements, &$array = \null) { |
| 44 | + if ($array === \null) { |
| 45 | + unset($array); |
| 46 | + $array =& $this->lines; |
47 | 47 | } |
48 | 48 |
|
49 | | - $i = 0 ; |
50 | | - while ( $i < \count( $array ) ) { |
| 49 | + $i = 0; |
| 50 | + while ($i < \count($array)) { |
51 | 51 |
|
52 | 52 | /* Recurse into blocks */ |
53 | | - if ( \is_array( $array[$i] ) ) { |
54 | | - $this->replace( $replacements, $array[$i][1] ) ; |
| 53 | + if (\is_array($array[$i])) { |
| 54 | + $this->replace($replacements, $array[$i][1]); |
55 | 55 |
|
56 | | - if ( \count( $array[$i][1] ) == 0 ) { |
57 | | - $nextelse = isset( $array[$i+1] ) && \is_array( $array[$i+1] ) && \preg_match( '/^\s*else\s*$/i', $array[$i+1][0] ) ; |
| 56 | + if (\count($array[$i][1]) == 0) { |
| 57 | + $nextelse = isset($array[$i+1]) && \is_array($array[$i+1]) && \preg_match('/^\s*else\s*$/i', $array[$i+1][0]); |
58 | 58 |
|
59 | | - $delete = \preg_match( '/^\s*else\s*$/i', $array[$i][0] ) ; |
60 | | - $delete = $delete || ( \preg_match( '/^\s*if\s*\(/i', $array[$i][0] ) && !$nextelse ) ; |
| 59 | + $delete = \preg_match('/^\s*else\s*$/i', $array[$i][0]); |
| 60 | + $delete = $delete || (\preg_match('/^\s*if\s*\(/i', $array[$i][0]) && !$nextelse); |
61 | 61 |
|
62 | | - if ( $delete ) { |
| 62 | + if ($delete) { |
63 | 63 | // Is this always safe? Not if the expression has side-effects. |
64 | | - // print "/* REMOVING EMPTY BLOCK: " . $array[$i][0] . "*/\n" ; |
65 | | - \array_splice( $array, $i, 1 ) ; |
66 | | - continue ; |
| 64 | + // print "/* REMOVING EMPTY BLOCK: " . $array[$i][0] . "*/\n"; |
| 65 | + \array_splice($array, $i, 1); |
| 66 | + continue; |
67 | 67 | } |
68 | 68 | } |
69 | | - } |
70 | | - |
71 | | - /* Handle replacing lines with \null to remove, or string, array of strings or PHPBuilder to replace */ |
72 | | - else { |
73 | | - if ( \array_key_exists( $array[$i], $replacements ) ) { |
74 | | - $rep = $replacements[$array[$i]] ; |
75 | | - |
76 | | - if ( $rep === \null ) { |
77 | | - \array_splice( $array, $i, 1 ) ; |
78 | | - continue ; |
| 69 | + } else { |
| 70 | + /* Handle replacing lines with \null to remove, or string, array of strings or PHPBuilder to replace */ |
| 71 | + if (\array_key_exists($array[$i], $replacements)) { |
| 72 | + $rep = $replacements[$array[$i]]; |
| 73 | + |
| 74 | + if ($rep === \null) { |
| 75 | + \array_splice($array, $i, 1); |
| 76 | + continue; |
79 | 77 | } |
80 | 78 |
|
81 | | - if ( is_string( $rep ) ) { |
82 | | - $array[$i] = $rep ; |
83 | | - $i++ ; |
84 | | - continue ; |
| 79 | + if (is_string($rep)) { |
| 80 | + $array[$i] = $rep; |
| 81 | + $i++ ; |
| 82 | + continue; |
85 | 83 | } |
86 | 84 |
|
87 | | - if ( $rep instanceof PHPBuilder ) $rep = $rep->lines ; |
| 85 | + if ($rep instanceof PHPBuilder) $rep = $rep->lines; |
88 | 86 |
|
89 | | - if ( \is_array( $rep ) ) { |
90 | | - \array_splice( $array, $i, 1, $rep ) ; $i += \count( $rep ) + 1 ; |
91 | | - continue ; |
| 87 | + if (\is_array($rep)) { |
| 88 | + \array_splice($array, $i, 1, $rep); $i += \count($rep) + 1; |
| 89 | + continue; |
92 | 90 | } |
93 | 91 |
|
94 | | - throw new \Exception('Unknown type passed to PHPBuilder#replace') ; |
| 92 | + throw new \Exception('Unknown type passed to PHPBuilder#replace'); |
95 | 93 | } |
96 | 94 | } |
97 | 95 |
|
98 | | - $i++ ; |
| 96 | + $i++; |
99 | 97 | } |
100 | 98 |
|
101 | | - return $this ; |
| 99 | + return $this; |
102 | 100 | } |
103 | 101 |
|
104 | | - function render( $array = \null, $indent = "" ) { |
105 | | - if ( $array === \null ) $array = $this->lines ; |
| 102 | + function render($array = \null, $indent = "") { |
106 | 103 |
|
107 | | - $out = [] ; |
108 | | - foreach( $array as $line ) { |
109 | | - if ( \is_array( $line ) ) { |
110 | | - [$entry, $block] = $line ; |
111 | | - $str = $this->render( $block, $indent . "\t" ) ; |
| 104 | + if ($array === \null) { |
| 105 | + $array = $this->lines; |
| 106 | + } |
112 | 107 |
|
113 | | - if ( \strlen( $str ) < 40 ) { |
114 | | - $out[] = $indent . $entry . ' { ' . \ltrim( $str ) . ' }' ; |
| 108 | + $out = []; |
| 109 | + foreach($array as $line) { |
| 110 | + if (\is_array($line)) { |
| 111 | + [$entry, $block] = $line; |
| 112 | + $str = $this->render($block, $indent . "\t"); |
| 113 | + |
| 114 | + if (\strlen($str) < 40) { |
| 115 | + $out[] = $indent . $entry . ' { ' . \ltrim($str) . ' }'; |
| 116 | + } else { |
| 117 | + $out[] = $indent . $entry . ' {'; |
| 118 | + $out[] = $str; |
| 119 | + $out[] = $indent . '}'; |
115 | 120 | } |
116 | | - else { |
117 | | - $out[] = $indent . $entry . ' {' ; |
118 | | - $out[] = $str ; |
119 | | - $out[] = $indent . '}' ; |
120 | | - } |
121 | | - } |
122 | | - else { |
123 | | - $out[] = $indent . $line ; |
| 121 | + } else { |
| 122 | + $out[] = $indent . $line; |
124 | 123 | } |
125 | 124 | } |
126 | 125 |
|
127 | | - return \implode(\PHP_EOL, $out ) ; |
| 126 | + return \implode(\PHP_EOL, $out); |
| 127 | + |
128 | 128 | } |
| 129 | + |
129 | 130 | } |
0 commit comments