ListSorter   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 22
lcom 1
cbo 3
dl 0
loc 159
ccs 55
cts 55
cp 1
rs 10
c 2
b 1
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getSortByKey() 0 8 2
A setSortByKey() 0 4 1
A getSortDirKey() 0 8 2
A setSortDirKey() 0 4 1
A findSortableItem() 0 10 3
A getSelectedSortableItem() 0 19 3
A getDefaultSortBy() 0 4 1
A setDefaultSortBy() 0 8 2
A setDefaultSortDir() 0 4 1
A getDefaultSortDir() 0 4 1
A getSortBy() 0 9 2
A getSortDir() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ListSorter;
6
7
class ListSorter extends AbstractListSorter
8
{
9
    const DEFAULT_SORT_BY_KEY = 'by';
10
    const DEFAULT_SORT_DIR_KEY = 'dir';
11
12
    private $sortByKey;
13
    private $sortDirKey;
14
    private $defaultSortBy;
15
    private $defaultSortDir;
16
17
    /**
18
     * @return string
19
     */
20 6
    public function getSortByKey()
21
    {
22 6
        if (!isset($this->sortByKey)) {
23 6
            return self::DEFAULT_SORT_BY_KEY;
24
        }
25
26 1
        return $this->sortByKey;
27
    }
28
29
    /**
30
     * @param string $sortByKey
31
     */
32 1
    public function setSortByKey($sortByKey)
33
    {
34 1
        $this->sortByKey = $sortByKey;
35 1
    }
36
37
    /**
38
     * @return string
39
     */
40 4
    public function getSortDirKey()
41
    {
42 4
        if (!isset($this->sortDirKey)) {
43 4
            return self::DEFAULT_SORT_DIR_KEY;
44
        }
45
46 1
        return $this->sortDirKey;
47
    }
48
49
    /**
50
     * @param string $sortDirKey
51
     */
52 1
    public function setSortDirKey($sortDirKey)
53
    {
54 1
        $this->sortDirKey = $sortDirKey;
55 1
    }
56
57
    /**
58
     * @param $key
59
     *
60
     * @return bool|SortableItem
61
     */
62 6
    private function findSortableItem($key)
63
    {
64 6
        foreach ($this->getSortableItems() as $sortableItem) {
65 6
            if ($sortableItem->getKey() === $key) {
66 6
                return $sortableItem;
67
            }
68
        }
69
70 4
        return false;
71
    }
72
73
    /**
74
     * @return bool|SortableItem|void
75
     */
76 5
    public function getSelectedSortableItem()
77
    {
78 5
        $key = $this->getRequest()->input($this->getSortByKey());
79 5
        $sortableItem = $this->findSortableItem($key);
80
81 5
        if (empty($sortableItem)) {
82 3
            $sortableItem = $this->findSortableItem($this->getDefaultSortBy());
83
        }
84
85 5
        if (!$sortableItem instanceof SortableItem) {
86 3
            return;
87
        }
88
89 3
        $dir = $this->getRequest()->input($this->getSortDirKey());
90 3
        $sortableItem->setSortDir($dir);
0 ignored issues
show
Bug introduced by
It seems like $dir defined by $this->getRequest()->inp...$this->getSortDirKey()) on line 89 can also be of type array; however, ListSorter\SortableItem::setSortDir() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
91 3
        $sortableItem->setIsSelected(true);
92
93 3
        return $sortableItem;
94
    }
95
96
    /**
97
     * @return string
98
     */
99 4
    public function getDefaultSortBy()
100
    {
101 4
        return $this->defaultSortBy;
102
    }
103
104
    /**
105
     * @param $key
106
     *
107
     * @throws \Exception
108
     */
109 2
    public function setDefaultSortBy($key)
110
    {
111 2
        if (!$this->findSortableItem($key) instanceof SortableItem) {
112 1
            throw new \Exception('Invalid sortable item key : '.$key);
113
        }
114
115 2
        $this->defaultSortBy = $key;
116 2
    }
117
118
    /**
119
     * @param $dir
120
     */
121 2
    public function setDefaultSortDir($dir)
122
    {
123 2
        $this->defaultSortDir = $dir;
124 2
    }
125
126
    /**
127
     * @return string
128
     */
129 2
    public function getDefaultSortDir()
130
    {
131 2
        return $this->defaultSortDir;
132
    }
133
134
    /**
135
     * @return string|void
136
     */
137 2
    public function getSortBy()
138
    {
139 2
        $selectedSortableItem = $this->getSelectedSortableItem();
140 2
        if (!$selectedSortableItem instanceof SortableItem) {
141 1
            return;
142
        }
143
144 1
        return $selectedSortableItem->getSortBy();
145
    }
146
147
    /**
148
     * @return string|void
149
     */
150 2
    public function getSortDir()
151
    {
152 2
        $selectedSortableItem = $this->getSelectedSortableItem();
153 2
        if (!$selectedSortableItem instanceof SortableItem) {
154 1
            return;
155
        }
156
157 1
        $sortDir = $selectedSortableItem->getSortDir();
158
159 1
        if (empty($sortDir)) {
160 1
            return $this->getDefaultSortDir();
161
        }
162
163 1
        return $sortDir;
164
    }
165
}
166