Completed
Push — master ( 12981b...2f1b03 )
by Robbie
14s
created

UserFormValidator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 125
Duplicated Lines 23.2 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 35.82%

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 6
dl 29
loc 125
ccs 24
cts 67
cp 0.3582
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D php() 29 122 16

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
class UserFormValidator extends RequiredFields
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...
5
{
6 1
    public function php($data)
7
    {
8 1
        if (!parent::php($data)) {
9
            return false;
10
        }
11
12
        // Skip unsaved records
13 1
        if (empty($data['ID']) || !is_numeric($data['ID'])) {
14
            return true;
15
        }
16
17 1
        $fields = EditableFormField::get()->filter('ParentID', $data['ID'])->sort('"Sort" ASC');
18
19
        // Current nesting
20 1
        $stack = array();
21 1
        $conditionalStep = false; // Is the current step conditional?
22 1
        foreach ($fields as $field) {
23 1
            if ($field instanceof EditableFormStep) {
24
                // Page at top level, or after another page is ok
25 1
                if (empty($stack) || (count($stack) === 1 && $stack[0] instanceof EditableFormStep)) {
26 1
                    $stack = array($field);
27 1
                    $conditionalStep = $field->EffectiveDisplayRules()->count() > 0;
28 1
                    continue;
29
                }
30
31
                $this->validationError(
32
                    'FormFields',
33
                    _t(
34
                        "UserFormValidator.UNEXPECTED_BREAK",
35
                        "Unexpected page break '{name}' inside nested field '{group}'",
36
                        array(
0 ignored issues
show
Documentation introduced by
array('name' => $field->... end($stack)->CMSTitle) is of type array<string,?,{"name":"?","group":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
                            'name' => $field->CMSTitle,
0 ignored issues
show
Documentation introduced by
The property CMSTitle does not exist on object<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...
38
                            'group' => end($stack)->CMSTitle
39
                        )
40
                    ),
41
                    'error'
42
                );
43
                return false;
44
            }
45
46
            // Validate no pages
47 1
            if (empty($stack)) {
48
                $this->validationError(
49
                    'FormFields',
50
                    _t(
51
                        "UserFormValidator.NO_PAGE",
52
                        "Field '{name}' found before any pages",
53
                        array(
0 ignored issues
show
Documentation introduced by
array('name' => $field->CMSTitle) is of type array<string,?,{"name":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
                            'name' => $field->CMSTitle
55
                        )
56
                    ),
57
                    'error'
58
                );
59
                return false;
60
            }
61
62
            // Nest field group
63 1
            if ($field instanceof EditableFieldGroup) {
64 1
                $stack[] = $field;
65 1
                continue;
66
            }
67
68
            // Unnest field group
69 1
            if ($field instanceof EditableFieldGroupEnd) {
70 1
                $top = end($stack);
71
72
                // Check that the top is a group at all
73 1
                if (!$top instanceof EditableFieldGroup) {
74
                    $this->validationError(
75
                        'FormFields',
76
                        _t(
77
                            "UserFormValidator.UNEXPECTED_GROUP_END",
78
                            "'{name}' found without a matching group",
79
                            array(
0 ignored issues
show
Documentation introduced by
array('name' => $field->CMSTitle) is of type array<string,?,{"name":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
                                'name' => $field->CMSTitle
0 ignored issues
show
Documentation introduced by
The property CMSTitle does not exist on object<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...
81
                            )
82
                        ),
83
                        'error'
84
                    );
85
                    return false;
86
                }
87
88
                // Check that the top is the right group
89 1 View Code Duplication
                if ($top->EndID != $field->ID) {
0 ignored issues
show
Documentation introduced by
The property EndID does not exist on object<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...
90
                    $this->validationError(
91
                        'FormFields',
92
                        _t(
93
                            "UserFormValidator.WRONG_GROUP_END",
94
                            "'{name}' found closes the wrong group '{group}'",
95
                            array(
0 ignored issues
show
Documentation introduced by
array('name' => $field->...oup' => $top->CMSTitle) is of type array<string,?,{"name":"?","group":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
                                'name' => $field->CMSTitle,
0 ignored issues
show
Documentation introduced by
The property CMSTitle does not exist on object<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...
97
                                'group' => $top->CMSTitle
0 ignored issues
show
Documentation introduced by
The property CMSTitle does not exist on object<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...
98
                            )
99
                        ),
100
                        'error'
101
                    );
102
                    return false;
103
                }
104
105
                // Unnest group
106 1
                array_pop($stack);
107
            }
108
109
            // Normal field type
110 1 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...
111
                $this->validationError(
112
                    'FormFields',
113
                    _t(
114
                        "UserFormValidator.CONDITIONAL_REQUIRED",
115
                        "Required field '{name}' cannot be placed within a conditional page",
116
                        array(
0 ignored issues
show
Documentation introduced by
array('name' => $field->CMSTitle) is of type array<string,?,{"name":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
                            'name' => $field->CMSTitle
118
                        )
119
                    ),
120
                    'error'
121
                );
122 1
                return false;
123
            }
124
        }
125
126 1
        return true;
127
    }
128
}
129