HitCounterController::actionIndex()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 3
nop 0
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 4
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/*
3
 * @copyright Copyright (C) 2019 Sergio coderius <coderius>
4
 * @license This program is free software: the MIT License (MIT)
5
 */
6
7
namespace coderius\hitCounter\controllers;
8
9
use yii\web\Controller;
10
use Yii;
11
use yii\web\Response;
12
use yii\web\ServerErrorHttpException;
13
use coderius\hitCounter\services\HitCounterService;
14
use coderius\hitCounter\models\HitCounterModel;
15
16
/**
17
 * Default controller.
18
 */
19
class HitCounterController extends Controller
20
{
21
    private $service;
22
23 12
    public function __construct($id, $module, HitCounterService $service, $config = [])
24
    {
25 12
        $this->service = $service;
26 12
        parent::__construct($id, $module, $config);
27 12
    }
28
    
29 12
    public function actionIndex()
30
    {
31 12
        $request = Yii::$app->request;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->request can also be of type yii\web\Request. However, the property $request is declared as type yii\console\Request. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
32 12
        $modelHitCounter = $this->service->loadModel($request);
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type yii\console\Request; however, parameter $request of coderius\hitCounter\serv...terService::loadModel() does only seem to accept yii\web\Request, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        $modelHitCounter = $this->service->loadModel(/** @scrutinizer ignore-type */ $request);
Loading history...
33
34 9
        if ($modelHitCounter && $modelHitCounter->validate()) {
35
            try {
36 6
                $hit = $this->service->create($modelHitCounter);
0 ignored issues
show
Unused Code introduced by
The assignment to $hit is dead and can be removed.
Loading history...
37 3
                \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
38 3
                return ['status' => 'success'];
39 3
            } catch (\DomainException $e) {
40 3
                Yii::$app->errorHandler->logException($e);
41 3
                \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
42
                return [
43 3
                    'status' => 'error',
44
                    // 'errors' => $e->getMessage(),
45
                ];
46
            }
47
        }
48
49 3
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
50 3
        return $modelHitCounter->errors;
51
        // return $modelHitCounter->getAttributes();
52
    }
53
54
}