Completed
Push — master ( e41cc1...feed29 )
by Andrey
08:18
created

ArticleController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 21
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace app\controllers;
4
5
use yii\web\NotFoundHttpException;
6
use yii\filters\{AccessControl, VerbFilter};
7
use yii\helpers\ArrayHelper;
8
use Itstructure\MFUploader\models\OwnerMediafile;
9
use app\models\Article;
10
11
/**
12
 * Class ArticleController
13
 *
14
 * @package app\controllers
15
 */
16 View Code Duplication
class ArticleController extends BaseController
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /**
19
     * @return array
20
     */
21
    public function behaviors()
22
    {
23
        return ArrayHelper::merge(parent::behaviors(), [
24
            'access' => [
25
                'class' => AccessControl::class,
26
                'rules' => [
27
                    [
28
                        'allow' => true,
29
                        'actions' => ['view'],
30
                        'roles' => ['?', '@'],
31
                    ],
32
                ],
33
            ],
34
            'verbs' => [
35
                'class' => VerbFilter::class,
36
                'actions' => [
37
                    'view' => ['get'],
38
                ],
39
            ],
40
        ]);
41
    }
42
43
    /**
44
     * Displays article page.
45
     *
46
     * @return string
47
     *
48
     * @throws NotFoundHttpException
49
     */
50
    public function actionView($alias)
51
    {
52
        $model = Article::find()->where([
53
            'alias' => $alias
54
        ])->andWhere([
55
            'active' => 1
56
        ])->one();
57
58
        if (null === $model) {
59
            throw new NotFoundHttpException('Article not fount with alias = '.$alias.'.');
60
        }
61
62
        $this->setMetaParams($model);
0 ignored issues
show
Bug introduced by
It seems like $model defined by \app\models\Article::fin...('active' => 1))->one() on line 52 can also be of type array; however, app\controllers\BaseController::setMetaParams() does only seem to accept null|object<yii\db\ActiveRecord>, 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...
63
64
        $images = OwnerMediafile::getImageFiles(Article::tableName(), $model->id);
65
66
        return $this->render('view', [
67
            'model' => $model,
68
            'images' => $images
69
        ]);
70
    }
71
}
72