Passed
Pull Request — develop (#4)
by
unknown
03:42
created

Commerce::orderComplete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
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\db\CategoryQuery;
18
use craft\elements\db\MatrixBlockQuery;
19
use craft\elements\db\TagQuery;
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
                    $hasVariants = $lineItem->purchasable->product->type->hasVariants ?? null;
158
159
                    if (!$hasVariants) {
160
                        //No variants (i.e. default variant)
161
                        $productData['name'] = $lineItem->purchasable->title;
162
                        $productData['category'] = $lineItem->purchasable->product->type['name'];
163
                    } else {
164
                        // Product with variants
165
                        $productData['name'] = $lineItem->purchasable->product->title;
166
                        $productData['category'] = $lineItem->purchasable->product->type['name'];
167
                        $productData['variant'] = $lineItem->purchasable->title;
168
                    }
169
                } else {
170
                    $productVariant = $lineItem->purchasable;
171
                    $productData['name'] = $lineItem->purchasable->title;
172
                    $productData['category'] = $lineItem->purchasable->type->name;
173
                }
174
                
175
                $result = $productData['name'];
176
                
177
                if ($index) {
178
                    $productData['position'] = $index;
179
                }
180
181
                if ($listName) {
182
                    $productData['list'] = $listName;
183
                }
184
185
                $settings = InstantAnalytics::$plugin->getSettings();
186
187
                if (isset($settings) && isset($settings['productCategoryField']) && $settings['productCategoryField'] != "") {
188
                    $productData['category'] = $this->_pullDataFromField($productVariant, $settings['productCategoryField']);
189
                }
190
191
                if (isset($settings) && isset($settings['productBrandField']) && $settings['productBrandField'] != "") {
192
                    $productData['brand'] = $this->_pullDataFromField($productVariant, $settings['productBrandField']);
193
                }
194
195
                //Add each product to the hit to be sent
196
                $analytics->addProduct($productData);
197
            }
198
        }
199
200
        return $result;
201
    }
202
203
    /**
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...
204
     * Add a product impression from a Craft Commerce Product or Variant
205
     * @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...
206
     * @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...
207
     * @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...
208
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
209
    public function addCommerceProductImpression($analytics = null, $productVariant = null, $index = 0, $listName = "default", $listIndex = 1)
210
    {
211
        if ($productVariant && $analytics) {
212
            $productData = $this->getProductDataFromProduct($productVariant);
213
214
            /**
215
             * As per: https://github.com/theiconic/php-ga-measurement-protocol/issues/26
216
             */
217
            if ($listName && $listIndex) {
218
                $analytics->setProductImpressionListName($listName, $listIndex);
219
            }
220
221
            if ($index) {
222
                $productData['position'] = $index;
223
            }
224
225
            //Add the product to the hit to be sent
226
            $analytics->addProductImpression($productData, $listIndex);
227
228
            Craft::info(Craft::t('instant-analytics', "addCommerceProductImpression for `{sku}` - `{name}` - `{name}` - `{index}`", [ 'sku' => $productData['sku'], 'name' => $productData['name'], 'index' => $index ]), __METHOD__);
229
        }
230
    }
231
232
    /**
233
     * Add a product detail view from a Craft Commerce Product or Variant
234
     * @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...
235
     * @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...
236
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
237
    public function addCommerceProductDetailView($analytics = null, $productVariant = null)
238
    {
239
        if ($productVariant && $analytics) {
240
            $productData = $this->getProductDataFromProduct($productVariant);
241
242
            // Don't forget to set the product action, in this case to DETAIL
243
            $analytics->setProductActionToDetail();
244
245
            //Add the product to the hit to be sent
246
            $analytics->addProduct($productData);
247
248
            Craft::info(Craft::t('instant-analytics', "addCommerceProductDetailView for `{sku}` - `{name} - `{name}`", [ 'sku' => $productData['sku'], 'name' => $productData['name'] ]), __METHOD__);
249
        }
250
    }
251
252
    /**
253
     * Add a checkout step and option to an Analytics object
254
     * @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...
255
     * @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...
256
     * @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...
257
     * @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...
258
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
259
    public function addCommerceCheckoutStep($analytics = null, $orderModel = null, $step = 1, $option = "")
260
    {
261
        if ($orderModel && $analytics) {
262
            // Add each line item in the transaction
263
            // Two cases - variant and non variant products
264
            $index = 1;
265
266
            foreach ($orderModel->lineItems as $key => $lineItem) {
267
                $this->addProductDataFromLineItem($analytics, $lineItem, $index, "");
268
                $index++;
269
            }
270
271
            $analytics->setCheckoutStep($step);
272
            
273
            if ($option) {
274
                $analytics->setCheckoutStepOption($option);
275
            }
276
277
            // Don't forget to set the product action, in this case to CHECKOUT
278
            $analytics->setProductActionToCheckout();
279
280
            Craft::info(Craft::t('instant-analytics', "addCommerceCheckoutStep step: `{step}` with option: `{option}`", [ 'step' => $step, 'option' => $option ]), __METHOD__);
281
        }
282
    }
283
284
    /**
285
     * Extract product data from a Craft Commerce Product or Variant
286
     * @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...
287
     * @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...
288
     */
