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 — master ( e91cf6...3a6b3f )
by Alexander
10:43
created

DefaultHandler   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 29
lcom 1
cbo 9
dl 0
loc 146
rs 10
c 4
b 0
f 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAvailability() 0 15 4
C processData() 0 56 10
C getRelation() 0 23 10
A getProductType() 0 13 3
A getPrice() 0 10 1
A getShipping() 0 11 1
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
            'g:shipping' => static::getShipping()
38
        ];
39
        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...
40
            $event->dataArray['g:brand'] = htmlspecialchars($manufacturer);
41
        }
42
        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...
43
            $event->dataArray['g:gtin'] = htmlspecialchars($gin);
44
        }
45
        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...
46
            $event->dataArray['g:mpn'] = htmlspecialchars($mpn);
47
        }
48
49
        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...
50
            $event->dataArray['g:google_product_category'] = htmlspecialchars($item_google_product_category);
51
        }
52
53
        if (!empty($event->model->old_price) && $event->model->old_price > $event->model->price) {
54
            $event->dataArray['g:price'] = static::getPrice($event->model,
55
                $event->sender->mainCurrency,
56
                $event->model->old_price
57
            );
58
            $event->dataArray['g:g:sale_price'] = static::getPrice($event->model,
59
                $event->sender->mainCurrency,
60
                $event->model->price
61
            );
62
        } else {
63
64
            $event->dataArray['g:price'] = static::getPrice($event->model,
65
                $event->sender->mainCurrency,
66
                $event->model->price
67
            );
68
        }
69
        $imageObject = $event->model->image;
70
        if ($imageObject) {
71
            $event->dataArray['g:image_link'] = htmlspecialchars(
72
                $event->sender->host . $event->model->image->getOriginalUrl()
73
            );
74
        }
75
        if ($event->model->parent_id !== 0) {
76
            $event->dataArray['g:item_group_id'] = $event->model->parent_id;
77
        }
78
    }
79
80
81
    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...
82
    {
83
        $result = $default_value;
84
        $relation = self::$modelSetting->$relationName;
85
86
        if ($relation['type'] === 'field' && !empty($relation['key'])) {
87
            try {
88
                $result = $model->{$relation['key']};
89
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
90
            }
91
        } elseif ($relation['type'] === 'property' && !empty($relation['key'])) {
92
            try {
93
                $propertyKey = Property::find()->select('key')->where(['id' => $relation['key']])->asArray()->scalar();
94
                $result = $model->property($propertyKey);
95
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
96
            }
97
        } elseif ($relation['type'] === 'relation' && !empty($relation['key']) && !empty($relation['value'])) {
98
            $relModel = $model->{$relation['key']}();
99
            $result = $relModel->$relation['value'];
100
        }
101
102
        return $result;
103
    }
104
105
106
    protected static function getProductType(Product $model)
107
    {
108
        if (!isset(self::$breadcrumbsData[$model->main_category_id])) {
109
            $parentIds = $model->getMainCategory()->getParentIds();
110
            $breadcrumbs = [];
111
            foreach ($parentIds as $id) {
112
                $breadcrumbs[] = Category::find()->select(['name'])->where(['id' => $id])->asArray()->scalar();
113
            }
114
            $breadcrumbs[] = $model->getMainCategory()->name;
115
            self::$breadcrumbsData[$model->main_category_id] = $breadcrumbs;
116
        }
117
        return htmlspecialchars(implode(' > ', self::$breadcrumbsData[$model->main_category_id]));
118
    }
119
120
    protected static function getPrice(Product $model, $mainCurrency, $price)
121
    {
122
        return number_format(
123
            CurrencyHelper::convertCurrencies($price, $model->currency,
0 ignored issues
show
Bug introduced by
The property currency does not seem to exist. Did you mean currency_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
124
                $mainCurrency),
125
            2,
126
            '.',
127
            ''
128
        ) . ' ' . $mainCurrency->iso_code;
129
    }
130
131
    protected static function getAvailability(Product $model)
132
    {
133
        if ($model->unlimited_count === 1) {
134
            $inStock = 'in stock';
135
        } else {
136
            $inStock = 'out of stock';
137
            foreach ($model->getWarehousesState() as $warehouse) {
138
                if ($warehouse->in_warehouse > 0) {
139
                    $inStock = 'in stock';
140
                    break;
141
                }
142
            }
143
        }
144
        return $inStock;
145
    }
146
147
    protected static function getShipping()
148
    {
149
        $data = explode(':', self::$modelSetting->shop_delivery_price);
0 ignored issues
show
Documentation introduced by
The property shop_delivery_price does not exist on object<app\modules\shop\models\GoogleFeed>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
150
151
        return [
152
            'g:country' => $data[0],
153
            'g:service' => $data[1],
154
            'g:price' => $data[2]
155
        ];
156
157
    }
158
159
160
}