Passed
Push — master ( 1c101f...c673c5 )
by Matthew
11:42
created

AbstractGridSource::getDefaultSort()   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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Dtc\GridBundle\Grid\Source;
4
5
use Dtc\GridBundle\Grid\Column\AbstractGridColumn;
6
use Dtc\GridBundle\Grid\Pager\GridSourcePager;
7
use Symfony\Component\HttpFoundation\Request;
8
9
abstract class AbstractGridSource implements GridSourceInterface
10
{
11
    protected $limit = 25;
12
    protected $offset = 0;
13
    protected $filter = array();
14
    protected $orderBy = array();
15
    protected $pager = array();
16
    protected $id = 'grid';
17
    protected $columns;
18
    protected $parameters;
19
20
    public function bind(Request $request)
21
    {
22
        // Change limit, offset
23
        if ($limit = $request->get('limit')) {
24
            $this->limit = $limit;
25
        }
26
27
        if ($page = $request->get('page')) {
28
            $this->offset = $this->limit * ($page - 1);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->limit * ($page - 1) can also be of type double. However, the property $offset is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
29
        }
30
31
        if ($offset = $request->get('offset')) {
32
            $this->offset = $offset;
33
        }
34
35
        if ($filter = $request->get('filter')) {
36
            $this->filter = $filter;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filter of type * is incompatible with the declared type array of property $filter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        }
38
39
        if ($sortColumn = $request->get('sort_column')) {
40
            $sortOrder = $request->get('sort_order');
41
            $sortOrder = strtoupper($sortOrder);
42
43
            $this->orderBy[$sortColumn] = $sortOrder;
44
        }
45
46
        if ($orderBy = $request->get('order')) {
47
            $this->orderBy = $orderBy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $orderBy of type * is incompatible with the declared type array of property $orderBy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
        }
49
    }
50
51
    public function getDefaultSort()
52
    {
53
        return null;
54
    }
55
56
    public function getId()
57
    {
58
        return $this->id;
59
    }
60
61
    private $divId = null;
62
63
    public function getDivId()
64
    {
65
        if (!$this->divId) {
66
            $this->divId = preg_replace('/[^a-zA-Z0-9\-]/', '', $this->id);
67
        }
68
69
        return $this->divId;
70
    }
71
72
    public function setId($value)
73
    {
74
        $this->id = $value;
75
    }
76
77
    public function getColumns()
78
    {
79
        return $this->columns;
80
    }
81
82
    public function setColumns($columns)
83
    {
84
        /** @var AbstractGridColumn $col */
85
        foreach ($columns as $col) {
86
            $this->columns[$col->getField()] = $col;
87
        }
88
    }
89
90
    public function removeColumn($field)
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
    {
92
        $this->removeColumns(func_get_args());
93
    }
94
95
    public function selectColumns(array $fields)
96
    {
97
        $selectedCols = array();
98
        foreach ($fields as $field) {
99
            if (isset($this->columns[$field])) {
100
                $selectedCols[$field] = $this->columns[$field];
101
            }
102
        }
103
104
        $this->columns = $selectedCols;
105
    }
106
107
    public function removeColumns(array $fields)
108
    {
109
        foreach ($fields as $field) {
110
            unset($this->columns[$field]);
111
        }
112
    }
113
114
    public function getPager()
115
    {
116
        if (!$this->pager) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->pager of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
117
            $this->pager = new GridSourcePager($this);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Dtc\GridBundle\Grid...\GridSourcePager($this) of type object<Dtc\GridBundle\Grid\Pager\GridSourcePager> is incompatible with the declared type array of property $pager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
118
        }
119
120
        return $this->pager;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->pager; of type Dtc\GridBundle\Grid\Pager\GridSourcePager|array adds the type array to the return on line 120 which is incompatible with the return type declared by the interface Dtc\GridBundle\Grid\Sour...urceInterface::getPager of type Dtc\GridBundle\Grid\Pager\GridSourcePager.
Loading history...
121
    }
122
123
    /**
124
     * @return $limit
0 ignored issues
show
Documentation introduced by
The doc-type $limit could not be parsed: Unknown type name "$limit" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
125
     */
126
    public function getLimit()
127
    {
128
        return $this->limit;
129
    }
130
131
    /**
132
     * @param int $limit
133
     */
134
    public function setLimit($limit)
135
    {
136
        $this->limit = $limit;
137
    }
138
139
    /**
140
     * @return $offset
0 ignored issues
show
Documentation introduced by
The doc-type $offset could not be parsed: Unknown type name "$offset" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
141
     */
142
    public function getOffset()
143
    {
144
        return $this->offset;
145
    }
146
147
    /**
148
     * @param int $offset
149
     */
150
    public function setOffset($offset)
151
    {
152
        $this->offset = $offset;
153
    }
154
155
    /**
156
     * @return $filter
0 ignored issues
show
Documentation introduced by
The doc-type $filter could not be parsed: Unknown type name "$filter" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
157
     */
158
    public function getFilter()
159
    {
160
        return $this->filter;
161
    }
162
163
    /**
164
     * @param $filter
165
     */
166
    public function setFilter($filter)
167
    {
168
        $this->filter = $filter;
169
    }
170
171
    /**
172
     * @return $orderBy
0 ignored issues
show
Documentation introduced by
The doc-type $orderBy could not be parsed: Unknown type name "$orderBy" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
173
     */
174
    public function getOrderBy()
175
    {
176
        return $this->orderBy;
177
    }
178
179
    /**
180
     * @param $orderBy
181
     */
182
    public function setOrderBy($orderBy)
183
    {
184
        $this->orderBy = $orderBy;
185
    }
186
187
    public function getLastModified()
188
    {
189
        return null;
190
    }
191
192
    public function getParameter($key)
193
    {
194
        return $this->parameters[$key];
195
    }
196
197
    public function setParameter($key, $value)
198
    {
199
        $this->parameters[$key] = $value;
200
    }
201
202
    /**
203
     * @return $parameters
0 ignored issues
show
Documentation introduced by
The doc-type $parameters could not be parsed: Unknown type name "$parameters" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
204
     */
205
    public function getParameters()
206
    {
207
        return $this->parameters;
208
    }
209
210
    /**
211
     * @param $parameters
212
     */
213
    public function setParameters($parameters)
214
    {
215
        $this->parameters = $parameters;
216
    }
217
218
    /**
219
     * @return bool Returns true if this GridSource has an ID or false otherise
220
     */
221
    public function hasIdColumn()
222
    {
223
        return false;
224
    }
225
226
    /**
227
     * @param $id
228
     *
229
     * @return null|mixed returns the row identified by Id if found
230
     */
231
    public function find($id)
232
    {
233
        return null;
234
    }
235
}
236