289
    public function getProductDataFromProduct($productVariant = null)
290
    {
291
        $result = [];
292
        
293
        if ($productVariant) {
294
            if (is_object($productVariant) && (is_a($productVariant, Product::class) || is_a($productVariant, Purchasable::class))) {
295
                $productType = property_exists($productVariant, "typeId") ? CommercePlugin::getInstance()->getProductTypes()->getProductTypeById($productVariant->typeId) : null;
296
                
297
                if ($productType && $productType->hasVariants) {
298
                    $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

298
                    /** @scrutinizer ignore-call */ 
299
                    $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...
299
                    $product = $productVariant->getProduct();
300
                    
301
                    if ($product) {
302
                        $category = $product->getType()['name'];
303
                        $name = $product->title;
304
                        $variant = $productVariant->title;
305
                    } else {
306
                        $category = $productVariant->getType()['name'];
307
                        $name = $productVariant->title;
308
                        $variant = "";
309
                    }
310
                } else {
311
                    if (isset($productVariant->defaultVariantId)) {
312
                        $productVariant = CommercePlugin::getInstance()->getVariants()->getVariantById($productVariant->defaultVariantId);
313
                        $category = $productVariant->getProduct()->getType()['name'];
314
                        $name = $productVariant->title;
315
                        $variant = "";
316
                    } else {
317
                        if (isset($productVariant->product)) {
318
                            $category = $productVariant->product->getType()['name'];
319
                            $name = $productVariant->product->title;
320
                        } else {
321
                            $category = $productVariant->getType()['name'];
322
                            $name = $productVariant->title;
323
                        }
324
325
                        $variant = $productVariant->title;
326
                    }
327
                }
328
            }
329
330
            $productData = [
331
                'sku' => $productVariant->sku,
332
                '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...
333
                'price' => number_format($productVariant->price, 2, '.', ''),
334
                '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...
335
            ];
336
337
            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...
338
                $productData['variant'] = $variant;
339
            }
340
341
            $settings = InstantAnalytics::$plugin->getSettings();
342
            $isVariant = is_a($productVariant, Variant::class);
343
            
344
            if (isset($settings) && isset($settings['productCategoryField']) && $settings['productCategoryField'] != "") {
345
                $productData['category'] = $this->_pullDataFromField(
346
                    $productVariant,
347
                    $settings['productCategoryField']
348
                );
349
350
                if (empty($productData['category']) && $isVariant) {
351
                    $productData['category'] = $this->_pullDataFromField(
352
                        $productVariant->product,
353
                        $settings['productCategoryField']
354
                    );
355
                }
356
            }
357
358
            if (isset($settings) && isset($settings['productBrandField']) && $settings['productBrandField'] != "") {
359
                $productData['brand'] = $this->_pullDataFromField(
360
                    $productVariant,
361
                    $settings['productBrandField'],
362
                    true
363
                );
364
365
                if (empty($productData['brand']) && $isVariant) {
366
                    $productData['brand'] = $this->_pullDataFromField(
367
                        $productVariant,
368
                        $settings['productBrandField'],
369
                        true
370
                    );
371
                }
372
            }
373
374
            $result = $productData;
375
        }
376
377
        return $result;
378
    }
379
380
381
    /**
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...
382
     * Extract the value of a field
383
     * @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...
384
     * @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...
385
     * @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...
386
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
387
     */
388
    private function _pullDataFromField($productVariant, $fieldHandle, $isBrand = false)
389
    {
390
        $result = "";
391
392
        if ($productVariant) {
393
            if ($fieldHandle) {
394
                $srcField = $productVariant[$fieldHandle];
395
396
                if ($srcField == null) {
397
                    $srcField = $productVariant->product->$fieldHandle;
398
                }
399
400
                switch (get_class($srcField)) {
401
                    case MatrixBlockQuery::class:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
402
                        break;
403
                    case TagQuery::class:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
404
                        break;
405
                    case CategoryQuery::class: {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
406
                        $cats = [];
407
408
                        if ($isBrand) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
409
                            // Because we can only have one brand, we'll get
410
                            // the very last category. This means if our
411
                            // brand is a sub-category, we'll get the child
412
                            // not the parent.
413
                            foreach ($srcField->all() as $cat) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
414
                                $cats = [$cat->title];
415
                            }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
416
                        } else {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
417
                            // For every category, show its ancestors
418
                            // delimited by a slash.
419
                            foreach ($srcField->all() as $cat) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
420
                                $name = $cat->title;
421
422
                                while ($cat = $cat->parent) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 28 spaces, found 32
Loading history...
423
                                    $name = $cat->title . "/" . $name;
424
                                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 28 spaces, found 32
Loading history...
425
426
                                $cats[] = $name;
427
                            }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
428
                        }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
429
430
                        // Join separate categories with a pipe.
431
                        $result = implode("|", $cats);
432
                        break;
433
                    }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
Coding Style introduced by
Case breaking statement indented incorrectly; expected 24 spaces, found 20
Loading history...
434
435
                    default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
436
                        $result = strip_tags($srcField);
437
                        break;
438
                }
439
            }
440
        }
441
442
        return $result;
443
    }
444
    
445
}
446