ThreadTransformer::includeMessages()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace ReliQArts\Mardin\Transformers;
4
5
use Carbon\Carbon;
6
use ReliQArts\Mardin\Contracts\Thread;
7
use League\Fractal\TransformerAbstract;
8
use ReliQArts\Mardin\Helpers\StringHelper;
9
use ReliQArts\Mardin\Contracts\UserTransformer;
10
11
class ThreadTransformer extends TransformerAbstract
12
{
13
    /**
14
     * List of resources available to include.
15
     *
16
     * @var array
17
     */
18
    protected $availableIncludes = [
19
        'messages',
20
        'unreadMessages',
21
    ];
22
23
    /**
24
     * List of resources to automatically include.
25
     *
26
     * @var array
27
     */
28
    protected $defaultIncludes = [
29
        'latestMessage',
30
    ];
31
32
    /**
33
     * Transform the data.
34
     * @return array API suitable information.
35
     */
36
    public function transform(Thread $thread)
37
    {
38
        $user = auth()->user();
39
        $userId = $user ? $user->id : 0;
40
        $unread = $thread->isUnread($userId);
41
        $unreadCount = $thread->userUnreadMessagesCount($userId);
42
        $participants = $thread->participantsString($user);
43
44
        return [
45
            'id' => (int) $thread->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface ReliQArts\Mardin\Contracts\Thread suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
46
            'subject' => $thread->subject,
0 ignored issues
show
Bug introduced by
Accessing subject on the interface ReliQArts\Mardin\Contracts\Thread suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
47
            'url' => route('show-message', ['id' => $thread->id]),
0 ignored issues
show
Bug introduced by
Accessing id on the interface ReliQArts\Mardin\Contracts\Thread suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
48
            'new' => $unreadCount,
49
            'unread' => $unread,
50
            'unread_count' => $unreadCount,
51
            'participants' => $participants,
52
            'participant_count' => $thread->participants()->count(),
53
            'deleted' => $thread->deleted_at,
0 ignored issues
show
Bug introduced by
Accessing deleted_at on the interface ReliQArts\Mardin\Contracts\Thread suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
54
            'created_at' => StringHelper::date(Carbon::createFromFormat('Y-n-j G:i:s', $thread->created_at)),
0 ignored issues
show
Bug introduced by
Accessing created_at on the interface ReliQArts\Mardin\Contracts\Thread suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
55
            'created_at_raw' => $thread->created_at,
0 ignored issues
show
Bug introduced by
Accessing created_at on the interface ReliQArts\Mardin\Contracts\Thread suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
56
            'updated_at' => $thread->updated_at ? StringHelper::date(Carbon::createFromFormat('Y-n-j G:i:s', $thread->updated_at)) : 'N/A',
0 ignored issues
show
Bug introduced by
Accessing updated_at on the interface ReliQArts\Mardin\Contracts\Thread suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 139 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
57
            'updated_at_raw' => $thread->updated_at,
0 ignored issues
show
Bug introduced by
Accessing updated_at on the interface ReliQArts\Mardin\Contracts\Thread suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
58
        ];
59
    }
60
61
    /**
62
     * Include Messages.
63
     *
64
     * @param Thread $thread
65
     * @return \League\Fractal\Resource\Collection
0 ignored issues
show
Documentation introduced by
Should the return type not be \League\Fractal\Resource\Collection|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
66
     */
67
    public function includeMessages(Thread $thread)
68
    {
69
        if ($messages = $thread->messages) {
0 ignored issues
show
Bug introduced by
Accessing messages on the interface ReliQArts\Mardin\Contracts\Thread suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
70
            return $this->collection($messages, new MessageTransformer);
0 ignored issues
show
Documentation introduced by
new \ReliQArts\Mardin\Tr...rs\MessageTransformer() is of type object<ReliQArts\Mardin\...ers\MessageTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
        }
72
    }
73
74
    /**
75
     * Include unread Messages.
76
     *
77
     * @param Thread $thread
78
     * @return \League\Fractal\Resource\Collection
0 ignored issues
show
Documentation introduced by
Should the return type not be \League\Fractal\Resource\Collection|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
79
     */
80
    public function includeUnreadMessages(Thread $thread)
81
    {
82
        if ($messages = $thread->userUnreadMessages(auth()->user()->id)) {
83
            return $this->collection($messages, new MessageTransformer);
0 ignored issues
show
Documentation introduced by
new \ReliQArts\Mardin\Tr...rs\MessageTransformer() is of type object<ReliQArts\Mardin\...ers\MessageTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
        }
85
    }
86
87
    /**
88
     * Include last Message.
89
     *
90
     * @param Thread $thread
91
     * @return \League\Fractal\Resource\Item
0 ignored issues
show
Documentation introduced by
Should the return type not be \League\Fractal\Resource\Item|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
92
     */
93
    public function includeLatestMessage(Thread $thread)
94
    {
95
        if ($message = $thread->latestMessage) {
0 ignored issues
show
Bug introduced by
Accessing latestMessage on the interface ReliQArts\Mardin\Contracts\Thread suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
96
            return $this->item($message, new MessageTransformer);
0 ignored issues
show
Documentation introduced by
new \ReliQArts\Mardin\Tr...rs\MessageTransformer() is of type object<ReliQArts\Mardin\...ers\MessageTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
        }
98
    }
99
100
    /**
101
     * Include Participants.
102
     *
103
     * @param Thread $thread
104
     * @return \League\Fractal\Resource\Collection
0 ignored issues
show
Documentation introduced by
Should the return type not be \League\Fractal\Resource\Collection|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
105
     */
106
    public function includeParticipants(Thread $thread)
107
    {
108
        if ($participants = $thread->users) {
0 ignored issues
show
Bug introduced by
Accessing users on the interface ReliQArts\Mardin\Contracts\Thread suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
109
            return $this->collection($participants, new UserTransformer);
0 ignored issues
show
Documentation introduced by
new \ReliQArts\Mardin\Contracts\UserTransformer() is of type object<ReliQArts\Mardin\...tracts\UserTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
110
        }
111
    }
112
}
113