Passed
Pull Request — develop (#4)
by
unknown
06:51
created

Commerce::getProductDataFromProduct()   F

Complexity

Conditions 22
Paths 199

Size

Total Lines 89
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 89
rs 3.3416
c 0
b 0
f 0
cc 22
nc 199
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
 * Instant Analytics plugin for Craft CMS 3.x
4
 *
5
 * Instant Analytics brings full Google Analytics support to your Twig templates
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\instantanalytics\services;
12
13
use nystudio107\instantanalytics\InstantAnalytics;
14
15
use Craft;
16
use craft\base\Component;
17
use craft\elements\Category;
18
use craft\elements\MatrixBlock;
19
use craft\elements\Tag;
20
use craft\helpers\ArrayHelper;
21
22
use craft\commerce\Plugin as CommercePlugin;
0 ignored issues
show
Bug introduced by
The type craft\commerce\Plugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use craft\commerce\base\Purchasable;
0 ignored issues
show
Bug introduced by
The type craft\commerce\base\Purchasable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use craft\commerce\elements\Product;
0 ignored issues
show
Bug introduced by
The type craft\commerce\elements\Product was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use craft\commerce\elements\Variant;
0 ignored issues
show
Bug introduced by
The type craft\commerce\elements\Variant was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
27
28
/**
29
 * Commerce Service
30
 *
31
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
32
 * @package   InstantAnalytics
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
33
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
34
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
35
class Commerce extends Component
36
{
37
    // Public Methods
38
    // =========================================================================
39
40
    /**
41
     * Send analytics information for the completed order
42
     * @param IAnalytics $analytics the Analytics object
0 ignored issues
show
Bug introduced by
The type nystudio107\instantanalytics\services\IAnalytics was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Doc comment for parameter $analytics does not match actual variable name $order
Loading history...
43
     * @param Order  $order the Product or Variant
0 ignored issues
show
Bug introduced by
The type nystudio107\instantanalytics\services\Order was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Superfluous parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
44
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
45
    public function orderComplete($order = null)
46
    {
47
        if ($order) {
48
            $analytics = InstantAnalytics::$plugin->ia->eventAnalytics("Commerce", "Purchase", $order->number, $order->totalPrice);
49
            
50
            if ($analytics) {
51
                $this->addCommerceOrderToAnalytics($analytics, $order);
52
                // Don't forget to set the product action, in this case to PURCHASE
53
                $analytics->setProductActionToPurchase();
54
                $analytics->sendEvent();
55
56
                Craft::info(Craft::t('instant-analytics', 'orderComplete for `Commerce` - `Purchase` - `{number}` - `{price}`', [ 'number' => $order->number, 'price' => $order->totalPrice ]), __METHOD__);
57
            }
58
        }
59
    }
60
61
    /**
62
     * Send analytics information for the item added to the cart
63
     * @param Order  $order the Product or Variant
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
64
     * @param LineItem  $lineItem the line item that was added
0 ignored issues
show
Bug introduced by
The type nystudio107\instantanalytics\services\LineItem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
65
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
66
    public function addToCart($order = null, $lineItem = null)
0 ignored issues
show
Unused Code introduced by
The parameter $order is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

66
    public function addToCart(/** @scrutinizer ignore-unused */ $order = null, $lineItem = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68
        if ($lineItem) {
69
            $title = $lineItem->purchasable->title;
70
            $quantity = $lineItem->qty;
71
            $analytics = InstantAnalytics::$plugin->ia->eventAnalytics("Commerce", "Add to Cart", $title, $quantity);
72
            
73
            if ($analytics) {
74
                $title = $this->addProductDataFromLineItem($analytics, $lineItem);
75
                $analytics->setEventLabel($title);
76
                // Don't forget to set the product action, in this case to ADD
77
                $analytics->setProductActionToAdd();
78
                $analytics->sendEvent();
79
80
                Craft::info(Craft::t('instant-analytics', 'addToCart for `Commerce` - `Add to Cart` - `{title}` - `{quantity}`', [ 'title' => $title, 'quantity' => $quantity ]), __METHOD__);
81
            }
82
        }
83
    }
