Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS: improve control structure layout/consistency #650

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@
<!-- This repo complies with PSR 0 for filename conventions. -->
<exclude name="WordPress.Files.FileName"/>

<!-- This code base consistently has the second keyword of multi-part control structures
on a new line.
Once WPCS 3.0.0 comes out, the style used in this repo can be enforced via the
Universal.ControlStructures.IfElseDeclaration sniff from PHPCSExtra. -->
<exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterCloseBrace"/>

<!-- WPCS demands long arrays. We'll be using short arrays from now on. -->
<exclude name="Generic.Arrays.DisallowShortArraySyntax"/>

Expand Down Expand Up @@ -141,8 +135,6 @@

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

Expand Down
3 changes: 1 addition & 2 deletions build/ghpages/UpdateMarkdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ private function put_contents(string $target, string $contents, string $type = '
if (@mkdir($target_dir, 0777, true) === false) {
throw new RuntimeException(sprintf('Failed to create the %s directory.', $target_dir));
}
}
// phpcs:enable WordPress
} // phpcs:enable WordPress

// Make sure the file always ends on a new line.
$contents = rtrim($contents) . "\n";
Expand Down
27 changes: 11 additions & 16 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ protected function normalize_attribute($name, $value) {
$delta_seconds = (int) $value;
if ($delta_seconds <= 0) {
$expiry_time = 0;
}
else {
} else {
$expiry_time = $this->reference_time + $delta_seconds;
}

Expand Down Expand Up @@ -383,14 +382,14 @@ public function format_for_set_cookie() {
// Ignore non-associative attributes
if (is_numeric($key)) {
$parts[] = $value;
}
else {
} else {
$parts[] = sprintf('%s=%s', $key, $value);
}
}

$header_value .= '; ' . implode('; ', $parts);
}

return $header_value;
}

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

if (!empty($name)) {
$value = $cookie_header;
}
elseif (strpos($kvparts, '=') === false) {
} elseif (strpos($kvparts, '=') === false) {
// Some sites might only have a value without the equals separator.
// Deviate from RFC 6265 and pretend it was actually a blank name
// (`=foo`)
//
// https://bugzilla.mozilla.org/show_bug.cgi?id=169091
$name = '';
$value = $kvparts;
}
else {
} else {
list($name, $value) = explode('=', $kvparts, 2);
}

$name = trim($name);
$value = trim($value);

Expand All @@ -447,8 +445,7 @@ public static function parse($cookie_header, $name = '', $reference_time = null)
if (strpos($part, '=') === false) {
$part_key = $part;
$part_value = true;
}
else {
} else {
list($part_key, $part_value) = explode('=', $part, 2);
$part_value = trim($part_value);
}
Expand Down Expand Up @@ -483,8 +480,7 @@ public static function parse_from_headers(Headers $headers, Iri $origin = null,
if (empty($parsed->attributes['domain']) && !empty($origin)) {
$parsed->attributes['domain'] = $origin->host;
$parsed->flags['host-only'] = true;
}
else {
} else {
$parsed->flags['host-only'] = false;
}

Expand All @@ -498,19 +494,18 @@ public static function parse_from_headers(Headers $headers, Iri $origin = null,
// the uri-path is not a %x2F ("/") character, output
// %x2F ("/") and skip the remaining steps.
$path = '/';
}
elseif (substr_count($path, '/') === 1) {
} elseif (substr_count($path, '/') === 1) {
// If the uri-path contains no more than one %x2F ("/")
// character, output %x2F ("/") and skip the remaining
// step.
$path = '/';
}
else {
} else {
// Output the characters of the uri-path from the first
// character up to, but not including, the right-most
// %x2F ("/").
$path = substr($path, 0, strrpos($path, '/'));
}

$parsed->attributes['path'] = $path;
}

Expand Down
58 changes: 26 additions & 32 deletions src/IdnaEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static function encode($hostname) {
foreach ($parts as &$part) {
$part = self::to_ascii($part);
}

return implode('.', $parts);
}

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

// One byte sequence:
if ((~$value & 0x80) === 0x80) {
if ((~$value & 0x80) === 0x80) { // One byte sequence:
$character = $value;
$length = 1;
$remaining = 0;
}
// Two byte sequence:
elseif (($value & 0xE0) === 0xC0) {
} elseif (($value & 0xE0) === 0xC0) { // Two byte sequence:
$character = ($value & 0x1F) << 6;
$length = 2;
$remaining = 1;
}
// Three byte sequence:
elseif (($value & 0xF0) === 0xE0) {
} elseif (($value & 0xF0) === 0xE0) { // Three byte sequence:
$character = ($value & 0x0F) << 12;
$length = 3;
$remaining = 2;
}
// Four byte sequence:
elseif (($value & 0xF8) === 0xF0) {
} elseif (($value & 0xF8) === 0xF0) { // Four byte sequence:
$character = ($value & 0x07) << 18;
$length = 4;
$remaining = 3;
}
// Invalid byte:
else {
} else { // Invalid byte:
throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value);
}

if ($remaining > 0) {
if ($position + $length > $strlen) {
throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);
}

for ($position++; $remaining > 0; $position++) {
$value = ord($input[$position]);

Expand All @@ -218,6 +211,7 @@ protected static function utf8_to_codepoints($input) {
--$remaining;
$character |= ($value & 0x3F) << ($remaining * 6);
}

$position--;
}

