UserFormsFieldList   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getParent() 0 4 1
A setParent() 0 3 1
A processNext() 0 14 3
A clearEmptySteps() 0 5 4
1
<?php
2
3
namespace SilverStripe\UserForms\FormField;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\UserForms\Model\EditableFormField;
7
8
/**
9
 * A list of formfields which allows for iterative processing of nested composite fields
10
 */
11
class UserFormsFieldList extends FieldList implements UserFormsFieldContainer
12
{
13
    public function processNext(EditableFormField $field)
14
    {
15
        $formField = $field->getFormField();
16
        if (!$formField) {
0 ignored issues
show
introduced by
$formField is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
17
            return $this;
18
        }
19
20
        $this->push($formField);
21
22
        if ($formField instanceof UserFormsFieldContainer) {
23
            return $formField->setParent($this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $formField->setParent($this) also could return the type SilverStripe\Forms\FormF...UserFormsFieldContainer which is incompatible with the return type mandated by SilverStripe\UserForms\F...ontainer::processNext() of SilverStripe\UserForms\F...\EditableContainerField.
Loading history...
24
        }
25
26
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type SilverStripe\UserForms\F...ield\UserFormsFieldList which is incompatible with the return type mandated by SilverStripe\UserForms\F...ontainer::processNext() of SilverStripe\UserForms\F...\EditableContainerField.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
27
    }
28
29
    public function getParent()
30
    {
31
        // Field list does not have a parent
32
        return null;
33
    }
34
35
    public function setParent(UserFormsFieldContainer $parent)
36
    {
37
        return $this;
38
    }
39
40
    /**
41
     * Remove all empty steps
42
     */
43
    public function clearEmptySteps()
44
    {
45
        foreach ($this as $field) {
46
            if ($field instanceof UserFormsStepField && count($field->getChildren()) === 0) {
47
                $this->remove($field);
48
            }
49
        }
50
    }
51
}
52