84
85
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $order should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $lineItem should have a doc-comment as per coding-style.
Loading history...
86
     * Send analytics information for the item removed from the cart
87
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
88
    public function removeFromCart($order = null, $lineItem = null)
0 ignored issues
show
Unused Code introduced by
The parameter $order is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

88
    public function removeFromCart(/** @scrutinizer ignore-unused */ $order = null, $lineItem = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90
        if ($lineItem) {
91
            $title = $lineItem->purchasable->title;
92
            $quantity = $lineItem->qty;
93
            $analytics = InstantAnalytics::$plugin->ia->eventAnalytics("Commerce", "Remove from Cart", $title, $quantity);
94
            
95
            if ($analytics) {
96
                $title = $this->addProductDataFromLineItem($analytics, $lineItem);
97
                $analytics->setEventLabel($title);
98
                // Don't forget to set the product action, in this case to ADD
99
                $analytics->setProductActionToRemove();
100
                $analytics->sendEvent();
101
102
                Craft::info(Craft::t('instant-analytics', 'removeFromCart for `Commerce` - `Remove to Cart` - `{title}` - `{quantity}`', [ 'title' => $title, 'quantity' => $quantity ]), __METHOD__);
103
            }
104
        }
105
    }
106
107
108
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $order should have a doc-comment as per coding-style.
Loading history...
109
     * Add a Craft Commerce OrderModel to an Analytics object
110
     * @param IAnalytics $analytics the Analytics object
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
111
     * @param Order  $orderModel the Product or Variant
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Doc comment for parameter $orderModel does not match actual variable name $order
Loading history...
112
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
113
    public function addCommerceOrderToAnalytics($analytics = null, $order = null)
114
    {
115
        if ($order && $analytics) {
116
            // First, include the transaction data
117
            $analytics->setTransactionId($order->number)
118
                ->setRevenue($order->totalPrice)
119
                ->setTax($order->totalTax)
120
                ->setShipping($order->totalShippingCost);
121
            
122
            // Coupon code?
123
            if ($order->couponCode) {
124
                $analytics->setCouponCode($order->couponCode);
125
            }
126
127
            // Add each line item in the transaction
128
            // Two cases - variant and non variant products
129
            $index = 1;
130
131
            foreach ($order->lineItems as $key => $lineItem) {
132
                $this->addProductDataFromLineItem($analytics, $lineItem, $index, "");
133
                $index++;
134
            }
135
        }
136
    }
137
138
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $analytics should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $lineItem should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $index should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $listName should have a doc-comment as per coding-style.
Loading history...
139
     * Add a Craft Commerce LineItem to an Analytics object
140
     * @return string the title of the product
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
141
     */
142
    public function addProductDataFromLineItem($analytics = null, $lineItem = null, $index = 0, $listName = "")
143
    {
144
        $result = "";
145
        if ($lineItem) {
146
            if ($analytics) {
147
                //This is the same for both variant and non variant products
148
                $productData = [
149
                    'sku' => $lineItem->purchasable->sku,
150
                    'price' => $lineItem->salePrice,
151
                    'quantity' => $lineItem->qty,
152
                ];
153
                
154
                if (isset($lineItem->purchasable->product)) {
155
                    $productVariant = $lineItem->purchasable->product;
156
157
                    if (!$lineItem->purchasable->product->type->hasVariants) {
158
                        //No variants (i.e. default variant)
159
                        $productData['name'] = $lineItem->purchasable->title;
160
                        $productData['category'] = $lineItem->purchasable->product->type['name'];
161
                    } else {
162
                        // Product with variants
163
                        $productData['name'] = $lineItem->purchasable->product->title;
164
                        $productData['category'] = $lineItem->purchasable->product->type['name'];
165
                        $productData['variant'] = $lineItem->purchasable->title;
166
                    }
167
                } else {
168
                    $productVariant = $lineItem->purchasable;
169
                    $productData['name'] = $lineItem->purchasable->title;
170
                    $productData['category'] = $lineItem->purchasable->type->name;
171
                }
172
                
173
                $result = $productData['name'];
174
                
175
                if ($index) {
176
                    $productData['position'] = $index;
177
                }
178
179
                if ($listName) {
180
                    $productData['list'] = $listName;
181
                }
182
183
                $settings = InstantAnalytics::$plugin->getSettings();
184
185
                if (isset($settings) && isset($settings['productCategoryField']) && $settings['productCategoryField'] != "") {
186
                    $productData['category'] = $this->_pullDataFromField($productVariant, $settings['productCategoryField']);
187
                }
188
189
                if (isset($settings) && isset($settings['productBrandField']) && $settings['productBrandField'] != "") {
190
                    $productData['brand'] = $this->_pullDataFromField($productVariant, $settings['productBrandField']);
191
                }
192
193
                //Add each product to the hit to be sent
194
                $analytics->addProduct($productData);
195
            }
196
        }
197
198
        return $result;
199
    }
