Completed
Pull Request — master (#647)
by Robbie
20:35 queued 18:30
created

UserFormsCompositeField::setParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\UserForms\FormField;
4
5
use SilverStripe\Forms\CompositeField;
6
use SilverStripe\UserForms\Model\EditableFormField;
7
use SilverStripe\UserForms\Model\EditableFormField\EditableFormStep;
8
9
/**
10
 * Represents a composite field group, which may contain other groups
11
 */
12
abstract class UserFormsCompositeField extends CompositeField implements UserFormsFieldContainer
13
{
14
    /**
15
     * Parent field
16
     *
17
     * @var UserFormsFieldContainer
18
     */
19
    protected $parent = null;
20
21
    public function getParent()
22
    {
23
        return $this->parent;
24
    }
25
26
    public function setParent(UserFormsFieldContainer $parent)
27
    {
28
        $this->parent = $parent;
29
        return $this;
30
    }
31
32
    public function processNext(EditableFormField $field)
33
    {
34
        // When we find a step, bubble up to the top
35
        if ($field instanceof EditableFormStep) {
36
            return $this->getParent()->processNext($field);
37
        }
38
39
        // Skip over fields that don't generate formfields
40
        if (get_class($field) === EditableFormField::class || !$field->getFormField()) {
41
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (SilverStripe\UserForms\F...UserFormsCompositeField) 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...
42
        }
43
        /** @var EditableFormField $formField */
44
        $formField = $field->getFormField();
45
46
        // Save this field
47
        $this->push($formField);
0 ignored issues
show
Documentation introduced by
$formField is of type object<SilverStripe\User...odel\EditableFormField>, but the function expects a object<SilverStripe\Forms\FormField>.

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...
48
49
        // Nest fields that are containers
50
        if ($formField instanceof UserFormsFieldContainer) {
51
            return $formField->setParent($this);
52
        }
53
54
        // Add any subsequent fields to this
55
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (SilverStripe\UserForms\F...UserFormsCompositeField) 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...
56
    }
57
}
58