Expand Down Expand Up @@ -277,25 +271,26 @@ public static function punycode_encode($input) {
// TODO: this should also check if it's valid for a URL
$output .= chr($char);
$h++;
}
// Check if the character is non-ASCII, but below initial n
// This never occurs for Punycode, so ignore in coverage
// @codeCoverageIgnoreStart
elseif ($char < $n) {

// Check if the character is non-ASCII, but below initial n
// This never occurs for Punycode, so ignore in coverage
// @codeCoverageIgnoreStart
} elseif ($char < $n) {
throw new Exception('Invalid character', 'idna.character_outside_domain', $char);
}
// @codeCoverageIgnoreEnd
else {
// @codeCoverageIgnoreEnd
} else {
$extended[$char] = true;
}
}

$extended = array_keys($extended);
sort($extended);
$b = $h;
// [copy them] followed by a delimiter if b > 0
if (strlen($output) > 0) {
$output .= '-';
}

// {if the input contains a non-basic code point < n then fail}
// while h < length(input) do begin
$codepointcount = count($codepoints);
Expand All @@ -313,9 +308,7 @@ public static function punycode_encode($input) {
// if c < n then increment delta, fail on overflow
if ($c < $n) {
$delta++;
}
// if c == n then begin
elseif ($c === $n) {
} elseif ($c === $n) { // if c == n then begin
// let q = delta
$q = $delta;
// for k = base to infinity in steps of base do begin
Expand All @@ -324,17 +317,17 @@ public static function punycode_encode($input) {
// tmax if k >= bias + tmax, or k - bias otherwise
if ($k <= ($bias + self::BOOTSTRAP_TMIN)) {
$t = self::BOOTSTRAP_TMIN;
}
elseif ($k >= ($bias + self::BOOTSTRAP_TMAX)) {
} elseif ($k >= ($bias + self::BOOTSTRAP_TMAX)) {
$t = self::BOOTSTRAP_TMAX;
}
else {
} else {
$t = $k - $bias;
}

// if q < t then break
if ($q < $t) {
break;
}

// output the code point for digit t + ((q - t) mod (base - t))
$digit = $t + (($q - $t) % (self::BOOTSTRAP_BASE - $t));
$output .= self::digit_to_char($digit);
Expand Down Expand Up @@ -375,6 +368,7 @@ protected static function digit_to_char($digit) {
if ($digit < 0 || $digit > 35) {
throw new Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit);
}

// @codeCoverageIgnoreEnd
$digits = 'abcdefghijklmnopqrstuvwxyz0123456789';
return substr($digits, $digit, 1);
Expand All @@ -395,11 +389,11 @@ protected static function adapt($delta, $numpoints, $firsttime) {
// if firsttime then let delta = delta div damp
if ($firsttime) {
$delta = floor($delta / self::BOOTSTRAP_DAMP);
}
// else let delta = delta div 2
else {
} else {
// else let delta = delta div 2
$delta = floor($delta / 2);
}

// let delta = delta + (delta div numpoints)
$delta += floor($delta / $numpoints);
// let k = 0
Expand Down
30 changes: 14 additions & 16 deletions src/Ipv6.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,24 @@ public static function uncompress($ip) {
if (strpos($ip2, '.') !== false) {
$c2++;
}
// ::

if ($c1 === -1 && $c2 === -1) {
// ::
$ip = '0:0:0:0:0:0:0:0';
}
// ::xxx
elseif ($c1 === -1) {
} elseif ($c1 === -1) {
// ::xxx
$fill = str_repeat('0:', 7 - $c2);
$ip = str_replace('::', $fill, $ip);
}
// xxx::
elseif ($c2 === -1) {
} elseif ($c2 === -1) {
// xxx::
$fill = str_repeat(':0', 7 - $c1);
$ip = str_replace('::', $fill, $ip);
}
// xxx::xxx
else {
} else {
// xxx::xxx
$fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
$ip = str_replace('::', $fill, $ip);
}

return $ip;
}

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

if ($ip_parts[1] !== '') {
return implode(':', $ip_parts);
}
else {
} else {
return $ip_parts[0];
}
}
Expand All @@ -144,8 +142,7 @@ private static function split_v6_v4($ip) {
$ipv6_part = substr($ip, 0, $pos);
$ipv4_part = substr($ip, $pos + 1);
return [$ipv6_part, $ipv4_part];
}
else {
} else {
return [$ip, ''];
}
}
Expand Down Expand Up @@ -188,6 +185,7 @@ public static function check_ipv6($ip) {
return false;
}
}

if (count($ipv4) === 4) {
foreach ($ipv4 as $ipv4_part) {
$value = (int) $ipv4_part;
Expand All @@ -196,9 +194,9 @@ public static function check_ipv6($ip) {
}
}
}

return true;
}
else {
} else {
return false;
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/Proxy/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,13 @@ final class Http implements Proxy {
public function __construct($args = null) {
if (is_string($args)) {
$this->proxy = $args;
}
elseif (is_array($args)) {
} elseif (is_array($args)) {
if (count($args) === 1) {
list($this->proxy) = $args;
}
elseif (count($args) === 3) {
} elseif (count($args) === 3) {
list($this->proxy, $this->user, $this->pass) = $args;
$this->use_authentication = true;
}
else {
} else {
throw ArgumentCount::create(
'an array with exactly one element or exactly three elements',
count($args),
Expand Down
Loading