Completed
Pull Request — master (#647)
by Robbie
05:30 queued 03:19
created

UserFormsFieldList   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 41
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processNext() 0 15 3
A getParent() 0 5 1
A setParent() 0 4 1
A clearEmptySteps() 0 8 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) {
17
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (SilverStripe\UserForms\F...ield\UserFormsFieldList) is incompatible with the return type declared by the interface SilverStripe\UserForms\F...dContainer::processNext of type SilverStripe\UserForms\F...\EditableContainerField.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
18
        }
19
20
        $this->push($formField);
21
22
        if ($formField instanceof UserFormsFieldContainer) {
23
            return $formField->setParent($this);
24
        }
25
26
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (SilverStripe\UserForms\F...ield\UserFormsFieldList) is incompatible with the return type declared by the interface SilverStripe\UserForms\F...dContainer::processNext of type SilverStripe\UserForms\F...\EditableContainerField.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

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