EcommerceAlsoRecommendedDOD   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 95
Duplicated Lines 35.79 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 34
loc 95
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B updateCMSFields() 0 28 2
C onAfterWrite() 18 23 9
A EcommerceRecommendedProductsForSale() 8 8 2
A RecommendedForForSale() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
4
5
class EcommerceAlsoRecommendedDOD extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    private static $many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
8
        'EcommerceRecommendedProducts' => 'Product'
9
    );
10
11
    private static $belongs_many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $belongs_many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
        'RecommendedFor' => 'Product'
13
    );
14
15
    public function updateCMSFields(FieldList $fields)
16
    {
17
        if ($this->owner instanceof Product) {
18
            $fields->addFieldToTab(
19
                'Root.Links',
20
                GridField::create(
21
                    'EcommerceRecommendedProducts',
22
                    'Also Recommended Products',
23
                    $this->owner->EcommerceRecommendedProducts(),
24
                    $config = GridFieldBasicPageRelationConfig::create()
25
                )
26
            );
27
            $component = $config->getComponentByType('GridFieldAddExistingAutocompleter');
28
            $component->setSearchFields(array("InternalItemID", "Title"));
29
30
            $fields->addFieldToTab(
31
                'Root.Links',
32
                GridField::create(
33
                    'RecommendedFor',
34
                    'Recommended For',
35
                    $this->owner->RecommendedFor(),
36
                    $config = GridFieldBasicPageRelationConfig::create()
37
                )
38
            );
39
            $component = $config->getComponentByType('GridFieldAddExistingAutocompleter');
40
            $component->setSearchFields(array("InternalItemID", "Title"));
41
        }
42
    }
43
44
    /**
45
     *
46
     * small cleanup
47
     */
48
    public function onAfterWrite()
49
    {
50
        $products = $this->owner->EcommerceRecommendedProducts();
51 View Code Duplication
        if ($products->count()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
            foreach ($products as $product) {
53
                if (!$product instanceof Product) {
54
                    $products->remove($product);
55
                } elseif (!$product->AllowPurchase) {
56
                    $products->remove($product);
57
                }
58
            }
59
        }
60
        $products = $this->owner->RecommendedFor();
61 View Code Duplication
        if ($products->count()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            foreach ($products as $product) {
63
                if (!$product instanceof Product) {
64
                    $products->remove($product);
65
                } elseif (!$product->AllowPurchase) {
66
                    $products->remove($product);
67
                }
68
            }
69
        }
70
    }
71
72
    /**
73
     * only returns the products that are for sale
74
     * if only those need to be showing.
75
     * @return DataList
76
     */
77 View Code Duplication
    public function EcommerceRecommendedProductsForSale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        if ($this->owner->EcomConfig()->OnlyShowProductsThatCanBePurchased) {
80
            return $this->owner->EcommerceRecommendedProducts()->filter(array("AllowPurchase" => 1));
81
        } else {
82
            return $this->owner->EcommerceRecommendedProducts();
83
        }
84
    }
85
86
    /**
87
     * only returns the products that are for sale
88
     * if only those need to be showing.
89
     * @return DataList
90
     */
91 View Code Duplication
    public function RecommendedForForSale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        if ($this->owner->EcomConfig()->OnlyShowProductsThatCanBePurchased) {
94
            return $this->owner->RecommendedFor()->filter(array("AllowPurchase" => 1));
95
        } else {
96
            return $this->owner->RecommendedFor();
97
        }
98
    }
99
}
100