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.
Passed
Pull Request — master (#313)
by
unknown
09:28
created

DefaultHandler::processData()   D

Complexity

Conditions 10
Paths 256

Size

Total Lines 58
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 58
rs 4.9315
cc 10
eloc 40
nc 256
nop 1

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\shop\components\GoogleMerchants;
4
5
6
use app\models\Property;
7
use app\modules\shop\helpers\CurrencyHelper;
8
use app\modules\shop\models\Category;
9
use app\modules\shop\models\GoogleFeed;
10
use app\modules\shop\models\Product;
11
use yii\console\Exception;
12
use yii\helpers\Url;
13
14
15
class DefaultHandler implements ModificationDataInterface
16
{
17
18
    protected static $breadcrumbsData = [];
19
20
    protected static $modelSetting = null;
21
22
23
    public static function processData(ModificationDataEvent $event)
24
    {
25
        if (!self::$modelSetting) {
26
            self::$modelSetting = new GoogleFeed();
27
            self::$modelSetting->loadConfig();
28
        }
29
        $event->dataArray = [
30
            'title' => self::getRelation($event->model, 'item_title', $event->model->name),
31
            'description' => self::getRelation($event->model, 'item_description', $event->model->announce),
32
            'link' => $event->sender->host . htmlspecialchars(Url::toRoute(['@product', 'model' => $event->model])),
33
            'g:id' => $event->model->id,
34
            'g:condition' => self::$modelSetting->item_condition,
35
            'g:product_type' => self::getProductType($event->model),
36
            'g:availability' => static::getAvailability($event->model)
37
        ];
38
        if ($manufacturer = self::getRelation($event->model, 'item_brand', false)) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
            $event->dataArray['g:brand'] = $manufacturer;
40
        }
41
        if ($gin = self::getRelation($event->model, 'item_gtin', false)) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
            $event->dataArray['g:gtin'] = $gin;
43
        }
44
        if ($mpn = self::getRelation($event->model, 'item_mpn', false)) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
            $event->dataArray['g:mpn'] = $mpn;
46
        }
47
48
        if ($item_google_product_category = self::getRelation($event->model, 'item_google_product_category', false)) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
            $event->dataArray['g:google_product_category'] = $item_google_product_category;
50
        }
51
52
        if (!empty($event->model->old_price) && $event->model->old_price > $event->model->price) {
53
            $event->dataArray['g:price'] = static::getPrice(
54
                $event->model,
55
                $event->sender->mainCurrency,
56
                $event->model->old_price
57
            );
58
            $event->dataArray['g:sale_price'] = static::getPrice(
59
                $event->model,
60
                $event->sender->mainCurrency,
61
                $event->model->price
62
            );
63
        } else {
64
65
            $event->dataArray['g:price'] = static::getPrice(
66
                $event->model,
67
                $event->sender->mainCurrency,
68
                $event->model->price
69
            );
70
        }
71
        $imageObject = $event->model->image;
72
        if ($imageObject) {
73
            $event->dataArray['g:image_link'] = htmlspecialchars(
74
                $event->sender->host . $event->model->image->getOriginalUrl()
75
            );
76
        }
77
        if ($event->model->parent_id !== 0) {
78
            $event->dataArray['g:item_group_id'] = $event->model->parent_id;
79
        }
80
    }
81
82
83
    protected static function getRelation($model, $relationName, $default_value = '')
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...
84
    {
85
        $result = $default_value;
86
        $relation = self::$modelSetting->$relationName;
87
88
        if ($relation['type'] === 'field' && !empty($relation['key'])) {
89
            try {
90
                $result = $model->{$relation['key']};
91
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class yii\console\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
92
            }
93
        } elseif ($relation['type'] === 'property' && !empty($relation['key'])) {
94
            try {
95
                $propertyKey = Property::find()->select('key')->where(['id' => $relation['key']])->asArray()->scalar();
96
                $result = $model->property($propertyKey);
97
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class yii\console\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
98
            }
99
        } elseif ($relation['type'] === 'relation' && !empty($relation['key']) && !empty($relation['value'])) {
100
            $relModel = $model->{$relation['key']}();
101
            $result = $relModel->$relation['value'];
102
        }
103
104
        return $result;
105
    }
106
107
108
    protected static function getProductType(Product $model)
109
    {
110
        if (!isset(self::$breadcrumbsData[$model->main_category_id])) {
111
            $parentIds = $model->getMainCategory()->getParentIds();
112
            $breadcrumbs = [];
113
            foreach ($parentIds as $id) {
114
                $breadcrumbs[] = Category::find()->select(['name'])->where(['id' => $id])->asArray()->scalar();
115
            }
116
            $breadcrumbs[] = $model->getMainCategory()->name;
117
            self::$breadcrumbsData[$model->main_category_id] = $breadcrumbs;
118
        }
119
        return htmlspecialchars(implode(' > ', self::$breadcrumbsData[$model->main_category_id]));
120
    }
121
122
    protected static function getPrice(Product $model, $mainCurrency, $price)
123
    {
124
        return number_format(
125
            CurrencyHelper::convertCurrencies(
126
                $price,
127
                $model->currency,
128
                $mainCurrency
129
            ),
130
            2,
131
            '.',
132
            ''
133
        ) . ' ' . $mainCurrency->iso_code;
134
    }
135
136
    protected static function getAvailability(Product $model)
137
    {
138
        if ($model->unlimited_count === 1) {
139
            $inStock = 'in stock';
140
        } else {
141
            $inStock = 'out of stock';
142
            foreach ($model->getWarehousesState() as $warehouse) {
143
                if ($warehouse->in_warehouse > 0) {
144
                    $inStock = 'in stock';
145
                    break;
146
                }
147
            }
148
        }
149
        return $inStock;
150
    }
151
}
152