aboutsummaryrefslogtreecommitdiff
path: root/lib/Minz/Url.php
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2024-06-09 20:32:12 +0200
committerGravatar GitHub <noreply@github.com> 2024-06-09 20:32:12 +0200
commit5b28a35003a015e29770094932157f13a3f7f5c0 (patch)
tree4cbe4100379ca0d148115ad31f5a1c0c95ff7c80 /lib/Minz/Url.php
parente98c57841b843ed881f06ce6ed1c9c89942c27b8 (diff)
Pass PHPStan level 9 (#6544)
* More PHPStan * More, passing * 4 more files * Update to PHPStan 1.11.4 Needed for fixed bug: Consider numeric-string types after string concat https://github.com/phpstan/phpstan/releases/tag/1.11.4 * Pass PHPStan level 9 Start tracking booleansInConditions * Fix mark as read * Fix doctype * ctype_digit
Diffstat (limited to 'lib/Minz/Url.php')
-rw-r--r--lib/Minz/Url.php37
1 files changed, 16 insertions, 21 deletions
diff --git a/lib/Minz/Url.php b/lib/Minz/Url.php
index 67b927f05..73edcf76d 100644
--- a/lib/Minz/Url.php
+++ b/lib/Minz/Url.php
@@ -7,7 +7,7 @@ declare(strict_types=1);
class Minz_Url {
/**
* Display a formatted URL
- * @param string|array<string,string|array<string,mixed>> $url The URL to format, defined as an array:
+ * @param string|array{c?:string,a?:string,params?:array<string,mixed>} $url The URL to format, defined as an array:
* $url['c'] = controller
* $url['a'] = action
* $url['params'] = array of additional parameters
@@ -26,7 +26,7 @@ class Minz_Url {
$url_string = '';
- if ($absolute) {
+ if ($absolute !== false) {
$url_string = Minz_Request::getBaseUrl();
if (strlen($url_string) < strlen('http://a.bc')) {
$url_string = Minz_Request::guessBaseUrl();
@@ -58,7 +58,7 @@ class Minz_Url {
/**
* Construit l'URI d'une URL
- * @param array<string,mixed> $url l'url sous forme de tableau
+ * @param array{c:string,a:string,params:array<string,mixed>} $url URL as array definition
* @param string $encodage pour indiquer comment encoder les & (& ou &amp; pour html)
* @return string uri sous la forme ?key=value&key2=value2
*/
@@ -74,17 +74,19 @@ class Minz_Url {
}
if (!empty($url['params']) && is_array($url['params']) && !empty($url['params']['#'])) {
- $anchor = '#' . ($encodage === 'html' ? htmlspecialchars($url['params']['#'], ENT_QUOTES, 'UTF-8') : $url['params']['#']);
+ if (is_string($url['params']['#'])) {
+ $anchor = '#' . ($encodage === 'html' ? htmlspecialchars($url['params']['#'], ENT_QUOTES, 'UTF-8') : $url['params']['#']);
+ }
unset($url['params']['#']);
}
- if (isset($url['c'])
+ if (isset($url['c']) && is_string($url['c'])
&& $url['c'] != Minz_Request::defaultControllerName()) {
$uri .= $separator . 'c=' . $url['c'];
$separator = $and;
}
- if (isset($url['a'])
+ if (isset($url['a']) && is_string($url['a'])
&& $url['a'] != Minz_Request::defaultActionName()) {
$uri .= $separator . 'a=' . $url['a'];
$separator = $and;
@@ -94,7 +96,7 @@ class Minz_Url {
unset($url['params']['c']);
unset($url['params']['a']);
foreach ($url['params'] as $key => $param) {
- if (!is_string($key) || (!is_string($param) && !is_int($param))) {
+ if (!is_string($key) || (!is_string($param) && !is_int($param) && !is_bool($param))) {
continue;
}
$uri .= $separator . urlencode($key) . '=' . urlencode((string)$param);
@@ -102,10 +104,6 @@ class Minz_Url {
}
}
- if (!empty($url['#']) && is_string($url['#'])) {
- $uri .= '#' . ($encodage === 'html' ? htmlspecialchars($url['#'], ENT_QUOTES, 'UTF-8') : $url['#']);
- }
-
$uri .= $anchor;
return $uri;
@@ -113,8 +111,8 @@ class Minz_Url {
/**
* Check that all array elements representing the controller URL are OK
- * @param array<string,string|array<string,mixed>> $url controller URL as array
- * @return array{'c':string,'a':string,'params':array<string,mixed>} Verified controller URL as array
+ * @param array{c?:string,a?:string,params?:array<string,mixed>} $url controller URL as array
+ * @return array{c:string,a:string,params:array<string,mixed>} Verified controller URL as array
*/
public static function checkControllerUrl(array $url): array {
return [
@@ -124,7 +122,7 @@ class Minz_Url {
];
}
- /** @param array{'c'?:string,'a'?:string,'params'?:array<string,mixed>} $url */
+ /** @param array{c?:string,a?:string,params?:array<string,mixed>} $url */
public static function serialize(?array $url = []): string {
if (empty($url)) {
return '';
@@ -136,19 +134,16 @@ class Minz_Url {
}
}
- /**
- * @phpstan-return array{'c'?:string,'a'?:string,'params'?:array<string,mixed>}
- * @return array<string,string|array<string,string>>
- */
+ /** @return array{c?:string,a?:string,params?:array<string,mixed>} */
public static function unserialize(string $url = ''): array {
$result = json_decode(base64_decode($url, true) ?: '', true, JSON_THROW_ON_ERROR) ?? [];
- /** @var array{'c'?:string,'a'?:string,'params'?:array<string,mixed>} $result */
+ /** @var array{c?:string,a?:string,params?:array<string,mixed>} $result */
return $result;
}
/**
* Returns an array representing the URL as passed in the address bar
- * @return array{'c'?:string,'a'?:string,'params'?:array<string,mixed>} URL representation
+ * @return array{c?:string,a?:string,params?:array<string,string>} URL representation
*/
public static function build(): array {
$url = [
@@ -184,5 +179,5 @@ function _url(string $controller, string $action, ...$args) {
$params[$arg] = '' . $args[$i + 1];
}
- return Minz_Url::display (array ('c' => $controller, 'a' => $action, 'params' => $params));
+ return Minz_Url::display(['c' => $controller, 'a' => $action, 'params' => $params]);
}