coderius /
yii2-hit-counter
| 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
|
|||||
| 32 | 12 | $modelHitCounter = $this->service->loadModel($request); |
|||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 33 | |||||
| 34 | 9 | if ($modelHitCounter && $modelHitCounter->validate()) { |
|||
| 35 | try { |
||||
| 36 | 6 | $hit = $this->service->create($modelHitCounter); |
|||
|
0 ignored issues
–
show
|
|||||
| 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 | } |
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.