Passed
Push — master ( 81f334...c86e8c )
by Mihail
03:35
created

ActionRead   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
C read() 0 32 8
1
<?php
2
3
namespace Apps\Controller\Admin\Feedback;
4
5
use Apps\ActiveRecord\FeedbackPost;
6
use Apps\Model\Admin\Feedback\FormAnswerAdd;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\NotFoundException;
10
use Ffcms\Core\Helper\Type\Any;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
14
/**
15
 * Trait ActionRead
16
 * @package Apps\Controller\Admin\Feedback
17
 * @property Request $request
18
 * @property Response $response
19
 * @property View $view
20
 */
21
trait ActionRead
22
{
23
    /**
24
     * Read feedback post and answer and add answer to thread post
25
     * @param string $id
26
     * @return string
27
     * @throws NotFoundException
28
     * @throws \Ffcms\Core\Exception\SyntaxException
29
     */
30
    public function read(string $id): ?string
31
    {
32
        if (!Any::isInt($id) || $id < 1) {
33
            throw new NotFoundException('Bad id format');
34
        }
35
        // find feedback post by id
36
        $record = FeedbackPost::with(['user', 'user.profile'])
0 ignored issues
show
Bug introduced by
The method find 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...
37
            ->find($id);
38
        if ($record === null || $record === false) {
39
            throw new NotFoundException(__('The feedback message is not founded'));
40
        }
41
42
        // initialize model with answer add if thread is not closed
43
        $model = null;
44
        if ((int)$record->closed !== 1) {
45
            $model = new FormAnswerAdd($record, App::$User->identity()->getId());
46
            if ($model->send()) {
47
                if ($model->validate()) {
48
                    $model->make();
49
                    App::$Session->getFlashBag()->add('success', __('New answer is successful add'));
50
                } else {
51
                    App::$Session->getFlashBag()->add('error', 'Validation failure');
52
                }
53
            }
54
        }
55
56
        // render view
57
        return $this->view->render('read', [
58
            'record' => $record,
59
            'model' => $model
60
        ]);
61
    }
62
}
63