Completed
Push — master ( 1386f1...75bec8 )
by Guy
14s queued 10s
created

Validator::php()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 44
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 23
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 44
rs 7.6666

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
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Forms\RequiredFields;
6
use SilverStripe\UserForms\Model\EditableCustomRule;
7
8
class Validator extends RequiredFields
9
{
10
    /**
11
     *
12
     * @var EditableFormField
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...Field\EditableFormField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
     */
14
    protected $record = null;
15
16
    /**
17
     *
18
     * @param EditableFormField $record
19
     * @return $this
20
     */
21
    public function setRecord($record)
22
    {
23
        $this->record = $record;
24
        return $this;
25
    }
26
27
    /*
28
     * @return EditableFormField
29
     */
30
    public function getRecord()
31
    {
32
        return $this->record;
33
    }
34
35
36
    public function php($data)
37
    {
38
        if (!parent::php($data)) {
39
            return false;
40
        }
41
42
        // When the record is unsaved and the classname is not set throw an error
43
        if ((!$this->record || !$this->record->exists()) && (!isset($data['ClassName']) || empty($data['ClassName']))) {
44
            $this->validationError(
45
                'ClassName',
46
                _t(
47
                    __CLASS__ . 'CLASSNAME_ERROR',
48
                    'You need to select a field type before you can create the field'
49
                )
50
            );
51
            return false;
52
        }
53
54
        // Skip unsaved records
55
        if (!$this->record || !$this->record->exists()) {
56
            return true;
57
        }
58
59
        // Skip validation if not required
60
        if (empty($data['Required'])) {
61
            return;
62
        }
63
64
        // Skip validation if no rules
65
        $count = EditableCustomRule::get()->filter('ParentID', $this->record->ID)->count();
66
        if ($count == 0) {
67
            return true;
68
        }
69
70
        // Both required = true and rules > 0 should error
71
        $this->validationError(
72
            'Required_Error',
73
            _t(
74
                __CLASS__.'.REQUIRED_ERROR',
75
                'Form fields cannot be required and have conditional display rules.'
76
            ),
77
            'error'
78
        );
79
        return false;
80
    }
81
}
82