Thread::participantsString()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 2
nop 2
1
<?php
2
3
namespace ReliQArts\Mardin\Models;
4
5
use ModelNotFoundException;
6
use Cmgmyr\Messenger\Models\Thread as MessengerThread;
7
use ReliQArts\Mardin\Contracts\Thread as ThreadContract;
8
9
class Thread extends MessengerThread implements ThreadContract
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function messages()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
15
    {
16
        return $this->hasMany(Message::class, 'thread_id', 'id')->orderBy('updated_at', 'desc');
17
    }
18
19
    /**
20
     * Generates a string of participant information.
21
     *
22
     * @param User  $excludingUser User to be excluded from string.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $excludingUser not be User|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
23
     * @param bool  $array Whether an array should be returned.
24
     *
25
     * @return string
26
     */
27
    public function participantsString($excludingUser = null, $array = false)
28
    {
29
        $participants = $this->users->map(function ($participant) use ($excludingUser) {
0 ignored issues
show
Documentation introduced by
The property users does not exist on object<ReliQArts\Mardin\Models\Thread>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
30
            if (! $excludingUser || $excludingUser->name != $participant->name) {
31
                return $participant->name;
32
            }
33
        })->reject(function ($name) {
34
            return empty($name);
35
        })->toArray();
36
37
        return $array ? $participants : implode(', ', $participants);
38
    }
39
40
    /**
41
     * Mark a thread as unread for a user.
42
     *
43
     * @param int $userId
44
     */
45
    public function markAsUnread($userId)
46
    {
47
        try {
48
            $participant = $this->getParticipantFromUser($userId);
49
            $participant->last_read = null;
50
            $participant->save();
51
        } catch (ModelNotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class ModelNotFoundException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
52
            // do nothing
53
        }
54
    }
55
}
56