Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
9 | class EloquentDataProvider extends DataProvider |
||
10 | { |
||
11 | protected $collection; |
||
12 | |||
13 | protected $paginator; |
||
14 | |||
15 | /** @var $iterator \ArrayIterator */ |
||
16 | protected $iterator; |
||
17 | |||
18 | /** |
||
19 | * Constructor. |
||
20 | * |
||
21 | * @param Builder $src |
||
22 | */ |
||
23 | public function __construct(Builder $src) |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | public function reset() |
||
32 | { |
||
33 | $this->getIterator()->rewind(); |
||
34 | return $this; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | public function getCollection() |
||
41 | { |
||
42 | if (!$this->collection) { |
||
43 | $paginator = $this->getPaginator(); |
||
44 | if (version_compare(Application::VERSION, '5', '<')) { |
||
45 | $this->collection = $paginator->getCollection(); |
||
46 | } else { |
||
47 | $this->collection = Collection::make( |
||
48 | $this->getPaginator()->items() |
||
49 | ); |
||
50 | } |
||
51 | } |
||
52 | return $this->collection; |
||
53 | } |
||
54 | |||
55 | public function getPaginator() |
||
56 | { |
||
57 | if (!$this->paginator) { |
||
58 | $this->paginator = $this->src->paginate($this->page_size); |
||
59 | } |
||
60 | return $this->paginator; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return \Illuminate\Pagination\Factory |
||
65 | */ |
||
66 | public function getPaginationFactory() |
||
70 | |||
71 | protected function getIterator() |
||
72 | { |
||
73 | if (!$this->iterator) { |
||
74 | $this->iterator = $this->getCollection()->getIterator(); |
||
75 | } |
||
76 | return $this->iterator; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return Builder |
||
81 | */ |
||
82 | public function getBuilder() |
||
86 | |||
87 | View Code Duplication | public function getRow() |
|
|
|||
88 | { |
||
89 | if ($this->index < $this->count()) { |
||
90 | $this->index++; |
||
91 | $item = $this->iterator->current(); |
||
92 | $this->iterator->next(); |
||
93 | $row = new EloquentDataRow($item, $this->getRowId()); |
||
94 | Event::fire(self::EVENT_FETCH_ROW, [$row, $this]); |
||
95 | return $row; |
||
96 | } else { |
||
97 | return null; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function count() |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function orderBy($fieldName, $direction) |
||
113 | { |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | View Code Duplication | public function filter($fieldName, $operator, $value) |
|
153 | } |
||
154 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.