200
201
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $listName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $listIndex should have a doc-comment as per coding-style.
Loading history...
202
     * Add a product impression from a Craft Commerce Product or Variant
203
     * @param IAnalytics $analytics the Analytics object
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 37 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
204
     * @param Commerce_ProductModel or Commerce_VariantModel  $productVariant the Product or Variant
0 ignored issues
show
Bug introduced by
The type nystudio107\instantanalytics\services\or was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
205
     * @param int  $index Where the product appears in the list
0 ignored issues
show
Coding Style introduced by
Expected 44 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter name; 1 found
Loading history...
206
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
207
    public function addCommerceProductImpression($analytics = null, $productVariant = null, $index = 0, $listName = "default", $listIndex = 1)
208
    {
209
        if ($productVariant && $analytics) {
210
            $productData = $this->getProductDataFromProduct($productVariant);
211
212
            /**
213
             * As per: https://github.com/theiconic/php-ga-measurement-protocol/issues/26
214
             */
215
            if ($listName && $listIndex) {
216
                $analytics->setProductImpressionListName($listName, $listIndex);
217
            }
218
219
            if ($index) {
220
                $productData['position'] = $index;
221
            }
222
223
            //Add the product to the hit to be sent
224
            $analytics->addProductImpression($productData, $listIndex);
225
226
            Craft::info(Craft::t('instant-analytics', "addCommerceProductImpression for `{sku}` - `{name}` - `{name}` - `{index}`", [ 'sku' => $productData['sku'], 'name' => $productData['name'], 'index' => $index ]), __METHOD__);
227
        }
228
    }
229
230
    /**
231
     * Add a product detail view from a Craft Commerce Product or Variant
232
     * @param IAnalytics $analytics the Analytics object
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
233
     * @param Product or Variant  $productVariant the Product or Variant
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
234
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
235
    public function addCommerceProductDetailView($analytics = null, $productVariant = null)
236
    {
237
        if ($productVariant && $analytics) {
238
            $productData = $this->getProductDataFromProduct($productVariant);
239
240
            // Don't forget to set the product action, in this case to DETAIL
241
            $analytics->setProductActionToDetail();
242
243
            //Add the product to the hit to be sent
244
            $analytics->addProduct($productData);
245
246
            Craft::info(Craft::t('instant-analytics', "addCommerceProductDetailView for `{sku}` - `{name} - `{name}`", [ 'sku' => $productData['sku'], 'name' => $productData['name'] ]), __METHOD__);
247
        }
248
    }
249
250
    /**
251
     * Add a checkout step and option to an Analytics object
252
     * @param IAnalytics $analytics the Analytics object
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
253
     * @param Commerce_OrderModel  $orderModel the Product or Variant
0 ignored issues
show
Bug introduced by
The type nystudio107\instantanaly...ces\Commerce_OrderModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
254
     * @param int $step the checkout step
0 ignored issues
show
Coding Style introduced by
Expected 17 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter name; 1 found
Loading history...
255
     * @param string $option the checkout option
0 ignored issues
show
Coding Style introduced by
Expected 14 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
256
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
257
    public function addCommerceCheckoutStep($analytics = null, $orderModel = null, $step = 1, $option = "")
258
    {
259
        if ($orderModel && $analytics) {
260
            // Add each line item in the transaction
261
            // Two cases - variant and non variant products
262
            $index = 1;
263
264
            foreach ($orderModel->lineItems as $key => $lineItem) {
265
                $this->addProductDataFromLineItem($analytics, $lineItem, $index, "");
266
                $index++;
267
            }
268
269
            $analytics->setCheckoutStep($step);
270
            
271
            if ($option) {
272
                $analytics->setCheckoutStepOption($option);
273
            }
274
275
            // Don't forget to set the product action, in this case to CHECKOUT
276
            $analytics->setProductActionToCheckout();
277
278
            Craft::info(Craft::t('instant-analytics', "addCommerceCheckoutStep step: `{step}` with option: `{option}`", [ 'step' => $step, 'option' => $option ]), __METHOD__);
279
        }
280
    }
281
282
    /**
283
     * Extract product data from a Craft Commerce Product or Variant
284
     * @param Commerce_ProductModel|Commerce_VariantModel  $productVariant the Product or Variant
0 ignored issues
show
Bug introduced by
The type nystudio107\instantanaly...s\Commerce_ProductModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type nystudio107\instantanaly...s\Commerce_VariantModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
285
     * @return array the product data
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
286
     */
