aboutsummaryrefslogtreecommitdiffstats
path: root/src/IRI.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/IRI.php')
-rw-r--r--src/IRI.php210
1 files changed, 93 insertions, 117 deletions
diff --git a/src/IRI.php b/src/IRI.php
index ce4c2c2..7fc538c 100644
--- a/src/IRI.php
+++ b/src/IRI.php
@@ -1,88 +1,53 @@
<?php
+// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
+// SPDX-FileCopyrightText: 2008 Steve Minutillo
+// SPDX-License-Identifier: BSD-3-Clause
+
declare(strict_types=1);
-/**
- * SimplePie
- *
- * A PHP-Based RSS and Atom Feed Framework.
- * Takes the hard work out of managing a complete RSS/Atom solution.
- *
- * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification, are
- * permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright notice, this list of
- * conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above copyright notice, this list
- * of conditions and the following disclaimer in the documentation and/or other materials
- * provided with the distribution.
- *
- * * Neither the name of the SimplePie Team nor the names of its contributors may be used
- * to endorse or promote products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
- * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
- * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- * @package SimplePie
- * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
- * @author Ryan Parman
- * @author Sam Sneddon
- * @author Ryan McCue
- * @link http://simplepie.org/ SimplePie
- * @license http://www.opensource.org/licenses/bsd-license.php BSD License
- */
namespace SimplePie;
/**
* IRI parser/serialiser/normaliser
*
- * @package SimplePie
- * @subpackage HTTP
- * @author Sam Sneddon
- * @author Steve Minutillo
- * @author Ryan McCue
- * @copyright 2007-2012 Sam Sneddon, Steve Minutillo, Ryan McCue
- * @license http://www.opensource.org/licenses/bsd-license.php
+ * @property ?string $scheme
+ * @property ?string $userinfo
+ * @property ?string $host
+ * @property ?int $port
+ * @property-write int|string|null $port
+ * @property ?string $authority
+ * @property string $path
+ * @property ?string $query
+ * @property ?string $fragment
*/
class IRI
{
/**
* Scheme
*
- * @var string
+ * @var ?string
*/
protected $scheme = null;
/**
* User Information
*
- * @var string
+ * @var ?string
*/
protected $iuserinfo = null;
/**
* ihost
*
- * @var string
+ * @var ?string
*/
protected $ihost = null;
/**
* Port
*
- * @var string
+ * @var ?int
*/
protected $port = null;
@@ -96,14 +61,14 @@ class IRI
/**
* iquery
*
- * @var string
+ * @var ?string
*/
protected $iquery = null;
/**
* ifragment
*
- * @var string
+ * @var ?string
*/
protected $ifragment = null;
@@ -112,6 +77,8 @@ class IRI
*
* Each key is the scheme, each value is an array with each key as the IRI
* part and value as the default value for that part.
+ *
+ * @var array<string, array<string, mixed>>
*/
protected $normalization = [
'acap' => [
@@ -140,7 +107,7 @@ class IRI
*/
public function __toString()
{
- return $this->get_iri();
+ return (string) $this->get_iri();
}
/**
@@ -148,11 +115,13 @@ class IRI
*
* @param string $name Property name
* @param mixed $value Property value
+ * @return void
*/
- public function __set($name, $value)
+ public function __set(string $name, $value)
{
- if (method_exists($this, 'set_' . $name)) {
- call_user_func([$this, 'set_' . $name], $value);
+ $callable = [$this, 'set_' . $name];
+ if (is_callable($callable)) {
+ call_user_func($callable, $value);
} elseif (
$name === 'iauthority'
|| $name === 'iuserinfo'
@@ -171,7 +140,7 @@ class IRI
* @param string $name Property name
* @return mixed
*/
- public function __get($name)
+ public function __get(string $name)
{
// isset() returns false for null, we don't want to do that
// Also why we use array_key_exists below instead of isset()
@@ -188,7 +157,7 @@ class IRI
$return = $this->$name;
}
// host -> ihost
- elseif (($prop = 'i' . $name) && array_key_exists($prop, $props)) {
+ elseif (array_key_exists($prop = 'i' . $name, $props)) {
$name = $prop;
$return = $this->$prop;
}
@@ -214,7 +183,7 @@ class IRI
* @param string $name Property name
* @return bool
*/
- public function __isset($name)
+ public function __isset(string $name)
{
return method_exists($this, 'get_' . $name) || isset($this->$name);
}
@@ -223,26 +192,29 @@ class IRI
* Overload __unset() to provide access via properties
*
* @param string $name Property name
+ * @return void
*/
- public function __unset($name)
+ public function __unset(string $name)
{
- if (method_exists($this, 'set_' . $name)) {
- call_user_func([$this, 'set_' . $name], '');
+ $callable = [$this, 'set_' . $name];
+ if (is_callable($callable)) {
+ call_user_func($callable, '');
}
}
/**
* Create a new IRI object, from a specified string
*
- * @param string $iri
+ * @param string|null $iri
*/
- public function __construct($iri = null)
+ public function __construct(?string $iri = null)
{
$this->set_iri($iri);
}
/**
* Clean up
+ * @return void
*/
public function __destruct()
{
@@ -322,27 +294,21 @@ class IRI
* Parse an IRI into scheme/authority/path/query/fragment segments
*
* @param string $iri
- * @return array
+ * @return array{
+ * scheme: string|null,
+ * authority: string|null,
+ * path: string,
+ * query: string|null,
+ * fragment: string|null,
+ * }|false
*/
- protected function parse_iri($iri)
+ protected function parse_iri(string $iri)
{
$iri = trim($iri, "\x20\x09\x0A\x0C\x0D");
- if (preg_match('/^((?P<scheme>[^:\/?#]+):)?(\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$/', $iri, $match)) {
- if ($match[1] === '') {
- $match['scheme'] = null;
- }
- if (!isset($match[3]) || $match[3] === '') {
- $match['authority'] = null;
- }
- if (!isset($match[5])) {
- $match['path'] = '';
- }
- if (!isset($match[6]) || $match[6] === '') {
- $match['query'] = null;
- }
- if (!isset($match[8]) || $match[8] === '') {
- $match['fragment'] = null;
- }
+ if (preg_match('/^(?:(?P<scheme>[^:\/?#]+):)?(:?\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(?:\?(?P<query>[^#]*))?(?:#(?P<fragment>.*))?$/', $iri, $match, \PREG_UNMATCHED_AS_NULL)) {
+ // TODO: Remove once we require PHP ≥ 7.4.
+ $match['query'] = $match['query'] ?? null;
+ $match['fragment'] = $match['fragment'] ?? null;
return $match;
}
@@ -356,7 +322,7 @@ class IRI
* @param string $input
* @return string
*/
- protected function remove_dot_segments($input)
+ protected function remove_dot_segments(string $input)
{
$output = '';
while (strpos($input, './') !== false || strpos($input, '/.') !== false || $input === '.' || $input === '..') {
@@ -405,13 +371,15 @@ class IRI
* @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(string $string, string $extra_chars, bool $iprivate = false)
{
// Normalize as many pct-encoded sections as possible
$string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', [$this, 'remove_iunreserved_percent_encoded'], $string);
+ \assert(\is_string($string), "For PHPStan: Should not occur, the regex is valid");
// Replace invalid percent characters
$string = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $string);
+ \assert(\is_string($string), "For PHPStan: Should not occur, the regex is valid");
// Add unreserved and % to $extra_chars (the latter is safe because all
// pct-encoded sections are now valid).
@@ -526,10 +494,10 @@ class IRI
* Removes sequences of percent encoded bytes that represent UTF-8
* encoded characters in iunreserved
*
- * @param array $match PCRE match
+ * @param array{string} $match PCRE match, a capture group #0 consisting of a sequence of valid percent-encoded bytes
* @return string Replacement
*/
- protected function remove_iunreserved_percent_encoded($match)
+ protected function remove_iunreserved_percent_encoded(array $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).
@@ -632,7 +600,8 @@ class IRI
}
} else {
for ($j = $start; $j <= $i; $j++) {
- $string .= chr(hexdec($bytes[$j]));
+ // Cast for PHPStan, this will always be a number between 0 and 0xFF so hexdec will return int.
+ $string .= chr((int) hexdec($bytes[$j]));
}
}
}
@@ -649,6 +618,9 @@ class IRI
return $string;
}
+ /**
+ * @return void
+ */
protected function scheme_normalization()
{
if (isset($this->normalization[$this->scheme]['iuserinfo']) && $this->iuserinfo === $this->normalization[$this->scheme]['iuserinfo']) {
@@ -709,15 +681,15 @@ class IRI
* Set the entire IRI. Returns true on success, false on failure (if there
* are any invalid characters).
*
- * @param string $iri
+ * @param string|null $iri
* @return bool
*/
- public function set_iri($iri, $clear_cache = false)
+ public function set_iri(?string $iri, bool $clear_cache = false)
{
static $cache;
if ($clear_cache) {
$cache = null;
- return;
+ return false;
}
if (!$cache) {
$cache = [];
@@ -769,10 +741,10 @@ class IRI
* Set the scheme. Returns true on success, false on failure (if there are
* any invalid characters).
*
- * @param string $scheme
+ * @param string|null $scheme
* @return bool
*/
- public function set_scheme($scheme)
+ public function set_scheme(?string $scheme)
{
if ($scheme === null) {
$this->scheme = null;
@@ -789,15 +761,15 @@ class IRI
* Set the authority. Returns true on success, false on failure (if there are
* any invalid characters).
*
- * @param string $authority
+ * @param string|null $authority
* @return bool
*/
- public function set_authority($authority, $clear_cache = false)
+ public function set_authority(?string $authority, bool $clear_cache = false)
{
static $cache;
if ($clear_cache) {
$cache = null;
- return;
+ return false;
}
if (!$cache) {
$cache = [];
@@ -821,13 +793,16 @@ class IRI
$remaining = $authority;
if (($iuserinfo_end = strrpos($remaining, '@')) !== false) {
- $iuserinfo = substr($remaining, 0, $iuserinfo_end);
+ // Cast for PHPStan on PHP < 8.0. It does not detect that
+ // the range is not flipped so substr cannot return false.
+ $iuserinfo = (string) substr($remaining, 0, $iuserinfo_end);
$remaining = substr($remaining, $iuserinfo_end + 1);
} else {
$iuserinfo = null;
}
if (($port_start = strpos($remaining, ':', intval(strpos($remaining, ']')))) !== false) {
- if (($port = substr($remaining, $port_start + 1)) === false) {
+ $port = substr($remaining, $port_start + 1);
+ if ($port === false) {
$port = null;
}
$remaining = substr($remaining, 0, $port_start);
@@ -852,10 +827,10 @@ class IRI
/**
* Set the iuserinfo.
*
- * @param string $iuserinfo
+ * @param string|null $iuserinfo
* @return bool
*/
- public function set_userinfo($iuserinfo)
+ public function set_userinfo(?string $iuserinfo)
{
if ($iuserinfo === null) {
$this->iuserinfo = null;
@@ -871,10 +846,10 @@ class IRI
* Set the ihost. Returns true on success, false on failure (if there are
* any invalid characters).
*
- * @param string $ihost
+ * @param string|null $ihost
* @return bool
*/
- public function set_host($ihost)
+ public function set_host(?string $ihost)
{
if ($ihost === null) {
$this->ihost = null;
@@ -915,7 +890,7 @@ class IRI
* Set the port. Returns true on success, false on failure (if there are
* any invalid characters).
*
- * @param string $port
+ * @param string|int|null $port
* @return bool
*/
public function set_port($port)
@@ -923,7 +898,7 @@ class IRI
if ($port === null) {
$this->port = null;
return true;
- } elseif (strspn($port, '0123456789') === strlen($port)) {
+ } elseif (strspn((string) $port, '0123456789') === strlen((string) $port)) {
$this->port = (int) $port;
$this->scheme_normalization();
return true;
@@ -936,15 +911,15 @@ class IRI
/**
* Set the ipath.
*
- * @param string $ipath
+ * @param string|null $ipath
* @return bool
*/
- public function set_path($ipath, $clear_cache = false)
+ public function set_path(?string $ipath, bool $clear_cache = false)
{
static $cache;
if ($clear_cache) {
$cache = null;
- return;
+ return false;
}
if (!$cache) {
$cache = [];
@@ -969,10 +944,10 @@ class IRI
/**
* Set the iquery.
*
- * @param string $iquery
+ * @param string|null $iquery
* @return bool
*/
- public function set_query($iquery)
+ public function set_query(?string $iquery)
{
if ($iquery === null) {
$this->iquery = null;
@@ -986,10 +961,10 @@ class IRI
/**
* Set the ifragment.
*
- * @param string $ifragment
+ * @param string|null $ifragment
* @return bool
*/
- public function set_fragment($ifragment)
+ public function set_fragment(?string $ifragment)
{
if ($ifragment === null) {
$this->ifragment = null;
@@ -1003,9 +978,10 @@ class IRI
/**
* Convert an IRI to a URI (or parts thereof)
*
+ * @param string $string
* @return string
*/
- public function to_uri($string)
+ public function to_uri(string $string)
{
static $non_ascii;
if (!$non_ascii) {
@@ -1026,7 +1002,7 @@ class IRI
/**
* Get the complete IRI
*
- * @return string
+ * @return string|false
*/
public function get_iri()
{
@@ -1063,13 +1039,13 @@ class IRI
*/
public function get_uri()
{
- return $this->to_uri($this->get_iri());
+ return $this->to_uri((string) $this->get_iri());
}
/**
* Get the complete iauthority
*
- * @return string
+ * @return ?string
*/
protected function get_iauthority()
{
@@ -1093,7 +1069,7 @@ class IRI
/**
* Get the complete authority
*
- * @return string
+ * @return ?string
*/
protected function get_authority()
{