[TOOLS] Fix errors reported by updated PHPStan

This commit is contained in:
Hugo Sales 2021-12-26 19:08:56 +00:00 committed by Diogo Peralta Cordeiro
parent 52e2231661
commit 7eff22d548
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
4 changed files with 20 additions and 17 deletions

View File

@ -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]];
}

View File

@ -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));
}

View File

@ -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(),

View File

@ -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:')) {