Completed
Push — master ( 25a4f1...b90886 )
by Thibaud
10:33
created

SortRequest::extractSortProperty()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Alchemy\RestBundle\Rest\Request;
4
5
use Alchemy\Rest\Request\Sort;
6
use Alchemy\Rest\Request\SortOptions;
7
8
class SortRequest implements SortOptions
9
{
10
    /**
11
     * @var string|array
12
     */
13
    private $sorts;
14
15
    /**
16
     * @var string
17
     */
18
    private $property;
19
20
    /**
21
     * @var string
22
     */
23
    private $direction;
24
25
    /**
26
     * @param $sorts
27
     * @param string|null $property
28
     * @param string|null $direction
29
     */
30 22
    public function __construct($sorts, $property = null, $direction = null)
31
    {
32 22
        $this->sorts = $sorts;
33 22
        $this->property = $property;
34 22
        $this->direction = $direction;
35 22
    }
36
37
    /**
38
     * @param array $sortMap
39
     * @return array
40
     */
41 20
    public function getSorts(array $sortMap = array())
42
    {
43 20
        $sorts = array();
44
45 20
        foreach ($this->normalizeSorts($this->sorts) as $sort) {
0 ignored issues
show
Bug introduced by
The expression $this->normalizeSorts($this->sorts) of type array|string|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
46 18
            $sort = $this->extractSortProperty($sort);
47 18
            $sort = $this->mapSortProperty($sortMap, $sort[0], $sort[1]);
48
49 18
            if ($sort) {
50 18
                $sorts[] = $sort;
51 18
            }
52 20
        }
53
54 20
        return $sorts;
55
    }
56
57
    /**
58
     * @return array|string|null
59
     */
60 20
    private function normalizeSorts($sorts)
61
    {
62 20
        if ($sorts === null && $this->property !== null && $this->direction !== null) {
63 6
            $sorts = array(array($this->property, $this->direction));
64 6
        }
65
66 20
        if (is_string($sorts)) {
67 12
            $sorts = explode(',', $sorts);
68 12
        }
69
70 20
        if ($sorts === null) {
71 2
            $sorts = array();
72 2
        }
73
74 20
        return $sorts;
75
    }
76
77
    /**
78
     * @param $sort
79
     * @return array
80
     */
81 18
    private function extractSortProperty($sort)
82
    {
83 18
        if (is_string($sort)) {
84 12
            $sort = explode(':', $sort);
85 12
        }
86
87 18
        if (count($sort) < 2) {
88 4
            $sort[1] = self::SORT_ASC;
89 4
        }
90
91 18
        return $sort;
92
    }
93
94
    /**
95
     * @param array $sortMap
96
     * @param $sort
97
     * @param $direction
98
     * @return Sort
99
     */
100 18
    private function mapSortProperty(array $sortMap, $sort, $direction)
101
    {
102 18
        if (isset($sortMap[$sort])) {
103 2
            $sort = $sortMap[$sort];
104 2
        }
105
106 18
        return new Sort($sort, $direction);
107
    }
108
}
109