Conditions | 8 |
Paths | 7 |
Total Lines | 73 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
55 | public function index($table, Request $request) |
||
56 | { |
||
57 | $this->_handleValidation($table, "index", $request); |
||
58 | |||
59 | $entity = config("mock.entrypoints.".$table); |
||
60 | |||
61 | if ($entity) { |
||
62 | |||
63 | /** |
||
64 | * @var $data Collection |
||
65 | */ |
||
66 | $data = factory($entity["class"], $entity["number"])->states($entity["states"])->make($entity["override"]); |
||
67 | |||
68 | $data = $data->map(function ($item, $key) { |
||
69 | |||
70 | if (!$item["id"]) { |
||
71 | $item['id'] = $key + 1; |
||
72 | } |
||
73 | |||
74 | return $item; |
||
75 | }); |
||
76 | |||
77 | } else { |
||
78 | |||
79 | $folder = public_path(Mock::getPath() . '/' . $table); |
||
80 | |||
81 | if (is_file(Mock::getPath() . '/' . $table . '.json')) { |
||
82 | $data = json_decode(file_get_contents(Mock::getPath() . '/' . $table . '.json'), true); |
||
83 | return response()->json($data); |
||
84 | } |
||
85 | |||
86 | if (!is_dir($folder)) { |
||
87 | \File::makeDirectory($folder, 0775, true); |
||
88 | } |
||
89 | |||
90 | $files = \File::allFiles(public_path(Mock::getPath() . '/' . $table)); |
||
91 | |||
92 | $data = []; |
||
93 | |||
94 | foreach ($files as $file) { |
||
95 | $item = json_decode(file_get_contents($file->getRealPath()), true); |
||
96 | |||
97 | if (!isset($item["id"])) { |
||
98 | $item["id"] = str_replace(".json", "", $file->getBasename()); |
||
99 | } |
||
100 | |||
101 | $data[] = $item; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | if (config("mock.paginate")) { |
||
106 | // Get current page form url e.x. &page=1 |
||
107 | $currentPage = LengthAwarePaginator::resolveCurrentPage(); |
||
108 | |||
109 | // Create a new Laravel collection from the array data |
||
110 | $itemCollection = collect($data); |
||
111 | |||
112 | // Define how many items we want to be visible in each page |
||
113 | $perPage = config("mock.per_page"); |
||
114 | |||
115 | // Slice the collection to get the items to display in current page |
||
116 | $currentPageItems = $itemCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->values()->all(); |
||
117 | |||
118 | // Create our paginator and pass it to the view |
||
119 | $paginatedItems= new LengthAwarePaginator($currentPageItems , count($itemCollection), $perPage); |
||
120 | |||
121 | // set url path for generted links |
||
122 | $paginatedItems->setPath($request->url()); |
||
123 | |||
124 | return $paginatedItems; |
||
125 | } |
||
126 | |||
127 | return $data; |
||
128 | } |
||
216 |