Conditions | 10 |
Paths | 32 |
Total Lines | 47 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
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 |
||
32 | public function paginate($data, $currentPage = 1, $limitPerPage = 10, $options = array()) |
||
33 | { |
||
34 | $paginator = new KnpPaginator(); |
||
35 | |||
36 | if ($currentPage === null) { |
||
37 | $currentPage = 1; |
||
38 | } |
||
39 | |||
40 | if (!isset($options['searchParameter'])) { |
||
41 | $options['searchParameter'] = 'search'; |
||
42 | } |
||
43 | |||
44 | // Temporary solution. We'll try to figure out a better one soon! |
||
45 | $searchFields = isset($options['searchFields']) |
||
46 | ? $options['searchFields'] |
||
47 | : false |
||
48 | ; |
||
49 | |||
50 | $searchValue = $this->app['request']->query->get( |
||
51 | $options['searchParameter'], |
||
52 | false |
||
53 | ); |
||
54 | |||
55 | if ($searchFields && !($data instanceof QueryBuilder)) { |
||
56 | throw new \Exception('If you want to use search, you MUST use the QueryBuilder!'); |
||
57 | } |
||
58 | |||
59 | if ($searchFields && $searchValue) { |
||
60 | if (is_string($searchFields)) { |
||
61 | $searchFields = explode(',', $searchFields); |
||
62 | } |
||
63 | |||
64 | foreach ($searchFields as $searchFieldKey => $searchField) { |
||
65 | $data |
||
66 | ->orWhere($searchField.' LIKE ?'.$searchFieldKey) |
||
67 | ->setParameter($searchFieldKey, '%'.$searchValue.'%') |
||
68 | ; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return $paginator->paginate( |
||
73 | $data, |
||
74 | $currentPage, |
||
75 | $limitPerPage, |
||
76 | $options |
||
77 | ); |
||
78 | } |
||
79 | } |
||
80 |