From 7eff22d5485a524487c4b08fab38e93bf076e478 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 26 Dec 2021 19:08:56 +0000 Subject: [PATCH] [TOOLS] Fix errors reported by updated PHPStan --- components/Posting/Posting.php | 2 +- components/Search/Util/Parser.php | 4 ++-- plugins/Favourite/Favourite.php | 27 +++++++++++++++------------ src/Util/HTML.php | 4 ++-- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index eac313fed7..cb01d77cec 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -96,7 +96,7 @@ class Posting extends Component Event::handle('PostingGetContextActor', [$request, $actor, &$context_actor]); $form_params = []; - if (!empty($in_targets)) { + if (!empty($in_targets)) { // @phpstan-ignore-line $form_params[] = ['in', ChoiceType::class, ['label' => _m('In:'), 'multiple' => false, 'expanded' => false, 'choices' => $in_targets]]; } diff --git a/components/Search/Util/Parser.php b/components/Search/Util/Parser.php index 48a8cbb540..8b8cfe8364 100644 --- a/components/Search/Util/Parser.php +++ b/components/Search/Util/Parser.php @@ -113,11 +113,11 @@ abstract class Parser $note_criteria = null; $actor_criteria = null; - if (!empty($note_parts)) { + if (!empty($note_parts)) { // @phpstan-ignore-line self::connectParts($note_parts, $note_criteria_arr, $last_op, $eb, force: true); $note_criteria = new Criteria($eb->orX(...$note_criteria_arr)); } - if (!empty($actor_parts)) { + if (!empty($actor_parts)) { // @phpstan-ignore-line self::connectParts($actor_parts, $actor_criteria_arr, $last_op, $eb, force: true); $actor_criteria = new Criteria($eb->orX(...$actor_criteria_arr)); } diff --git a/plugins/Favourite/Favourite.php b/plugins/Favourite/Favourite.php index 234e82f2d2..ab1edb35b6 100644 --- a/plugins/Favourite/Favourite.php +++ b/plugins/Favourite/Favourite.php @@ -46,40 +46,42 @@ class Favourite extends NoteHandlerPlugin { $opts = ['note_id' => $note_id, 'actor_id' => $actor_id]; $note_already_favoured = DB::find('favourite', $opts); + $activity = null; if (\is_null($note_already_favoured)) { DB::persist(FavouriteEntity::create($opts)); - $act = Activity::create([ + $activity = Activity::create([ 'actor_id' => $actor_id, 'verb' => 'favourite', 'object_type' => 'note', 'object_id' => $note_id, 'source' => $source, ]); - DB::persist($act); + DB::persist($activity); - Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $act, [], "{$actor->getNickname()} favoured note {$note_id}"]); + Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, [], "{$actor->getNickname()} favoured note {$note_id}"]); } - return $act ?? null; + return $activity; } public static function unfavourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity { $note_already_favoured = DB::find('favourite', ['note_id' => $note_id, 'actor_id' => $actor_id]); + $activity = null; if (!\is_null($note_already_favoured)) { DB::remove($note_already_favoured); $favourite_activity = DB::findBy('activity', ['verb' => 'favourite', 'object_type' => 'note', 'object_id' => $note_id], order_by: ['created' => 'DESC'])[0]; - $act = Activity::create([ + $activity = Activity::create([ 'actor_id' => $actor_id, 'verb' => 'undo', // 'undo_favourite', 'object_type' => 'activity', // 'note', 'object_id' => $favourite_activity->getId(), // $note_id, 'source' => $source, ]); - DB::persist($act); + DB::persist($activity); - Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $act, [], "{$actor->getNickname()} unfavoured note {$note_id}"]); + Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, [], "{$actor->getNickname()} unfavoured note {$note_id}"]); } - return $act ?? null; + return $activity; } /** @@ -204,15 +206,16 @@ class Favourite extends NoteHandlerPlugin } } + $activity = null; if ($type_activity->get('type') === 'Like') { - $act = self::favourNote($note_id, $actor->getId(), source: 'ActivityPub'); + $activity = self::favourNote($note_id, $actor->getId(), source: 'ActivityPub'); } else { - $act = self::unfavourNote($note_id, $actor->getId(), source: 'ActivityPub'); + $activity = self::unfavourNote($note_id, $actor->getId(), source: 'ActivityPub'); } - if (!\is_null($act)) { + if (!\is_null($activity)) { // Store ActivityPub Activity $ap_act = \Plugin\ActivityPub\Entity\ActivitypubActivity::create([ - 'activity_id' => $act->getId(), + 'activity_id' => $activity->getId(), 'activity_uri' => $type_activity->get('id'), 'created' => new DateTime($type_activity->get('published') ?? 'now'), 'modified' => new DateTime(), diff --git a/src/Util/HTML.php b/src/Util/HTML.php index 160e2f0b36..2f1f301c33 100644 --- a/src/Util/HTML.php +++ b/src/Util/HTML.php @@ -81,13 +81,13 @@ abstract class HTML */ private static function attr(array $attrs, array $options = []): string { - return ' ' . implode(' ', F\map($attrs, [self::class, '_process_attribute'])); + return ' ' . implode(' ', F\map($attrs, fn ($attr, $key) => self::process_attribute($attr, $key, $options))); } /** * Convert an attr ($key), $val pair to an HTML attribute, but validate to exclude some vectors of injection */ - public static function _process_attribute(string $val, string $key): string + private static function process_attribute(string $val, string $key, array $options): string { if (\in_array($key, array_merge($options['forbidden_attributes'] ?? [], self::FORBIDDEN_ATTRIBUTES)) || str_starts_with($val, 'javascript:')) {