Passed
Push — master ( d3fd5d...81f334 )
by Mihail
03:51
created

ActionShowWallAnswers   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 8

1 Method

Rating   Name   Duplication   Size   Complexity  
C showWallAnswers() 0 46 7
1
<?php
2
3
namespace Apps\Controller\Api\Profile;
4
5
use Apps\ActiveRecord\WallAnswer;
6
use Apps\ActiveRecord\WallPost;
7
use Ffcms\Core\Exception\NativeException;
8
use Ffcms\Core\Helper\Date;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Network\Request;
11
use Ffcms\Core\Network\Response;
12
13
/**
14
 * Trait ActionShowWallAnswers
15
 * @package Apps\Controller\Api\Profile
16
 * @property Request $request
17
 * @property Response $response
18
 * @method void setJsonHeader()
19
 */
20
trait ActionShowWallAnswers
21
{
22
    /**
23
     * Show all answers for this post id
24
     * @param string $postId
25
     * @return string
26
     * @throws NativeException
27
     * @return string
28
     */
29
    public function showWallAnswers(string $postId): ?string
30
    {
31
        $this->setJsonHeader();
32
33
        // check input post id num
34
        if (!Any::isInt($postId) || $postId < 1) {
35
            throw new NativeException('Wrong input data');
36
        }
37
38
        // try to find this post
39
        $object = WallPost::find($postId);
40
41
        // check if post is exist
42
        if (!$object) {
43
            throw new NativeException('Wrong input data');
44
        }
45
46
        // get answer object with relation to user, profile and role table
47
        $answers = WallAnswer::with(['user', 'user.profile', 'user.role'])
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
48
            ->where('post_id', $postId)
49
            ->orderBy('id', 'DESC')
50
            ->take(200)
51
            ->get();
52
53
        $response = [];
54
        /** @var WallAnswer[] $answers */
55
        foreach ($answers as $answer) {
56
            // check if user exist
57
            if ($answer->user === null || $answer->user->id < 1) {
58
                continue;
59
            }
60
            // generate response array
61
            $response[] = [
62
                'answer_id' => $answer->id,
63
                'user_id' => $answer->user_id,
64
                'user_nick' => $answer->user->profile->getNickname(),
65
                'user_avatar' => $answer->user->profile->getAvatarUrl('small'),
66
                'user_color' => $answer->user->role->color,
67
                'answer_message' => $answer->message,
68
                'answer_date' => Date::humanize($answer->created_at)
69
            ];
70
        }
71
72
        // encode and show json result
73
        return json_encode(['status' => 1, 'data' => $response]);
74
    }
75
}
76