Completed
Push — master ( 67ead9...61a703 )
by Daniel
12s
created

UserDefinedForm_EmailRecipientCondition::matches()   C

Complexity

Conditions 12
Paths 30

Size

Total Lines 42
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 12.0268

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 33
cts 35
cp 0.9429
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 36
nc 30
nop 1
crap 12.0268

How to fix   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
4
/**
5
 * Declares a condition that determines whether an email can be sent to a given recipient
6
 *
7
 * @method UserDefinedForm_EmailRecipient Parent()
8
 *
9
 * @property Enum ConditionOption
10
 * @property Varchar ConditionValue
11
 *
12
 * @method EditableFormField ConditionField
13
 */
14
class UserDefinedForm_EmailRecipientCondition extends DataObject
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
     * List of options
19
     *
20
     * @config
21
     * @var array
22
     */
23
    private static $condition_options = array(
0 ignored issues
show
Unused Code introduced by
The property $condition_options 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...
24
        "IsBlank" => "Is blank",
25
        "IsNotBlank" => "Is not blank",
26
        "Equals" => "Equals",
27
        "NotEquals" => "Doesn't equal",
28
        "ValueLessThan" => "Less than",
29
        "ValueLessThanEqual" => "Less than or equal",
30
        "ValueGreaterThan" => "Greater than",
31
        "ValueGreaterThanEqual" => "Greater than or equal"
32
    );
33
34
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db 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...
35
        'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")',
36
        'ConditionValue' => 'Varchar'
37
    );
38
39
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one 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...
40
        'Parent' => 'UserDefinedForm_EmailRecipient',
41
        'ConditionField' => 'EditableFormField'
42
    );
43
44
    /**
45
     *
46
     * Determine if this rule matches the given condition
47
     *
48
     * @param $data
49
     *
50
     * @return bool|null
51
     * @throws LogicException
52
     */
53 2
    public function matches($data)
54
    {
55 2
        $fieldName = $this->ConditionField()->Name;
0 ignored issues
show
Documentation Bug introduced by
The method ConditionField does not exist on object<UserDefinedForm_EmailRecipientCondition>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56 2
        $fieldValue = isset($data[$fieldName]) ? $data[$fieldName] : null;
57 2
        $conditionValue = $this->ConditionValue;
58 2
        $result = null;
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59 2
        switch ($this->ConditionOption) {
60 2
            case 'IsBlank':
61 2
                $result = empty($fieldValue);
62 2
                break;
63 2
            case 'IsNotBlank':
64 2
                $result = !empty($fieldValue);
65 2
                break;
66 2
            case 'ValueLessThan':
67 1
                $result = ($fieldValue < $conditionValue);
68 1
                break;
69 2
            case 'ValueLessThanEqual':
70 1
                $result = ($fieldValue <= $conditionValue);
71 1
                break;
72 2
            case 'ValueGreaterThan':
73 1
                $result = ($fieldValue > $conditionValue);
74 1
                break;
75 2
            case 'ValueGreaterThanEqual':
76 1
                $result = ($fieldValue >= $conditionValue);
77 1
                break;
78 2
            case 'NotEquals':
79 2
            case 'Equals':
80 2
                $result = is_array($fieldValue)
81 1
                    ? in_array($conditionValue, $fieldValue)
82 2
                    : $fieldValue == $conditionValue;
83
84 2
                if ($this->ConditionOption == 'NotEquals') {
85 2
                    $result = !($result);
86
                }
87 2
                break;
88
            default:
89
                throw new LogicException("Unhandled rule {$this->ConditionOption}");
90
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
91
        }
92
93 2
        return $result;
94
    }
95
96
        /**
97
     * Return whether a user can create an object of this type
98
     *
99
     * @param Member $member
100
     * @param array $context Virtual parameter to allow context to be passed in to check
0 ignored issues
show
Bug introduced by
There is no parameter named $context. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
101
     * @return bool
102
     */
103 View Code Duplication
    public function canCreate($member = null)
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...
104
    {
105
        // Check parent page
106
        $parent = $this->getCanCreateContext(func_get_args());
107
        if ($parent) {
108
            return $parent->canEdit($member);
109
        }
110
111
        // Fall back to secure admin permissions
112
        return parent::canCreate($member);
113
    }
114
115
    /**
116
     * Helper method to check the parent for this object
117
     *
118
     * @param array $args List of arguments passed to canCreate
119
     * @return SiteTree Parent page instance
120
     */
121 View Code Duplication
    protected function getCanCreateContext($args)
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...
122
    {
123
        // Inspect second parameter to canCreate for a 'Parent' context
124
        if (isset($args[1]['Parent'])) {
125
            return $args[1]['Parent'];
126
        }
127
        // Hack in currently edited page if context is missing
128
        if (Controller::has_curr() && Controller::curr() instanceof CMSMain) {
129
            return Controller::curr()->currentPage();
130
        }
131
132
        // No page being edited
133
        return null;
134
    }
135
136
    /**
137
     * @param Member
138
     *
139
     * @return boolean
140
     */
141
    public function canView($member = null)
142
    {
143
        return $this->Parent()->canView($member);
144
    }
145
146
    /**
147
     * @param Member
148
     *
149
     * @return boolean
150
     */
151
    public function canEdit($member = null)
152
    {
153
        return $this->Parent()->canEdit($member);
154
    }
155
156
    /**
157
     * @param Member
158
     *
159
     * @return boolean
160
     */
161
    public function canDelete($member = null)
162
    {
163
        return $this->canEdit($member);
164
    }
165
}
166