replace array_{add,remove} with array_diff

This commit is contained in:
Shish 2020-02-01 19:30:32 +00:00
parent deb26ff7d3
commit 43ea7fb70c
2 changed files with 4 additions and 36 deletions

View File

@ -3,35 +3,6 @@
* Things which should be in the core API * * Things which should be in the core API *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* Remove an item from an array
*/
function array_remove(array $array, $to_remove): array
{
$array = array_unique($array);
$a2 = [];
foreach ($array as $existing) {
if ($existing != $to_remove) {
$a2[] = $existing;
}
}
return $a2;
}
/**
* Adds an item to an array.
*
* Also removes duplicate values from the array.
*/
function array_add(array $array, $element): array
{
// Could we just use array_push() ?
// http://www.php.net/manual/en/function.array-push.php
$array[] = $element;
$array = array_unique($array);
return $array;
}
/** /**
* Return the unique elements of an array, case insensitively * Return the unique elements of an array, case insensitively
*/ */

View File

@ -227,7 +227,7 @@ class TagListTheme extends Themelet
$display_html = ''; $display_html = '';
$tag = $row['tag']; $tag = $row['tag'];
$h_tag = html_escape($tag); $h_tag = html_escape($tag);
$tag_category_css = ''; $tag_category_css = '';
$tag_category_style = ''; $tag_category_style = '';
$h_tag_split = explode(':', html_escape($tag), 2); $h_tag_split = explode(':', html_escape($tag), 2);
@ -277,8 +277,7 @@ class TagListTheme extends Themelet
if (!in_array($tag, $tags) && !in_array("-$tag", $tags)) { if (!in_array($tag, $tags) && !in_array("-$tag", $tags)) {
return ""; return "";
} else { } else {
$tags = array_remove($tags, $tag); $tags = array_diff($tags, [$tag, "-$tag"]);
$tags = array_remove($tags, "-$tag");
return "<a href='".$this->tag_link(join(' ', $tags))."' title='Remove' rel='nofollow'>R</a>"; return "<a href='".$this->tag_link(join(' ', $tags))."' title='Remove' rel='nofollow'>R</a>";
} }
} }
@ -288,8 +287,7 @@ class TagListTheme extends Themelet
if (in_array($tag, $tags)) { if (in_array($tag, $tags)) {
return ""; return "";
} else { } else {
$tags = array_remove($tags, "-$tag"); $tags = array_diff($tags, ["-$tag"]) + [$tag];
$tags = array_add($tags, $tag);
return "<a href='".$this->tag_link(join(' ', $tags))."' title='Add' rel='nofollow'>A</a>"; return "<a href='".$this->tag_link(join(' ', $tags))."' title='Add' rel='nofollow'>A</a>";
} }
} }
@ -299,8 +297,7 @@ class TagListTheme extends Themelet
if (in_array("-$tag", $tags)) { if (in_array("-$tag", $tags)) {
return ""; return "";
} else { } else {
$tags = array_remove($tags, $tag); $tags = array_diff($tags, [$tag]) + ["-$tag"];
$tags = array_add($tags, "-$tag");
return "<a href='".$this->tag_link(join(' ', $tags))."' title='Subtract' rel='nofollow'>S</a>"; return "<a href='".$this->tag_link(join(' ', $tags))."' title='Subtract' rel='nofollow'>S</a>";
} }
} }