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

QA/PHP 8.0 | Don't use reserved keywords as param names #561

Merged
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
36 changes: 18 additions & 18 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,19 @@ public function uri_matches(Iri $uri) {
/**
* Check if a cookie is valid for a given domain
*
* @param string $string Domain to check
* @param string $domain Domain to check
* @return boolean Whether the cookie is valid for the given domain
*/
public function domain_matches($string) {
public function domain_matches($domain) {
if (!isset($this->attributes['domain'])) {
// Cookies created manually; cookies created by Requests will set
// the domain to the requested domain
return true;
}

$domain_string = $this->attributes['domain'];
if ($domain_string === $string) {
// The domain string and the string are identical.
$cookie_domain = $this->attributes['domain'];
if ($cookie_domain === $domain) {
// The cookie domain and the passed domain are identical.
return true;
}

Expand All @@ -168,26 +168,26 @@ public function domain_matches($string) {
return false;
}

if (strlen($string) <= strlen($domain_string)) {
// For obvious reasons, the string cannot be a suffix if the domain
// is shorter than the domain string
if (strlen($domain) <= strlen($cookie_domain)) {
// For obvious reasons, the cookie domain cannot be a suffix if the passed domain
// is shorter than the cookie domain
return false;
}

if (substr($string, -1 * strlen($domain_string)) !== $domain_string) {
// The domain string should be a suffix of the string.
if (substr($domain, -1 * strlen($cookie_domain)) !== $cookie_domain) {
// The cookie domain should be a suffix of the passed domain.
return false;
}

$prefix = substr($string, 0, strlen($string) - strlen($domain_string));
$prefix = substr($domain, 0, strlen($domain) - strlen($cookie_domain));
if (substr($prefix, -1) !== '.') {
// The last character of the string that is not included in the
// The last character of the passed domain that is not included in the
// domain string should be a %x2E (".") character.
return false;
}

// The string should be a host name (i.e., not an IP address).
return !preg_match('#^(.+\.)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $string);
// The passed domain should be a host name (i.e., not an IP address).
return !preg_match('#^(.+\.)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $domain);
}

/**
Expand Down Expand Up @@ -365,15 +365,15 @@ public function format_for_set_cookie() {
* is an intentional deviation from RFC 2109 and RFC 2616. RFC 6265
* specifies some of this handling, but not in a thorough manner.
*
* @param string Cookie header value (from a Set-Cookie header)
* @param string $cookie_header Cookie header value (from a Set-Cookie header)
* @return \WpOrg\Requests\Cookie Parsed cookie object
*/
public static function parse($string, $name = '', $reference_time = null) {
$parts = explode(';', $string);
public static function parse($cookie_header, $name = '', $reference_time = null) {
$parts = explode(';', $cookie_header);
$kvparts = array_shift($parts);

if (!empty($name)) {
$value = $string;
$value = $cookie_header;
}
elseif (strpos($kvparts, '=') === false) {
// Some sites might only have a value without the equals separator.
Expand Down
12 changes: 6 additions & 6 deletions src/Cookie/Jar.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,16 @@ public function before_request($url, &$headers, &$data, &$type, &$options) {
/**
* Parse all cookies from a response and attach them to the response
*
* @var \WpOrg\Requests\Response $response
* @param \WpOrg\Requests\Response $response
*/
public function before_redirect_check(Response $return) {
$url = $return->url;
public function before_redirect_check(Response $response) {
$url = $response->url;
if (!$url instanceof Iri) {
$url = new Iri($url);
}

$cookies = Cookie::parse_from_headers($return->headers, $url);
$this->cookies = array_merge($this->cookies, $cookies);
$return->cookies = $this;
$cookies = Cookie::parse_from_headers($response->headers, $url);
$this->cookies = array_merge($this->cookies, $cookies);
$response->cookies = $this;
}
}
64 changes: 32 additions & 32 deletions src/IdnaEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,93 +52,93 @@ class IdnaEncoder {
/**
* Encode a hostname using Punycode
*
* @param string $string Hostname
* @param string $hostname Hostname
* @return string Punycode-encoded hostname
*/
public static function encode($string) {
$parts = explode('.', $string);
public static function encode($hostname) {
$parts = explode('.', $hostname);
foreach ($parts as &$part) {
$part = self::to_ascii($part);
}
return implode('.', $parts);
}

/**
* Convert a UTF-8 string to an ASCII string using Punycode
* Convert a UTF-8 text string to an ASCII string using Punycode
*
* @throws \WpOrg\Requests\Exception Provided string longer than 64 ASCII characters (`idna.provided_too_long`)
* @throws \WpOrg\Requests\Exception Prepared string longer than 64 ASCII characters (`idna.prepared_too_long`)
* @throws \WpOrg\Requests\Exception Provided string already begins with xn-- (`idna.provided_is_prefixed`)
* @throws \WpOrg\Requests\Exception Encoded string longer than 64 ASCII characters (`idna.encoded_too_long`)
*
* @param string $string ASCII or UTF-8 string (max length 64 characters)
* @param string $text ASCII or UTF-8 string (max length 64 characters)
* @return string ASCII string
*/
public static function to_ascii($string) {
// Step 1: Check if the string is already ASCII
if (self::is_ascii($string)) {
public static function to_ascii($text) {
// Step 1: Check if the text is already ASCII
if (self::is_ascii($text)) {
// Skip to step 7
if (strlen($string) < self::MAX_LENGTH) {
return $string;
if (strlen($text) < self::MAX_LENGTH) {
return $text;
}

throw new Exception('Provided string is too long', 'idna.provided_too_long', $string);
throw new Exception('Provided string is too long', 'idna.provided_too_long', $text);
}

// Step 2: nameprep
$string = self::nameprep($string);
$text = self::nameprep($text);

// Step 3: UseSTD3ASCIIRules is false, continue
// Step 4: Check if it's ASCII now
if (self::is_ascii($string)) {
if (self::is_ascii($text)) {
// Skip to step 7
if (strlen($string) < self::MAX_LENGTH) {
return $string;
if (strlen($text) < self::MAX_LENGTH) {
return $text;
}

throw new Exception('Prepared string is too long', 'idna.prepared_too_long', $string);
throw new Exception('Prepared string is too long', 'idna.prepared_too_long', $text);
}

// Step 5: Check ACE prefix
if (strpos($string, self::ACE_PREFIX) === 0) {
throw new Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $string);
if (strpos($text, self::ACE_PREFIX) === 0) {
throw new Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $text);
}

// Step 6: Encode with Punycode
$string = self::punycode_encode($string);
$text = self::punycode_encode($text);

// Step 7: Prepend ACE prefix
$string = self::ACE_PREFIX . $string;
$text = self::ACE_PREFIX . $text;

// Step 8: Check size
if (strlen($string) < self::MAX_LENGTH) {
return $string;
if (strlen($text) < self::MAX_LENGTH) {
return $text;
}

throw new Exception('Encoded string is too long', 'idna.encoded_too_long', $string);
throw new Exception('Encoded string is too long', 'idna.encoded_too_long', $text);
}

/**
* Check whether a given string contains only ASCII characters
* Check whether a given text string contains only ASCII characters
*
* @internal (Testing found regex was the fastest implementation)
*
* @param string $string
* @return bool Is the string ASCII-only?
* @param string $text
* @return bool Is the text string ASCII-only?
*/
protected static function is_ascii($string) {
return (preg_match('/(?:[^\x00-\x7F])/', $string) !== 1);
protected static function is_ascii($text) {
return (preg_match('/(?:[^\x00-\x7F])/', $text) !== 1);
}

/**
* Prepare a string for use as an IDNA name
* Prepare a text string for use as an IDNA name
*
* @todo Implement this based on RFC 3491 and the newer 5891
* @param string $string
* @param string $text
* @return string Prepared string
*/
protected static function nameprep($string) {
return $string;
protected static function nameprep($text) {
return $text;
}

/**
Expand Down
40 changes: 20 additions & 20 deletions src/Iri.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,28 +419,28 @@ protected function remove_dot_segments($input) {
/**
* Replace invalid character with percent encoding
*
* @param string $string Input string
* @param string $text Input string
* @param string $extra_chars Valid characters not in iunreserved or
* iprivate (this is ASCII-only)
* @param bool $iprivate Allow iprivate
* @return string
*/
protected function replace_invalid_with_pct_encoding($string, $extra_chars, $iprivate = false) {
protected function replace_invalid_with_pct_encoding($text, $extra_chars, $iprivate = false) {
// Normalize as many pct-encoded sections as possible
$string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array($this, 'remove_iunreserved_percent_encoded'), $string);
$text = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array($this, 'remove_iunreserved_percent_encoded'), $text);

// Replace invalid percent characters
$string = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $string);
$text = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $text);

// Add unreserved and % to $extra_chars (the latter is safe because all
// pct-encoded sections are now valid).
$extra_chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~%';

// Now replace any bytes that aren't allowed with their pct-encoded versions
$position = 0;
$strlen = strlen($string);
while (($position += strspn($string, $extra_chars, $position)) < $strlen) {
$value = ord($string[$position]);
$strlen = strlen($text);
while (($position += strspn($text, $extra_chars, $position)) < $strlen) {
$value = ord($text[$position]);

// Start position
$start = $position;
Expand Down Expand Up @@ -477,7 +477,7 @@ protected function replace_invalid_with_pct_encoding($string, $extra_chars, $ipr
if ($remaining) {
if ($position + $length <= $strlen) {
for ($position++; $remaining; $position++) {
$value = ord($string[$position]);
$value = ord($text[$position]);

// Check that the byte is valid, then add it to the character:
if (($value & 0xC0) === 0x80) {
Expand Down Expand Up @@ -528,15 +528,15 @@ protected function replace_invalid_with_pct_encoding($string, $extra_chars, $ipr
}

for ($j = $start; $j <= $position; $j++) {
$string = substr_replace($string, sprintf('%%%02X', ord($string[$j])), $j, 1);
$text = substr_replace($text, sprintf('%%%02X', ord($text[$j])), $j, 1);
$j += 2;
$position += 2;
$strlen += 2;
}
}
}

return $string;
return $text;
}

/**
Expand All @@ -545,13 +545,13 @@ protected function replace_invalid_with_pct_encoding($string, $extra_chars, $ipr
* Removes sequences of percent encoded bytes that represent UTF-8
* encoded characters in iunreserved
*
* @param array $match PCRE match
* @param array $regex_match PCRE match
* @return string Replacement
*/
protected function remove_iunreserved_percent_encoded($match) {
protected function remove_iunreserved_percent_encoded($regex_match) {
// As we just have valid percent encoded sequences we can just explode
// and ignore the first member of the returned array (an empty string).
$bytes = explode('%', $match[0]);
$bytes = explode('%', $regex_match[0]);

// Initialize the new string (this is what will be returned) and that
// there are no bytes remaining in the current sequence (unsurprising
Expand Down Expand Up @@ -991,11 +991,11 @@ protected function set_fragment($ifragment) {
/**
* Convert an IRI to a URI (or parts thereof)
*
* @param string|bool IRI to convert (or false from {@see \WpOrg\Requests\IRI::get_iri()})
* @param string|bool $iri IRI to convert (or false from {@see \WpOrg\Requests\IRI::get_iri()})
* @return string|false URI if IRI is valid, false otherwise.
*/
protected function to_uri($string) {
if (!is_string($string)) {
protected function to_uri($iri) {
if (!is_string($iri)) {
return false;
}

Expand All @@ -1005,14 +1005,14 @@ protected function to_uri($string) {
}

$position = 0;
$strlen = strlen($string);
while (($position += strcspn($string, $non_ascii, $position)) < $strlen) {
$string = substr_replace($string, sprintf('%%%02X', ord($string[$position])), $position, 1);
$strlen = strlen($iri);
while (($position += strcspn($iri, $non_ascii, $position)) < $strlen) {
$iri = substr_replace($iri, sprintf('%%%02X', ord($iri[$position])), $position, 1);
$position += 3;
$strlen += 2;
}

return $string;
return $iri;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,12 +802,12 @@ protected static function decode_chunked($data) {
/**
* Convert a key => value array to a 'key: value' array for headers
*
* @param array $array Dictionary of header values
* @param array $dictionary Dictionary of header values
* @return array List of headers
*/
public static function flatten($array) {
public static function flatten($dictionary) {
$return = array();
foreach ($array as $key => $value) {
foreach ($dictionary as $key => $value) {
$return[] = sprintf('%s: %s', $key, $value);
}
return $return;
Expand Down
4 changes: 2 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

date_default_timezone_set('UTC');

function define_from_env($name, $default = false) {
function define_from_env($name, $fallback = false) {
$env = getenv($name);
if ($env) {
define($name, $env);
}
else {
define($name, $default);
define($name, $fallback);
}
}

Expand Down