Completed
Push — master ( 016a0b...25fc98 )
by Franco
12s
created

EditableFieldGroupEnd::getInlineTitleField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Forms\HiddenField;
6
use SilverStripe\Forms\LabelField;
7
use SilverStripe\Security\Group;
8
use SilverStripe\UserForms\Model\EditableFormField;
9
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup;
10
11
/**
12
 * Specifies that this ends a group of fields
13
 */
14
class EditableFieldGroupEnd extends EditableFormField
15
{
16
    private static $belongs_to = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $belongs_to is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
        'Group' => EditableFieldGroup::class
18
    ];
19
20
    /**
21
     * Disable selection of group class
22
     *
23
     * @config
24
     * @var bool
25
     */
26
    private static $hidden = true;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $hidden is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
27
28
    /**
29
     * Non-data type
30
     *
31
     * @config
32
     * @var bool
33
     */
34
    private static $literal = true;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $literal is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    private static $table_name = 'EditableFieldGroupEnd';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $table_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
38
    public function getCMSTitle()
39
    {
40
        $group = $this->Group();
0 ignored issues
show
Documentation Bug introduced by
The method Group does not exist on object<SilverStripe\User...\EditableFieldGroupEnd>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
41
        return _t(
42
            __CLASS__.'.FIELD_GROUP_END',
43
            '{group} end',
44
            [
45
                'group' => ($group && $group->exists()) ? $group->CMSTitle : Group::class
46
            ]
47
        );
48
    }
49
50
    public function getCMSFields()
51
    {
52
        $fields = parent::getCMSFields();
53
        $fields->removeByName(['MergeField', 'Default', 'Validation', 'DisplayRules']);
54
        return $fields;
55
    }
56
57
    public function getInlineClassnameField($column, $fieldClasses)
58
    {
59
        return LabelField::create($column, $this->CMSTitle);
0 ignored issues
show
Documentation introduced by
The property CMSTitle does not exist on object<SilverStripe\User...\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...
Bug Best Practice introduced by
The return type of return \SilverStripe\For...lumn, $this->CMSTitle); (SilverStripe\Forms\LabelField) is incompatible with the return type of the parent method SilverStripe\UserForms\M...getInlineClassnameField of type SilverStripe\Forms\DropdownField.

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...
60
    }
61
62
    public function getInlineTitleField($column)
63
    {
64
        return HiddenField::create($column);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \SilverStripe\For...Field::create($column); (SilverStripe\Forms\HiddenField) is incompatible with the return type of the parent method SilverStripe\UserForms\M...ld::getInlineTitleField of type SilverStripe\Forms\TextField.

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...
65
    }
66
67
    public function getFormField()
68
    {
69
        return null;
70
    }
71
72
    public function showInReports()
73
    {
74
        return false;
75
    }
76
77
    public function onAfterWrite()
78
    {
79
        parent::onAfterWrite();
80
81
        // If this is not attached to a group, find the first group prior to this
82
        // with no end attached
83
        $group = $this->Group();
0 ignored issues
show
Documentation Bug introduced by
The method Group does not exist on object<SilverStripe\User...\EditableFieldGroupEnd>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
84
        if (!($group && $group->exists()) && $this->ParentID) {
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<SilverStripe\User...\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...
85
            $group = EditableFieldGroup::get()
86
                ->filter([
87
                    'ParentID' => $this->ParentID,
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<SilverStripe\User...\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...
88
                    'Sort:LessThanOrEqual' => $this->Sort
89
                ])
90
                ->where('"EditableFieldGroup"."EndID" IS NULL OR "EditableFieldGroup"."EndID" = 0')
91
                ->sort('"Sort" DESC')
92
                ->first();
93
94
            // When a group is found, attach it to this end
95
            if ($group) {
96
                $group->EndID = $this->ID;
97
                $group->write();
98
            }
99
        }
100
    }
101
}
102