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.
Test Setup Failed
Push — filters ( 8043be...a4f56b )
by Ivan
49:57 queued 36:44
created

ObjectImageWidget::run()   C

Complexity

Conditions 9
Paths 6

Size

Total Lines 68
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 68
rs 6.2813
cc 9
eloc 47
nc 6
nop 0

How to fix   Long Method   

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\image\widgets;
4
5
use app\modules\image\models\Image;
6
use app\traits\GetImages;
7
use devgroup\TagDependencyHelper\ActiveRecordHelper;
8
use Yii;
9
use yii\base\Widget;
10
use yii\caching\TagDependency;
11
12
/**
13
 * Class ObjectImageWidget
14
 * @package app\widgets
15
 */
16
class ObjectImageWidget extends Widget
17
{
18
    /**
19
     * @var \app\properties\HasProperties|\yii\db\ActiveRecord|GetImages|null
20
     */
21
    public $model = null;
22
    /**
23
     * @var string
24
     */
25
    public $viewFile = 'img';
26
    /**
27
     * @var null|int
28
     */
29
    public $limit = null;
30
    /**
31
     * @var int
32
     */
33
    public $offset = 0;
34
    /**
35
     * @var bool
36
     */
37
    public $thumbnailOnDemand = false;
38
    /**
39
     * @var int
40
     */
41
    public $thumbnailWidth = 400;
42
    /**
43
     * @var int
44
     */
45
    public $thumbnailHeight = 200;
46
    /**
47
     * @var bool
48
     */
49
    public $useWatermark = false;
50
51
    /**
52
     * @var bool if true and images array empty show "No image"
53
     */
54
55
    public $noImageOnEmptyImages = false;
56
57
    /** @var array $additional Additional data passed to view */
58
    public $additional = [];
59
60
    public function run()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
61
    {
62
        if (is_null($this->model) || empty($this->model->object)) {
63
            return '';
64
        }
65
        $cacheKey = static::className() . ':' . implode(
66
                "_",
67
                [
68
                    $this->model->object->id,
69
                    $this->model->id,
70
                    $this->viewFile,
71
                    $this->limit,
72
                    $this->offset,
73
                    $this->thumbnailOnDemand ? '1' : '0',
74
                    $this->thumbnailWidth,
75
                    $this->thumbnailHeight,
76
                    $this->useWatermark,
77
                ]
78
            );
79
        $result = Yii::$app->cache->get($cacheKey);
80
        if ($result === false) {
81
            if ($this->offset > 0 || !is_null($this->limit)) {
82
                $images = $this->model->getImages()->limit($this->limit)->offset($this->offset)->all();
0 ignored issues
show
Bug introduced by
The method getImages does only exist in app\traits\GetImages, but not in app\properties\HasProper...and yii\db\ActiveRecord.

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...
83
            } else {
84
                $images = $this->model->images;
85
            }
86
            if ($this->noImageOnEmptyImages === true && count($images) === 0) {
87
                return $this->render(
88
                    'noimage',
89
                    [
90
                        'model' => $this->model,
91
                        'thumbnailOnDemand' => $this->thumbnailOnDemand,
92
                        'thumbnailWidth' => $this->thumbnailWidth,
93
                        'thumbnailHeight' => $this->thumbnailHeight,
94
                        'useWatermark' => $this->useWatermark,
95
                        'additional' => $this->additional,
96
                    ]
97
                );
98
            }
99
            $result = $this->render(
100
                $this->viewFile,
101
                [
102
                    'model' => $this->model,
103
                    'images' => $images,
104
                    'thumbnailOnDemand' => $this->thumbnailOnDemand,
105
                    'thumbnailWidth' => $this->thumbnailWidth,
106
                    'thumbnailHeight' => $this->thumbnailHeight,
107
                    'useWatermark' => $this->useWatermark,
108
                    'additional' => $this->additional,
109
                ]
110
            );
111
            Yii::$app->cache->set(
112
                $cacheKey,
113
                $result,
114
                86400,
115
                new TagDependency(
116
                    [
117
                        'tags' => [
118
                            ActiveRecordHelper::getCommonTag(Image::className()),
119
                            ActiveRecordHelper::getCommonTag($this->model->className()),
0 ignored issues
show
Bug introduced by
The method className does only exist in app\properties\HasProper...and yii\db\ActiveRecord, but not in app\traits\GetImages.

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...
120
                        ]
121
                    ]
122
                )
123
            );
124
        }
125
126
        return $result;
127
    }
128
}
129