MessageRelations::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * This file is part of GitterBot package.
5
 *
6
 * @author Serafim <[email protected]>
7
 * @date 28.03.2016 23:55
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
namespace Domains\Analyser;
13
14
use Core\Lazy\Fetch;
15
use Domains\Message\Message;
16
use Domains\Message\Relation;
17
use Domains\User\User;
18
use Illuminate\Database\Eloquent\Collection;
19
20
/**
21
 * Class MessageRelations
22
 * @package Domains\Analyser
23
 */
24
class MessageRelations implements Analyser
25
{
26
    /**
27
     * @var Collection
28
     */
29
    private $buffer;
30
31
    /**
32
     * MessageRelations constructor.
33
     */
34
    public function __construct()
35
    {
36
        $this->buffer = new Collection();
37
    }
38
39
    /**
40
     * @return Analyser
41
     */
42
    public function clear() : Analyser
43
    {
44
        Relation::query()->delete();
45
        return $this;
46
    }
47
48
    /**
49
     * @param \Closure|null $progress
50
     * @return Analyser
51
     */
52
    public function analyse(\Closure $progress = null) : Analyser
53
    {
54
        $stackCount = 100;
55
        $response = new Fetch(
56
            Message::inHistoricalOrder()
0 ignored issues
show
Bug introduced by
The method with does only exist in Domains\Message\Message ...tabase\Eloquent\Builder, but not in Illuminate\Database\Query\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57
                ->with('mentions', 'user')
58
        );
59
60
        /** @var Message $message */
61
        foreach ($response as $i => $message) {
62
            $this->buffer->push($message);
63
            if ($this->buffer->count() > $stackCount) {
64
                $this->buffer->shift();
65
            }
66
67
            $relations = $this->getRelations($message);
68
69
            if (count($relations)) {
70
                $message->questions()->saveMany($relations);
71
            }
72
            
73
            if ($progress !== null) {
74
                $progress($message, $relations, $i);
75
            }
76
        }
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param Message $message
83
     * @return array
84
     */
85
    private function getRelations(Message $message) : array
86
    {
87
        $relations = [];
88
89
        /** @var User $mention */
90
        foreach ($message->mentions as $mention) {
91
            $question = $this->buffer->last(function ($i, Message $question) use ($mention) {
92
                if ($question->user) {
93
                    return $question->user->getId() === $mention->getId();
94
                }
95
96
                return false;
97
            });
98
99
            if ($question) {
100
                $relations[] = $question;
101
            }
102
        }
103
104
        return $relations;
105
    }
106
107
}
108