| Conditions | 12 |
| Paths | 36 |
| Total Lines | 53 |
| Lines | 13 |
| Ratio | 24.53 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | public function bindRequest(Request $request) |
||
| 20 | { |
||
| 21 | $query = $request->query; |
||
| 22 | $session = $request->getSession(); |
||
| 23 | |||
| 24 | $adminListName = 'listconfig_' . $request->get('_route'); |
||
| 25 | |||
| 26 | $this->page = $request->query->getInt('page', 1); |
||
|
|
|||
| 27 | $this->limit = $request->query->getInt('limit', $this->getLimitOptions()[0]); |
||
| 28 | |||
| 29 | $adminListSessionData = $request->getSession()->get($adminListName); |
||
| 30 | if (!$query->has('limit') && null !== $adminListSessionData && isset($adminListSessionData['limit'])) { |
||
| 31 | $this->limit = $adminListSessionData['limit']; |
||
| 32 | } |
||
| 33 | |||
| 34 | if ($request->query->has('limit') && !$request->query->has('page')) { |
||
| 35 | $this->page = 1; |
||
| 36 | } |
||
| 37 | |||
| 38 | // Allow alphanumeric, _ & . in order by parameter! |
||
| 39 | $this->orderBy = preg_replace('/[^[a-zA-Z0-9\_\.]]/', '', $request->query->get('orderBy', '')); |
||
| 40 | $this->orderDirection = $request->query->getAlpha('orderDirection', ''); |
||
| 41 | |||
| 42 | // there is a session and the filter param is not set |
||
| 43 | View Code Duplication | if ($session->has($adminListName) && !$query->has('filter')) { |
|
| 44 | if (!$query->has('page') && !$query->has('limit')) { |
||
| 45 | $this->page = $adminListSessionData['page']; |
||
| 46 | } |
||
| 47 | |||
| 48 | if (!$query->has('orderBy')) { |
||
| 49 | $this->orderBy = $adminListSessionData['orderBy']; |
||
| 50 | } |
||
| 51 | |||
| 52 | if (!$query->has('orderDirection')) { |
||
| 53 | $this->orderDirection = $adminListSessionData['orderDirection']; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | // save current parameters |
||
| 58 | $session->set( |
||
| 59 | $adminListName, |
||
| 60 | [ |
||
| 61 | 'page' => $this->page, |
||
| 62 | 'limit' => $this->limit, |
||
| 63 | 'orderBy' => $this->orderBy, |
||
| 64 | 'orderDirection' => $this->orderDirection, |
||
| 65 | ] |
||
| 66 | ); |
||
| 67 | |||
| 68 | // Remove limit from query param so it doesn't affect the session of the filter builder |
||
| 69 | $request->query->remove('limit'); |
||
| 70 | $this->getFilterBuilder()->bindRequest($request); |
||
| 71 | } |
||
| 72 | |||
| 92 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: