Skip to content

Commit 44cfa82

Browse files
authored
Merge pull request #650 from WordPress/feature/cs-consistent-control-structures
2 parents ced2e0a + b496a5b commit 44cfa82

18 files changed

Lines changed: 143 additions & 150 deletions

.phpcs.xml.dist

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@
9595
<!-- This repo complies with PSR 0 for filename conventions. -->
9696
<exclude name="WordPress.Files.FileName"/>
9797

98-
<!-- This code base consistently has the second keyword of multi-part control structures
99-
on a new line.
100-
Once WPCS 3.0.0 comes out, the style used in this repo can be enforced via the
101-
Universal.ControlStructures.IfElseDeclaration sniff from PHPCSExtra. -->
102-
<exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterCloseBrace"/>
103-
10498
<!-- WPCS demands long arrays. We'll be using short arrays from now on. -->
10599
<exclude name="Generic.Arrays.DisallowShortArraySyntax"/>
106100

@@ -141,8 +135,6 @@
141135

142136
<!-- Include replacement sniffs which enforce the opposite of WPCS for several excluded sniffs. -->
143137
<rule ref="Squiz.WhiteSpace.ControlStructureSpacing">
144-
<!-- Note: These two violations should potentially be addressed at a later point in time. -->
145-
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.NoLineAfterClose"/>
146138
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.LineAfterClose"/>
147139
</rule>
148140

build/ghpages/UpdateMarkdown.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,7 @@ private function put_contents(string $target, string $contents, string $type = '
339339
if (@mkdir($target_dir, 0777, true) === false) {
340340
throw new RuntimeException(sprintf('Failed to create the %s directory.', $target_dir));
341341
}
342-
}
343-
// phpcs:enable WordPress
342+
} // phpcs:enable WordPress
344343

345344
// Make sure the file always ends on a new line.
346345
$contents = rtrim($contents) . "\n";

src/Cookie.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,7 @@ protected function normalize_attribute($name, $value) {
331331
$delta_seconds = (int) $value;
332332
if ($delta_seconds <= 0) {
333333
$expiry_time = 0;
334-
}
335-
else {
334+
} else {
336335
$expiry_time = $this->reference_time + $delta_seconds;
337336
}
338337

@@ -383,14 +382,14 @@ public function format_for_set_cookie() {
383382
// Ignore non-associative attributes
384383
if (is_numeric($key)) {
385384
$parts[] = $value;
386-
}
387-
else {
385+
} else {
388386
$parts[] = sprintf('%s=%s', $key, $value);
389387
}
390388
}
391389

392390
$header_value .= '; ' . implode('; ', $parts);
393391
}
392+
394393
return $header_value;
395394
}
396395

@@ -423,19 +422,18 @@ public static function parse($cookie_header, $name = '', $reference_time = null)
423422

424423
if (!empty($name)) {
425424
$value = $cookie_header;
426-
}
427-
elseif (strpos($kvparts, '=') === false) {
425+
} elseif (strpos($kvparts, '=') === false) {
428426
// Some sites might only have a value without the equals separator.
429427
// Deviate from RFC 6265 and pretend it was actually a blank name
430428
// (`=foo`)
431429
//
432430
// https://bugzilla.mozilla.org/show_bug.cgi?id=169091
433431
$name = '';
434432
$value = $kvparts;
435-
}
436-
else {
433+
} else {
437434
list($name, $value) = explode('=', $kvparts, 2);
438435
}
436+
439437
$name = trim($name);
440438
$value = trim($value);
441439

@@ -447,8 +445,7 @@ public static function parse($cookie_header, $name = '', $reference_time = null)
447445
if (strpos($part, '=') === false) {
448446
$part_key = $part;
449447
$part_value = true;
450-
}
451-
else {
448+
} else {
452449
list($part_key, $part_value) = explode('=', $part, 2);
453450
$part_value = trim($part_value);
454451
}
@@ -483,8 +480,7 @@ public static function parse_from_headers(Headers $headers, Iri $origin = null,
483480
if (empty($parsed->attributes['domain']) && !empty($origin)) {
484481
$parsed->attributes['domain'] = $origin->host;
485482
$parsed->flags['host-only'] = true;
486-
}
487-
else {
483+
} else {
488484
$parsed->flags['host-only'] = false;
489485
}
490486

@@ -498,19 +494,18 @@ public static function parse_from_headers(Headers $headers, Iri $origin = null,
498494
// the uri-path is not a %x2F ("/") character, output
499495
// %x2F ("/") and skip the remaining steps.
500496
$path = '/';
501-
}
502-
elseif (substr_count($path, '/') === 1) {
497+
} elseif (substr_count($path, '/') === 1) {
503498
// If the uri-path contains no more than one %x2F ("/")
504499
// character, output %x2F ("/") and skip the remaining
505500
// step.
506501
$path = '/';
507-
}
508-
else {
502+
} else {
509503
// Output the characters of the uri-path from the first
510504
// character up to, but not including, the right-most
511505
// %x2F ("/").
512506
$path = substr($path, 0, strrpos($path, '/'));
513507
}
508+
514509
$parsed->attributes['path'] = $path;
515510
}
516511

src/IdnaEncoder.php

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static function encode($hostname) {
6767
foreach ($parts as &$part) {
6868
$part = self::to_ascii($part);
6969
}
70+
7071
return implode('.', $parts);
7172
}
7273

@@ -174,39 +175,31 @@ protected static function utf8_to_codepoints($input) {
174175
for ($position = 0; $position < $strlen; $position++) {
175176
$value = ord($input[$position]);
176177

177-
// One byte sequence:
178-
if ((~$value & 0x80) === 0x80) {
178+
if ((~$value & 0x80) === 0x80) { // One byte sequence:
179179
$character = $value;
180180
$length = 1;
181181
$remaining = 0;
182-
}
183-
// Two byte sequence:
184-
elseif (($value & 0xE0) === 0xC0) {
182+
} elseif (($value & 0xE0) === 0xC0) { // Two byte sequence:
185183
$character = ($value & 0x1F) << 6;
186184
$length = 2;
187185
$remaining = 1;
188-
}
189-
// Three byte sequence:
190-
elseif (($value & 0xF0) === 0xE0) {
186+
} elseif (($value & 0xF0) === 0xE0) { // Three byte sequence:
191187
$character = ($value & 0x0F) << 12;
192188
$length = 3;
193189
$remaining = 2;
194-
}
195-
// Four byte sequence:
196-
elseif (($value & 0xF8) === 0xF0) {
190+
} elseif (($value & 0xF8) === 0xF0) { // Four byte sequence:
197191
$character = ($value & 0x07) << 18;
198192
$length = 4;
199193
$remaining = 3;
200-
}
201-
// Invalid byte:
202-
else {
194+
} else { // Invalid byte:
203195
throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value);
204196
}
205197

206198
if ($remaining > 0) {
207199
if ($position + $length > $strlen) {
208200
throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
209201
}
202+
210203
for ($position++; $remaining > 0; $position++) {
211204
$value = ord($input[$position]);
212205

@@ -218,6 +211,7 @@ protected static function utf8_to_codepoints($input) {
218211
--$remaining;
219212
$character |= ($value & 0x3F) << ($remaining * 6);
220213
}
214+
221215
$position--;
222216
}
223217

@@ -277,25 +271,26 @@ public static function punycode_encode($input) {
277271
// TODO: this should also check if it's valid for a URL
278272
$output .= chr($char);
279273
$h++;
280-
}
281-
// Check if the character is non-ASCII, but below initial n
282-
// This never occurs for Punycode, so ignore in coverage
283-
// @codeCoverageIgnoreStart
284-
elseif ($char < $n) {
274+
275+
// Check if the character is non-ASCII, but below initial n
276+
// This never occurs for Punycode, so ignore in coverage
277+
// @codeCoverageIgnoreStart
278+
} elseif ($char < $n) {
285279
throw new Exception('Invalid character', 'idna.character_outside_domain', $char);
286-
}
287-
// @codeCoverageIgnoreEnd
288-
else {
280+
// @codeCoverageIgnoreEnd
281+
} else {
289282
$extended[$char] = true;
290283
}
291284
}
285+
292286
$extended = array_keys($extended);
293287
sort($extended);
294288
$b = $h;
295289
// [copy them] followed by a delimiter if b > 0
296290
if (strlen($output) > 0) {
297291
$output .= '-';
298292
}
293+
299294
// {if the input contains a non-basic code point < n then fail}
300295
// while h < length(input) do begin
301296
$codepointcount = count($codepoints);
@@ -313,9 +308,7 @@ public static function punycode_encode($input) {
313308
// if c < n then increment delta, fail on overflow
314309
if ($c < $n) {
315310
$delta++;
316-
}
317-
// if c == n then begin
318-
elseif ($c === $n) {
311+
} elseif ($c === $n) { // if c == n then begin
319312
// let q = delta
320313
$q = $delta;
321314
// for k = base to infinity in steps of base do begin
@@ -324,17 +317,17 @@ public static function punycode_encode($input) {
324317
// tmax if k >= bias + tmax, or k - bias otherwise
325318
if ($k <= ($bias + self::BOOTSTRAP_TMIN)) {
326319
$t = self::BOOTSTRAP_TMIN;
327-
}
328-
elseif ($k >= ($bias + self::BOOTSTRAP_TMAX)) {
320+
} elseif ($k >= ($bias + self::BOOTSTRAP_TMAX)) {
329321
$t = self::BOOTSTRAP_TMAX;
330-
}
331-
else {
322+
} else {
332323
$t = $k - $bias;
333324
}
325+
334326
// if q < t then break
335327
if ($q < $t) {
336328
break;
337329
}
330+
338331
// output the code point for digit t + ((q - t) mod (base - t))
339332
$digit = $t + (($q - $t) % (self::BOOTSTRAP_BASE - $t));
340333
$output .= self::digit_to_char($digit);
@@ -375,6 +368,7 @@ protected static function digit_to_char($digit) {
375368
if ($digit < 0 || $digit > 35) {
376369
throw new Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit);
377370
}
371+
378372
// @codeCoverageIgnoreEnd
379373
$digits = 'abcdefghijklmnopqrstuvwxyz0123456789';
380374
return substr($digits, $digit, 1);
@@ -395,11 +389,11 @@ protected static function adapt($delta, $numpoints, $firsttime) {
395389
// if firsttime then let delta = delta div damp
396390
if ($firsttime) {
397391
$delta = floor($delta / self::BOOTSTRAP_DAMP);
398-
}
399-
// else let delta = delta div 2
400-
else {
392+
} else {
393+
// else let delta = delta div 2
401394
$delta = floor($delta / 2);
402395
}
396+
403397
// let delta = delta + (delta div numpoints)
404398
$delta += floor($delta / $numpoints);
405399
// let k = 0

src/Ipv6.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,24 @@ public static function uncompress($ip) {
5858
if (strpos($ip2, '.') !== false) {
5959
$c2++;
6060
}
61-
// ::
61+
6262
if ($c1 === -1 && $c2 === -1) {
63+
// ::
6364
$ip = '0:0:0:0:0:0:0:0';
64-
}
65-
// ::xxx
66-
elseif ($c1 === -1) {
65+
} elseif ($c1 === -1) {
66+
// ::xxx
6767
$fill = str_repeat('0:', 7 - $c2);
6868
$ip = str_replace('::', $fill, $ip);
69-
}
70-
// xxx::
71-
elseif ($c2 === -1) {
69+
} elseif ($c2 === -1) {
70+
// xxx::
7271
$fill = str_repeat(':0', 7 - $c1);
7372
$ip = str_replace('::', $fill, $ip);
74-
}
75-
// xxx::xxx
76-
else {
73+
} else {
74+
// xxx::xxx
7775
$fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
7876
$ip = str_replace('::', $fill, $ip);
7977
}
78+
8079
return $ip;
8180
}
8281

@@ -120,8 +119,7 @@ public static function compress($ip) {
120119

121120
if ($ip_parts[1] !== '') {
122121
return implode(':', $ip_parts);
123-
}
124-
else {
122+
} else {
125123
return $ip_parts[0];
126124
}
127125
}
@@ -144,8 +142,7 @@ private static function split_v6_v4($ip) {
144142
$ipv6_part = substr($ip, 0, $pos);
145143
$ipv4_part = substr($ip, $pos + 1);
146144
return [$ipv6_part, $ipv4_part];
147-
}
148-
else {
145+
} else {
149146
return [$ip, ''];
150147
}
151148
}
@@ -188,6 +185,7 @@ public static function check_ipv6($ip) {
188185
return false;
189186
}
190187
}
188+
191189
if (count($ipv4) === 4) {
192190
foreach ($ipv4 as $ipv4_part) {
193191
$value = (int) $ipv4_part;
@@ -196,9 +194,9 @@ public static function check_ipv6($ip) {
196194
}
197195
}
198196
}
197+
199198
return true;
200-
}
201-
else {
199+
} else {
202200
return false;
203201
}
204202
}

src/Proxy/Http.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,13 @@ final class Http implements Proxy {
6767
public function __construct($args = null) {
6868
if (is_string($args)) {
6969
$this->proxy = $args;
70-
}
71-
elseif (is_array($args)) {
70+
} elseif (is_array($args)) {
7271
if (count($args) === 1) {
7372
list($this->proxy) = $args;
74-
}
75-
elseif (count($args) === 3) {
73+
} elseif (count($args) === 3) {
7674
list($this->proxy, $this->user, $this->pass) = $args;
7775
$this->use_authentication = true;
78-
}
79-
else {
76+
} else {
8077
throw ArgumentCount::create(
8178
'an array with exactly one element or exactly three elements',
8279
count($args),

0 commit comments

Comments
 (0)