Completed
Push — master ( 016a0b...25fc98 )
by Franco
12s
created

UserFormValidator::php()   D

Complexity

Conditions 16
Paths 13

Size

Total Lines 122
Code Lines 72

Duplication

Lines 29
Ratio 23.77 %

Importance

Changes 0
Metric Value
dl 29
loc 122
rs 4.8736
c 0
b 0
f 0
cc 16
eloc 72
nc 13
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
namespace SilverStripe\UserForms\Extension;
4
5
use SilverStripe\Forms\RequiredFields;
6
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup;
7
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd;
8
use SilverStripe\UserForms\Model\EditableFormField;
9
use SilverStripe\UserForms\Model\EditableFormField\EditableFormStep;
10
11
class UserFormValidator extends RequiredFields
12
{
13
    public function php($data)
14
    {
15
        if (!parent::php($data)) {
16
            return false;
17
        }
18
19
        // Skip unsaved records
20
        if (empty($data['ID']) || !is_numeric($data['ID'])) {
21
            return true;
22
        }
23
24
        $fields = EditableFormField::get()->filter('ParentID', $data['ID'])->sort('"Sort" ASC');
25
26
        // Current nesting
27
        $stack = array();
28
        $conditionalStep = false; // Is the current step conditional?
29
        foreach ($fields as $field) {
30
            if ($field instanceof EditableFormStep) {
31
                // Page at top level, or after another page is ok
32
                if (empty($stack) || (count($stack) === 1 && $stack[0] instanceof EditableFormStep)) {
33
                    $stack = array($field);
34
                    $conditionalStep = $field->EffectiveDisplayRules()->count() > 0;
35
                    continue;
36
                }
37
38
                $this->validationError(
39
                    'FormFields',
40
                    _t(
41
                        __CLASS__.".UNEXPECTED_BREAK",
42
                        "Unexpected page break '{name}' inside nested field '{group}'",
43
                        array(
44
                            'name' => $field->CMSTitle,
0 ignored issues
show
Documentation introduced by
The property CMSTitle does not exist on object<SilverStripe\User...Field\EditableFormStep>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
                            'group' => end($stack)->CMSTitle
46
                        )
47
                    ),
48
                    'error'
49
                );
50
                return false;
51
            }
52
53
            // Validate no pages
54
            if (empty($stack)) {
55
                $this->validationError(
56
                    'FormFields',
57
                    _t(
58
                        __CLASS__.".NO_PAGE",
59
                        "Field '{name}' found before any pages",
60
                        array(
61
                            'name' => $field->CMSTitle
62
                        )
63
                    ),
64
                    'error'
65
                );
66
                return false;
67
            }
68
69
            // Nest field group
70
            if ($field instanceof EditableFieldGroup) {
71
                $stack[] = $field;
72
                continue;
73
            }
74
75
            // Unnest field group
76
            if ($field instanceof EditableFieldGroupEnd) {
77
                $top = end($stack);
78
79
                // Check that the top is a group at all
80
                if (!$top instanceof EditableFieldGroup) {
81
                    $this->validationError(
82
                        'FormFields',
83
                        _t(
84
                            __CLASS__.".UNEXPECTED_GROUP_END",
85
                            "'{name}' found without a matching group",
86
                            array(
87
                                'name' => $field->CMSTitle
0 ignored issues
show
Documentation introduced by
The property CMSTitle does not exist on object<SilverStripe\User...\EditableFieldGroupEnd>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
88
                            )
89
                        ),
90
                        'error'
91
                    );
92
                    return false;
93
                }
94
95
                // Check that the top is the right group
96 View Code Duplication
                if ($top->EndID != $field->ID) {
0 ignored issues
show
Documentation introduced by
The property EndID does not exist on object<SilverStripe\User...eld\EditableFieldGroup>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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...
97
                    $this->validationError(
98
                        'FormFields',
99
                        _t(
100
                            __CLASS__.".WRONG_GROUP_END",
101
                            "'{name}' found closes the wrong group '{group}'",
102
                            array(
103
                                'name' => $field->CMSTitle,
0 ignored issues
show
Documentation introduced by
The property CMSTitle does not exist on object<SilverStripe\User...\EditableFieldGroupEnd>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
                                'group' => $top->CMSTitle
0 ignored issues
show
Documentation introduced by
The property CMSTitle does not exist on object<SilverStripe\User...eld\EditableFieldGroup>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
105
                            )
106
                        ),
107
                        'error'
108
                    );
109
                    return false;
110
                }
111
112
                // Unnest group
113
                array_pop($stack);
114
            }
115
116
            // Normal field type
117 View Code Duplication
            if ($conditionalStep && $field->Required) {
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...
118
                $this->validationError(
119
                    'FormFields',
120
                    _t(
121
                        __CLASS__.".CONDITIONAL_REQUIRED",
122
                        "Required field '{name}' cannot be placed within a conditional page",
123
                        array(
124
                            'name' => $field->CMSTitle
125
                        )
126
                    ),
127
                    'error'
128
                );
129
                return false;
130
            }
131
        }
132
133
        return true;
134
    }
135
}
136