GSTTaxDecorator   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 13.74 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 18
loc 131
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A BuyableCalculatedExcludedFrom() 9 9 3
A BuyableCalculatedAdditionalTax() 9 9 3
B updateCMSFields() 0 64 6
A TaxInclusivePrice() 0 4 1
A TaxExclusivePrice() 0 4 1

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
 * @author Nicolaas [at] sunnysideup.co.nz
6
 * @package: ecommerce
7
 * @sub-package: ecommerce_tax
8
 * @description: special tax rules for individual buyables
9
 *
10
 *
11
 */
12
13
14
class GSTTaxDecorator 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...
15
{
16
17
    /**
18
     * standard SS method
19
     * @return Array
20
     */
21
    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...
22
        "ExcludedFrom" => "GSTTaxModifierOptions",
23
        "AdditionalTax" => "GSTTaxModifierOptions"
24
    );
25
26
    /**
27
     * for variations, use product for data
28
     * @return DataList
29
     */
30 View Code Duplication
    public function BuyableCalculatedExcludedFrom()
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...
31
    {
32
        if ($this->owner instanceof ProductVariation) {
0 ignored issues
show
Bug introduced by
The class ProductVariation does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
33
            if ($product = $this->owner->Product()) {
34
                return $product->ExcludedFrom();
35
            }
36
        }
37
        return $this->owner->ExcludedFrom();
38
    }
39
40
    /**
41
     * for variations, use product for data
42
     * @return DataList
43
     */
44 View Code Duplication
    public function BuyableCalculatedAdditionalTax()
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...
45
    {
46
        if ($this->owner instanceof ProductVariation) {
0 ignored issues
show
Bug introduced by
The class ProductVariation does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
47
            if ($product = $this->owner->Product()) {
48
                return $product->AdditionalTax();
49
            }
50
        }
51
        return $this->owner->AdditionalTax();
52
    }
53
54
55
    /**
56
     * standard SS method
57
     * @param Object - $fields (FieldList)
58
     * @return Object - FieldList
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
59
     */
60
    public function updateCMSFields(FieldList $fields)
61
    {
62
        $additionalWhereForDefault = "";
63
        $fields->removeByName("ExcludedFrom");
64
        $fields->removeByName("AdditionalTax");
65
        $tabName = "Root.Tax";
66
        if ($this->owner instanceof ProductVariation) {
0 ignored issues
show
Bug introduced by
The class ProductVariation does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67
            $fields->addFieldToTab(
68
                $tabName,
69
                new LiteralField(
70
                    "SeeProductForAdditionalTax",
71
                    _t("GSTTaxModifier.SEE_PARENT", "See parent Product for Additional Tax")
72
                )
73
            );
74
        } else {
75
            //additional taxes
76
            $additionalOptions = GSTTaxModifierOptions::get()->filter(array("DoesNotApplyToAllProducts" => 1));
77
            if ($additionalOptions->count()) {
78
                $additionalOptionsList = $additionalOptions->map()->toArray();
79
                $fields->addFieldToTab(
80
                    $tabName,
81
                    new CheckboxSetField(
82
                        "AdditionalTax",
83
                        _t("GSTTaxMofidifier.ADDITIONAL_TAXES", "Additional taxes ..."),
84
                        $additionalOptionsList
85
                    )
86
                );
87
            }
88
        }
89
        if ($this->owner instanceof ProductVariation) {
0 ignored issues
show
Bug introduced by
The class ProductVariation does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
90
            $fields->addFieldToTab(
91
                $tabName,
92
                new LiteralField(
93
                    "SeeProductForExcludedFrom",
94
                    _t("GSTTaxModifier.SEE_PARRENT", "See parent product for excluded taxes")
95
                )
96
            );
97
        } else {
98
            //excluded options
99
            $excludedOptions = GSTTaxModifierOptions::get()->filter(array("DoesNotApplyToAllProducts" => 0));
100
            if ($excludedOptions->count()) {
101
                $excludedOptionsList = $excludedOptions->map()->toArray();
102
                $fields->addFieldToTab(
103
                    $tabName,
104
                    new CheckboxSetField(
105
                        "ExcludedFrom",
106
                        _t("GSTTaxMofidifier.EXCLUDE_TAXES", "Taxes that do not apply ..."),
107
                        $excludedOptionsList
108
                    )
109
                );
110
                $additionalWhereForDefault = " \"GSTTaxModifierOptions\".\"ID\" NOT IN (".implode(", ", $excludedOptions->map("ID", "ID")->toArray()).")";
111
            }
112
        }
113
        //default options
114
        $defaultOptions = GSTTaxModifierOptions::get()
115
            ->filter(array("DoesNotApplyToAllProducts" => 0))
116
            ->where($additionalWhereForDefault);
117
        if ($defaultOptions->count()) {
118
            $fields->addFieldToTab(
119
                $tabName,
120
                new ReadonlyField("AlwaysApplies", "+ ".implode(", ", $defaultOptions->map()->toArray()).".")
121
            );
122
        }
123
    }
124
125
    /**
126
     * returns the calculated price for the buyable including tax
127
     * @return float
0 ignored issues
show
Documentation introduced by
Should the return type not be double|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
128
     *
129
     */
130
    public function TaxInclusivePrice()
131
    {
132
        user_error("to be completed");
133
    }
134
135
    /**
136
     * returns the calculated price for the buyable excluding tax
137
     * @return float
0 ignored issues
show
Documentation introduced by
Should the return type not be double|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
138
     *
139
     */
140
    public function TaxExclusivePrice()
141
    {
142
        user_error("to be completed");
143
    }
144
}
145