287
    public function getProductDataFromProduct($productVariant = null)
288
    {
289
        $result = [];
290
        
291
        if ($productVariant) {
292
            if (is_object($productVariant) && (is_a($productVariant, Product::class) || is_a($productVariant, Purchasable::class))) {
293
                $productType = property_exists($productVariant, "typeId") ? CommercePlugin::getInstance()->getProductTypes()->getProductTypeById($productVariant->typeId) : null;
294
                
295
                if ($productType && $productType->hasVariants) {
296
                    $productVariant = ArrayHelper::getFirstValue($productVariant->getVariants());
0 ignored issues
show
Bug introduced by
The method getFirstValue() does not exist on craft\helpers\ArrayHelper. Did you maybe mean getValue()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

296
                    /** @scrutinizer ignore-call */ 
297
                    $productVariant = ArrayHelper::getFirstValue($productVariant->getVariants());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
297
                    $product = $productVariant->getProduct();
298
                    
299
                    if ($product) {
300
                        $category = $product->getType()['name'];
301
                        $name = $product->title;
302
                        $variant = $productVariant->title;
303
                    } else {
304
                        $category = $productVariant->getType()['name'];
305
                        $name = $productVariant->title;
306
                        $variant = "";
307
                    }
308
                } else {
309
                    if (isset($productVariant->defaultVariantId)) {
310
                        $productVariant = CommercePlugin::getInstance()->getVariants()->getVariantById($productVariant->defaultVariantId);
311
                        $category = $productVariant->getProduct()->getType()['name'];
312
                        $name = $productVariant->title;
313
                        $variant = "";
314
                    } else {
315
                        if (isset($productVariant->product)) {
316
                            $category = $productVariant->product->getType()['name'];
317
                            $name = $productVariant->product->title;
318
                        } else {
319
                            $category = $productVariant->getType()['name'];
320
                            $name = $productVariant->title;
321
                        }
322
323
                        $variant = $productVariant->title;
324
                    }
325
                }
326
            }
327
328
            $productData = [
329
                'sku' => $productVariant->sku,
330
                'name' => $name,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.
Loading history...
331
                'price' => number_format($productVariant->price, 2, '.', ''),
332
                'category' => $category,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $category does not seem to be defined for all execution paths leading up to this point.
Loading history...
333
            ];
334
335
            if ($variant) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $variant does not seem to be defined for all execution paths leading up to this point.
Loading history...
336
                $productData['variant'] = $variant;
337
            }
338
339
            $settings = InstantAnalytics::$plugin->getSettings();
340
            $isVariant = is_a($productVariant, Variant::class);
341
            
342
            if (isset($settings) && isset($settings['productCategoryField']) && $settings['productCategoryField'] != "") {
343
                $productData['category'] = $this->_pullDataFromField(
344
                    $productVariant,
345
                    $settings['productCategoryField']
346
                );
347
348
                if (empty($productData['category']) && $isVariant) {
349
                    $productData['category'] = $this->_pullDataFromField(
350
                        $productVariant->product,
351
                        $settings['productCategoryField']
352
                    );
353
                }
354
            }
355
356
            if (isset($settings) && isset($settings['productBrandField']) && $settings['productBrandField'] != "") {
357
                $productData['brand'] = $this->_pullDataFromField(
358
                    $productVariant,
359
                    $settings['productBrandField'],
360
                    true
361
                );
362
363
                if (empty($productData['brand']) && $isVariant) {
364
                    $productData['brand'] = $this->_pullDataFromField(
365
                        $productVariant,
366
                        $settings['productBrandField'],
367
                        true
368
                    );
369
                }
370
            }
371
372
            $result = $productData;
373
        }
