GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ProcessController::actionProcess()   F
last analyzed

Complexity

Conditions 22
Paths 445

Size

Total Lines 96

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 0.7708
c 0
b 0
f 0
cc 22
nc 445
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace app\modules\review\controllers;
4
5
use app\actions\SubmitFormAction;
6
use app\behaviors\spamchecker\SpamCheckerBehavior;
7
use app\components\Controller;
8
use app\models\SpamChecker;
9
use app\models\Submission;
10
use app\modules\review\models\RatingItem;
11
use app\modules\review\models\RatingValues;
12
use app\modules\review\models\Review;
13
use Yii;
14
use yii\helpers\Json;
15
use yii\web\HttpException;
16
17
class ProcessController extends Controller
18
{
19
    public function actions()
20
    {
21
        return [
22
            'submission' => [
23
              'class' => SubmitFormAction::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
24
            ],
25
        ];
26
    }
27
28
    /**
29
     * @param $id
30
     * @param $objectModelId
31
     * @param $objectId
32
     * @param string $returnUrl
33
     * @return \yii\web\Response
34
     * @throws HttpException
35
     */
36
    public function actionProcess($id, $objectModelId, $objectId, $returnUrl = '/')
37
    {
38
        if (false === Yii::$app->request->isPost) {
39
            throw new HttpException(403);
40
        }
41
        /** @var $review \app\modules\review\models\Review|SpamCheckerBehavior */
42
        $post = Yii::$app->request->post();
43
        $review = new Review(['scenario' => 'check']);
44
        $review->load($post);
45
        if (!Yii::$app->user->isGuest) {
46
            $review->author_email = Yii::$app->user->identity->email;
47
        }
48
        $review->object_id = $objectId;
49
        $review->object_model_id = $objectModelId;
50
        if ($review->validate()) {
51
            $submission_id = Yii::$app->runAction('review/process/submission', ['id' => $id]);
52
            if ($submission_id == "0") {
53
                Yii::$app->session->setFlash(
54
                    'info',
55
                    Yii::t('app', 'Error occurred while saving review. Sorry. Try again later')
56
                );
57
                return $this->redirect($returnUrl);
58
            }
59
            $review->submission_id = $submission_id;
60
            $review->status = Review::STATUS_NEW;
61
            if ($this->module->enableSpamChecking) {
62
                $activeSpamChecker = SpamChecker::getActive();
63
                if (!is_null($activeSpamChecker) && !empty($activeSpamChecker->api_key)) {
64
                    $review->attachBehavior(
65
                        'spamChecker',
66
                        [
67
                            'class' => SpamCheckerBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
68
                            'data' => [
69
                                $activeSpamChecker->name => [
70
                                    'class' => $activeSpamChecker->behavior,
71
                                    'value' => [
72
                                        'key' => $activeSpamChecker->api_key,
73
                                        SpamChecker::FIELD_TYPE_CONTENT => $review->review_text
74
                                    ],
75
                                ],
76
                            ],
77
                        ]
78
                    );
79
                    if ($review->isSpam()) {
0 ignored issues
show
Documentation Bug introduced by
The method isSpam does not exist on object<app\modules\review\models\Review>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
                        $review->status = Review::STATUS_NOT_APPROVED;
81
                        Submission::updateAll(['spam' => 1], ['id' => $submission_id]);
82
                    }
83
                }
84
            }
85
            if ($review->save()) {
86
                $ratingData = isset($post['ObjectRating']) ? $post['ObjectRating'] : null;
87
                if (null !== $ratingData) {
88
                    $group = isset($ratingData['group']) ? trim($ratingData['group']) : null;
89
                    $group = RatingItem::getGroupByName($group);
0 ignored issues
show
Bug introduced by
It seems like $group defined by \app\modules\review\mode...:getGroupByName($group) on line 89 can also be of type string; however, app\modules\review\model...gItem::getGroupByName() does only seem to accept null, 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...
90
                    $items = [];
91
                    if (!empty($ratingData['values']) && !empty($group)) {
92
                        $user_id = \Yii::$app->getUser()->isGuest ? 0 : \Yii::$app->user->identity->getId();
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

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...
93
                        $ratingId = md5(Json::encode(array_merge($ratingData['values'], [microtime(), $user_id])));
94
                        $date = date('Y-m-d H:m:s');
95
                        if ((0 == $group['require_review']) || ((0 != $group['require_review']))) {
96
                            $items = RatingItem::getItemsByAttributes(
97
                                ['rating_group' => $group['rating_group']],
98
                                true,
99
                                true
100
                            );
101
                        }
102
                        if (!empty($items)) {
103
                            foreach ($items as $key => $item) {
104
                                $model = new RatingValues();
105
                                $model->loadDefaultValues();
106
                                $model->object_id = $objectId;
107
                                $model->object_model_id = $objectModelId;
108
                                $model->rating_item_id = $item['id'];
109
                                $model->value = isset($ratingData['values'][$item['id']])
110
                                    ? intval($ratingData['values'][$item['id']])
111
                                    : 0;
112
                                $model->rating_id = $ratingId;
113
                                $model->date = $date;
114
                                $model->save();
115
                            }
116
                            if (isset($review)) {
117
                                $review->rating_id = $ratingId;
118
                                $review->save(true, ['rating_id']);
119
                            }
120
                        }
121
                    }
122
                }
123
                Yii::$app->session->setFlash(
124
                    'info',
125
                    Yii::t('app', 'Your review will appear on the website immediately after moderation')
126
                );
127
                return $this->redirect($returnUrl);
128
            }
129
        }
130
        return $this->redirect($returnUrl);
131
    }
132
}
133