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

ActionAdd::add()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 4
nop 0
dl 0
loc 35
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Api\Comments;
4
5
use Apps\Model\Api\Comments\CommentAnswerAdd;
6
use Apps\Model\Api\Comments\CommentPostAdd;
7
use Apps\Model\Api\Comments\EntityCommentData;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Exception\NativeException;
10
use Ffcms\Core\Network\Request;
11
use Ffcms\Core\Network\Response;
12
use Apps\ActiveRecord\App as AppRecord;
13
14
/**
15
 * Trait ActionAdd
16
 * @package Apps\Controller\Api\Comments
17
 * @property Request $request
18
 * @property Response $response
19
 * @method void setJsonHeader()
20
 */
21
trait ActionAdd
22
{
23
    /**
24
     * Add comment or answer via ajax.
25
     * @return string
26
     * @throws NativeException
27
     * @throws \Ffcms\Core\Exception\SyntaxException
28
     */
29
    public function add(): ?string
30
    {
31
        $this->setJsonHeader();
32
        $configs = AppRecord::getConfigs('widget', 'Comments');
33
34
        $replayTo = (int)$this->request->request->get('replay-to');
35
        $model = null;
0 ignored issues
show
Unused Code introduced by
$model is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36
        // check if its a answer (comment answer type)
37
        if ($replayTo > 0) {
38
            $model = new CommentAnswerAdd($configs);
0 ignored issues
show
Bug introduced by
It seems like $configs defined by \Apps\ActiveRecord\App::...s('widget', 'Comments') on line 32 can also be of type null or string; however, Apps\Model\Api\Comments\...nswerAdd::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
39
            $model->replayTo = $replayTo;
40
        } else { // sounds like new comment row
41
            $model = new CommentPostAdd($configs);
0 ignored issues
show
Bug introduced by
It seems like $configs defined by \Apps\ActiveRecord\App::...s('widget', 'Comments') on line 32 can also be of type null or string; however, Apps\Model\Api\Comments\...tPostAdd::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42
            $model->pathway = App::$Security->strip_tags($this->request->request->get('pathway'));
43
        }
44
45
        // pass general comment params to model
46
        $model->message = App::$Security->secureHtml((string)$this->request->request->get('message'));
47
        $model->guestName = App::$Security->strip_tags($this->request->request->get('guest-name'));
48
49
        // check model conditions before add new row
50
        if ($model === null || !$model->check()) {
51
            throw new NativeException('Unknown error');
52
        }
53
54
        // add comment post or answer to database and get response active record row
55
        $record = $model->buildRecord();
56
        // pass row to entity builder model
57
        $response = new EntityCommentData($record);
58
59
        return json_encode([
60
            'status' => 1,
61
            'data' => $response->make() // build row to standard format
62
        ]);
63
    }
64
}
65