374
375
        return $result;
376
    }
377
378
379
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $productVariant should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $fieldHandle should have a doc-comment as per coding-style.
Loading history...
380
     * Extract the value of a field
381
     * @param Commerce_OrderModel  $orderModel the Product or Variant
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Doc comment for parameter $orderModel does not match actual variable name $productVariant
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
382
     * @param Commerce_LineItemModel  $lineItem the line item that was added
0 ignored issues
show
Bug introduced by
The type nystudio107\instantanaly...\Commerce_LineItemModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Doc comment for parameter $lineItem does not match actual variable name $fieldHandle
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
383
     * @param boolean $isBrand Are we getting the brand?
0 ignored issues
show
Coding Style introduced by
Expected 16 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
384
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
385
     */
386
    private function _pullDataFromField($productVariant, $fieldHandle, $isBrand = false)
387
    {
388
        $result = "";
389
390
        if ($productVariant) {
391
            if ($fieldHandle) {
392
                $srcField = $productVariant[$fieldHandle];
393
394
                if ($srcField == null) {
395
                    $srcField = $productVariant->product->content->attributes[$fieldHandle];
396
                }
397
398
                if (isset($srcField->elementType)) {
399
                    switch ($srcField->elementType->classHandle) {
400
                        case MatrixBlock::class:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
401
                            break;
402
                        case Tag::class:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
403
                            break;
404
                        case Category::class: {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
405
                            $cats = [];
406
407
                            if ($isBrand) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
408
                                // Because we can only have one brand, we'll get
409
                                // the very last category. This means if our
410
                                // brand is a sub-category, we'll get the child
411
                                // not the parent.
412
                                /** @var CategoryModel $cat */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
413
                                foreach ($srcField as $cat) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 28 spaces, found 32
Loading history...
414
                                    $cats = [$cat->getTitle()];
415
                                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 28 spaces, found 32
Loading history...
416
                            } else {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
417
                                // For every category, show its ancestors
418
                                // delimited by a slash.
419
                                /** @var CategoryModel $cat */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
420
                                foreach ($srcField as $cat) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 28 spaces, found 32
Loading history...
421
                                    $name = $cat->getTitle();
422
423
                                    while ($cat = $cat->getParent()) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 32 spaces, found 36
Loading history...
424
                                        $name = $cat->getTitle() . "/" . $name;
425
                                    }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 32 spaces, found 36
Loading history...
426
427
                                    $cats[] = $name;
428
                                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 28 spaces, found 32
Loading history...
429
                            }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
430
431
                            // Join separate categories with a pipe.
432
                            $result = implode("|", $cats);
433
                            break;
434
                        }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
Coding Style introduced by
Case breaking statement indented incorrectly; expected 28 spaces, found 24
Loading history...
435
436
                        default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
437
                            $result = strip_tags($srcField);
0 ignored issues
show
Bug introduced by
$srcField of type object is incompatible with the type string expected by parameter $str of strip_tags(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

437
                            $result = strip_tags(/** @scrutinizer ignore-type */ $srcField);
Loading history...
438
                            break;
439
                    }
440
                } else {
441
                    $result = strip_tags($srcField);
442
                }
443
            }
444
        }
445
446
        return $result;
447
    }
448
    
449
}
450