Completed
Push — develop ( 35ebd6...f6916d )
by Mohamed
13:11 queued 06:49
created

SelectUser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 83
ccs 14
cts 28
cp 0.5
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 17 1
A createDataList() 0 12 3
A formatSelected() 0 8 1
A getValue() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Form\Former\Fields;
13
14
use Former\Traits\Field;
15
16
/**
17
 * SelectUser is a Former field class for selecting project users.
18
 *
19
 * @author Mohamed Alsharaf <[email protected]>
20
 */
21
class SelectUser extends Field
22
{
23
    /**
24
     * Properties to be injected as attributes.
25
     *
26
     * @var array
27
     */
28
    protected $injectedProperties = [];
29
30
    /**
31
     * Prints out the current tag.
32
     *
33
     * @return string An input tag
34
     */
35 1
    public function render()
36
    {
37 1
        $this->setAttribute('type', 'text');
38 1
        $this->addClass('search-query');
39 1
        $this->setId();
40 1
        $this->removeAttribute('name');
41
42 1
        $name = $this->name;
43
44
        // Render main input
45 1
        $input = parent::render();
46
47
        // Render list of selected users
48 1
        $input .= $this->createDataList('datalist_' . $name);
49
50 1
        return $input;
51
    }
52
53
    /**
54
     * Returns Html of selected users.
55
     *
56
     * @param string $id
57
     *
58
     * @return string
59
     */
60 1
    private function createDataList($id)
61
    {
62 1
        $dataList = '<ul id="' . $id . '" class="datalist ' . $id . '">';
63 1
        if (is_array($this->value)) {
64
            foreach ($this->value as $key => $value) {
65
                $dataList .= $this->formatSelected($key, $value);
66
            }
67
        }
68 1
        $dataList .= '</ul>';
69
70 1
        return $dataList;
71
    }
72
73
    /**
74
     * Returns Html of a selected user row.
75
     *
76
     * @param int    $id
77
     * @param string $name
78
     *
79
     * @return string
80
     */
81
    protected function formatSelected($id, $name)
82
    {
83
        return '<li class="project-user' . $id . '">'
84
        . '<a href="" class="delete">' . trans('tinyissue.remove') . '</a>'
85
        . $name
86
        . '<input type="hidden" name="user[' . $id . ']" value="' . $name . '" />'
87
        . '</li>';
88
    }
89
90
    /**
91
     * Returns an array of selected users.
92
     *
93
     * @return array
94
     */
95
    public function getValue()
96
    {
97
        if (!is_array($this->value)) {
98
            return [];
1 ignored issue
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type of the parent method HtmlObject\Traits\Tag::getValue of type string|null|HtmlObject\Traits\Tag.

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...
99
        }
100
101
        return array_keys($this->value);
1 ignored issue
show
Bug Best Practice introduced by
The return type of return array_keys($this->value); (array<integer|string>) is incompatible with the return type of the parent method HtmlObject\Traits\Tag::getValue of type string|null|HtmlObject\Traits\Tag.

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...
102
    }
103
}
104