Skip to content

Commit

Permalink
rex_string/rex_version: more types (#4608)
Browse files Browse the repository at this point in the history
  • Loading branch information
gharlan committed Apr 5, 2021
1 parent 5dd934d commit 038ae44
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 41 deletions.
48 changes: 14 additions & 34 deletions .tools/psalm/baseline.xml
Expand Up @@ -1786,10 +1786,8 @@
</MixedReturnStatement>
</file>
<file src="redaxo/src/addons/metainfo/lib/handler/handler.php">
<MixedArgument occurrences="31">
<MixedArgument occurrences="29">
<code>$attr</code>
<code>$attrArray['perm']</code>
<code>$attrArray['perm']</code>
<code>$defaultValue</code>
<code>$defaultValue</code>
<code>$field-&gt;getValue('callback')</code>
Expand Down Expand Up @@ -1819,16 +1817,10 @@
<code>$title</code>
<code>$typeLabel</code>
</MixedArgument>
<MixedArrayOffset occurrences="1">
<code>$attrArray[$value]</code>
</MixedArrayOffset>
<MixedAssignment occurrences="27">
<MixedAssignment occurrences="23">
<code>$activeItem</code>
<code>$activeItem</code>
<code>$attr</code>
<code>$attrValue</code>
<code>$category</code>
<code>$category</code>
<code>$category</code>
<code>$category</code>
<code>$dblength</code>
Expand All @@ -1849,7 +1841,6 @@
<code>$saveValue</code>
<code>$title</code>
<code>$typeLabel</code>
<code>$value</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="1">
<code>string|int|null</code>
Expand Down Expand Up @@ -4389,6 +4380,10 @@
<code>$navi['linkClasses']</code>
<code>$this-&gt;right</code>
</MixedArgument>
<MixedArgumentTypeCoercion occurrences="2">
<code>$attributes</code>
<code>$attributes</code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess occurrences="4">
<code>$navi['itemAttr']['class']</code>
<code>$navi['itemClasses'][0]</code>
Expand Down Expand Up @@ -6655,39 +6650,28 @@
</MixedAssignment>
</file>
<file src="redaxo/src/core/lib/util/string.php">
<MixedArgument occurrences="8">
<code>$comparator</code>
<code>$match[2]</code>
<MixedArgument occurrences="2">
<code>$query</code>
<code>$value</code>
<code>$value</code>
<code>$version</code>
<code>$version1</code>
<code>$version2</code>
</MixedArgument>
<MixedArgumentTypeCoercion occurrences="3">
<MixedArgumentTypeCoercion occurrences="2">
<code>$key</code>
<code>$key</code>
<code>[$match[1], '\\']</code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess occurrences="5">
<code>$match[1]</code>
<code>$match[1]</code>
<code>$match[2]</code>
<MixedArrayAccess occurrences="2">
<code>$quoted[$i++]</code>
<code>$quoted[$i++]</code>
</MixedArrayAccess>
<MixedArrayAssignment occurrences="2">
<code>$query[]</code>
<code>$quoted[]</code>
</MixedArrayAssignment>
<MixedAssignment occurrences="6">
<MixedAssignment occurrences="5">
<code>$result[$part[0]]</code>
<code>$result[]</code>
<code>$value</code>
<code>$value</code>
<code>$value</code>
<code>$value</code>
</MixedAssignment>
<MixedFunctionCall occurrences="1">
<code>$func($value, $key)</code>
Expand All @@ -6696,18 +6680,14 @@
<code>array</code>
<code>string</code>
</MixedInferredReturnType>
<MixedMethodCall occurrences="1">
<code>xss_clean</code>
</MixedMethodCall>
<MixedOperand occurrences="3">
<code>$fullkey</code>
<code>$match[1]</code>
<code>rex_escape($value)</code>
</MixedOperand>
<MixedReturnStatement occurrences="2">
<code>$antiXss-&gt;xss_clean($html)</code>
<code>Symfony\Component\Yaml\Yaml::parse($value)</code>
</MixedReturnStatement>
<MixedReturnTypeCoercion occurrences="2">
<code>$result</code>
<code>string[]</code>
</MixedReturnTypeCoercion>
</file>
<file src="redaxo/src/core/lib/util/timer.php">
<MixedAssignment occurrences="2">
Expand Down
14 changes: 14 additions & 0 deletions redaxo/src/addons/structure/lib/navigation.php
Expand Up @@ -401,17 +401,24 @@ protected function _getNavigation($categoryId, $depth = 1)

/**
* @param string[] $items
* @param array<int|string, int|string|list<string>> $attributes
*/
protected function getBreadcrumbListTag(array $items, array $attributes): string
{
return '<ul'.rex_string::buildAttributes($attributes).">\n".implode('', $items)."</ul>\n";
}

/**
* @param array<int|string, int|string|list<string>> $attributes
*/
protected function getBreadcrumbListItemTag(string $item, array $attributes, int $depth): string
{
return '<li'.rex_string::buildAttributes($attributes).'>'.$item."</li>\n";
}

/**
* @param array<int|string, int|string|list<string>> $attributes
*/
protected function getBreadcrumbLinkTag(rex_category $category, string $content, array $attributes, int $depth): string
{
if (!isset($attributes['href'])) {
Expand All @@ -423,17 +430,24 @@ protected function getBreadcrumbLinkTag(rex_category $category, string $content,

/**
* @param string[] $items
* @param array<int|string, int|string|list<string>> $attributes
*/
protected function getListTag(array $items, array $attributes, int $depth): string
{
return '<ul'.rex_string::buildAttributes($attributes).">\n".implode('', $items)."</ul>\n";
}

/**
* @param array<int|string, int|string|list<string>> $attributes
*/
protected function getListItemTag(rex_category $category, string $item, array $attributes, int $depth): string
{
return '<li'.rex_string::buildAttributes($attributes).'>'.$item."</li>\n";
}

/**
* @param array<int|string, int|string|list<string>> $attributes
*/
protected function getLinkTag(rex_category $category, string $content, array $attributes, int $depth): string
{
if (!isset($attributes['href'])) {
Expand Down
26 changes: 21 additions & 5 deletions redaxo/src/core/lib/util/string.php
Expand Up @@ -65,20 +65,22 @@ public static function normalize($string, $replaceChar = '_', $allowedChars = ''
*
* @param string $string
*
* @return array
* @return string[]
*/
public static function split($string)
{
$string = trim($string);
if (empty($string)) {
return [];
}

$result = [];
$spacer = '@@@REX_SPACER@@@';
$quoted = [];

$pattern = '@(?<=\s|=|^)(["\'])((?:.*[^\\\\])?(?:\\\\\\\\)*)\\1(?=\s|$)@Us';
$callback = static function ($match) use ($spacer, &$quoted) {
$callback = static function (array $match) use ($spacer, &$quoted) {
/** @var list<string> $match */
$quoted[] = str_replace(['\\' . $match[1], '\\\\'], [$match[1], '\\'], $match[2]);
return $spacer;
};
Expand All @@ -102,6 +104,9 @@ public static function split($string)

/**
* @deprecated since 5.10, use `rex_version::split` instead
*
* @param string $version
* @return list<string>
*/
#[\JetBrains\PhpStorm\Deprecated(reason: 'since 5.10, use `rex_version::split` instead', replacement: 'rex_version::split(!%parameter0%)')]
public static function versionSplit($version)
Expand All @@ -111,6 +116,12 @@ public static function versionSplit($version)

/**
* @deprecated since 5.10, use `rex_version::compare` instead
*
* @param string $version1
* @param string $version2
* @param null|'='|'=='|'!='|'<>'|'<'|'<='|'>'|'>=' $comparator
*
* @return int|bool
*/
#[\JetBrains\PhpStorm\Deprecated(reason: 'since 5.10, use `rex_version::compare` instead', replacement: 'rex_version::compare(!%parametersList%)')]
public static function versionCompare($version1, $version2, $comparator = '<')
Expand Down Expand Up @@ -159,7 +170,7 @@ public static function yamlDecode($value)
public static function buildQuery(array $params, $argSeparator = '&')
{
$query = [];
$func = static function (array $params, $fullkey = null) use (&$query, &$func) {
$func = static function (array $params, ?string $fullkey = null) use (&$query, &$func) {
foreach ($params as $key => $value) {
$key = $fullkey ? $fullkey . '[' . urlencode($key) . ']' : urlencode($key);
if (is_array($value)) {
Expand All @@ -176,6 +187,7 @@ public static function buildQuery(array $params, $argSeparator = '&')
/**
* Returns a string by key="value" pair.
*
* @param array<int|string, int|string|list<string>> $attributes
* @return string
*/
public static function buildAttributes(array $attributes)
Expand All @@ -184,13 +196,16 @@ public static function buildAttributes(array $attributes)

foreach ($attributes as $key => $value) {
if (is_int($key)) {
$attr .= ' ' . rex_escape($value);
if (is_array($value)) {
throw new InvalidArgumentException('For integer keys the value can not be an array');
}
$attr .= ' ' . (string) rex_escape($value);
} else {
if (is_array($value)) {
$value = implode(' ', $value);
}
// for bc reasons avoid double escaping of "&", especially in already escaped urls
$value = str_replace('&amp;', '&', $value);
$value = str_replace('&amp;', '&', (string) $value);
$attr .= ' ' . rex_escape($key) . '="' . rex_escape($value) . '"';
}
}
Expand All @@ -216,6 +231,7 @@ public static function highlight($string)
*/
public static function sanitizeHtml(string $html): string
{
/** @var voku\helper\AntiXSS|null $antiXss */
static $antiXss;

if (!$antiXss) {
Expand Down
5 changes: 3 additions & 2 deletions redaxo/src/core/lib/util/version.php
Expand Up @@ -20,6 +20,8 @@ public static function isUnstable(string $version): bool

/**
* Splits a version string into its parts.
*
* @return list<string>
*/
public static function split(string $version): array
{
Expand All @@ -36,8 +38,7 @@ public static function split(string $version): array
*
* @param string $version1 First version number
* @param string $version2 Second version number
* @param string $comparator Optional comparator
* @psalm-param null|'='|'=='|'!='|'<>'|'<'|'<='|'>'|'>=' $comparator
* @param null|'='|'=='|'!='|'<>'|'<'|'<='|'>'|'>=' $comparator Optional comparator
*
* @return bool
*/
Expand Down

0 comments on commit 038ae44

Please sign in to comment.