Conditions | 10 |
Paths | 2 |
Total Lines | 81 |
Code Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
44 | public function edit(Request $request) |
||
45 | { |
||
46 | $this->setAdminPrefs(); |
||
47 | $regexes = new Regexes(['Settings' => null, 'Table_Name' => 'category_regexes']); |
||
48 | |||
49 | // Set the current action. |
||
50 | $action = $request->input('action') ?? 'view'; |
||
51 | |||
52 | $regex = [ |
||
53 | 'id' => '', |
||
54 | 'group_regex' => '', |
||
55 | 'regex' => '', |
||
56 | 'description' => '', |
||
57 | 'ordinal' => '', |
||
58 | 'categories_id' => '', |
||
59 | 'status' => 1, ]; |
||
60 | |||
61 | $this->smarty->assign('regex', $regex); |
||
62 | |||
63 | switch ($action) { |
||
64 | case 'submit': |
||
65 | if (empty($request->input('group_regex'))) { |
||
66 | $this->smarty->assign('error', 'Group regex must not be empty!'); |
||
67 | break; |
||
68 | } |
||
69 | |||
70 | if (empty($request->input('regex'))) { |
||
71 | $this->smarty->assign('error', 'Regex cannot be empty'); |
||
72 | break; |
||
73 | } |
||
74 | |||
75 | if (! is_numeric($request->input('ordinal')) || $request->input('ordinal') < 0) { |
||
76 | $this->smarty->assign('error', 'Ordinal must be a number, 0 or higher.'); |
||
77 | break; |
||
78 | } |
||
79 | |||
80 | if (empty($request->input('id'))) { |
||
81 | $regexes->addRegex($request->all()); |
||
82 | } else { |
||
83 | $regexes->updateRegex($request->all()); |
||
84 | } |
||
85 | |||
86 | return redirect()->to('admin/category_regexes-list'); |
||
87 | break; |
||
|
|||
88 | |||
89 | case 'view': |
||
90 | default: |
||
91 | if ($request->has('id')) { |
||
92 | $meta_title = $title = 'Category Regex Edit'; |
||
93 | $id = $request->input('id'); |
||
94 | $regex = $regexes->getRegexByID($id); |
||
95 | } else { |
||
96 | $meta_title = $title = 'Category Regex Add'; |
||
97 | } |
||
98 | $this->smarty->assign('regex', $regex); |
||
99 | break; |
||
100 | } |
||
101 | |||
102 | $this->smarty->assign('status_ids', [Category::STATUS_ACTIVE, Category::STATUS_INACTIVE]); |
||
103 | $this->smarty->assign('status_names', ['Yes', 'No']); |
||
104 | |||
105 | $categories_db = Category::query() |
||
106 | ->select(['c.id', 'c.title', 'cp.title as parent_title']) |
||
107 | ->from('categories as c') |
||
108 | ->join('root_categories as cp', 'c.root_categories_id', '=', 'cp.id') |
||
109 | ->whereNotNull('c.root_categories_id') |
||
110 | ->orderBy('c.id') |
||
111 | ->get(); |
||
112 | // Build arrays for Smarty html_options helper. Previously only last row was kept. |
||
113 | $category_ids = []; |
||
114 | $category_names = []; |
||
115 | foreach ($categories_db as $category_db) { |
||
116 | $category_ids[] = $category_db->id; |
||
117 | $category_names[] = $category_db->parent_title.' '.$category_db->title.': '.$category_db->id; |
||
118 | } |
||
119 | $this->smarty->assign('category_names', $category_names); |
||
120 | $this->smarty->assign('category_ids', $category_ids); |
||
121 | |||
122 | $content = $this->smarty->fetch('category_regexes-edit.tpl'); |
||
123 | $this->smarty->assign(compact('title', 'meta_title', 'content')); |
||
124 | $this->adminrender(); |
||
125 | } |
||
127 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.