Conditions | 36 |
Paths | 13839 |
Total Lines | 131 |
Code Lines | 77 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
113 | public function setPrivileges( |
||
114 | $mode, |
||
115 | $type, |
||
116 | $object, |
||
117 | $public, |
||
118 | $usernames, |
||
119 | $groupnames, |
||
120 | $privileges, |
||
121 | $grantoption, |
||
122 | $cascade, |
||
123 | $table |
||
124 | ) { |
||
125 | $f_schema = $this->_schema; |
||
126 | $this->fieldClean($f_schema); |
||
127 | $this->fieldArrayClean($usernames); |
||
128 | $this->fieldArrayClean($groupnames); |
||
129 | |||
130 | // Input checking |
||
131 | if (!\is_array($privileges) || 0 === \count($privileges)) { |
||
132 | return -3; |
||
133 | } |
||
134 | |||
135 | if (!\is_array($usernames) || !\is_array($groupnames) || |
||
136 | (!$public && 0 === \count($usernames) && 0 === \count($groupnames))) { |
||
137 | return -4; |
||
138 | } |
||
139 | |||
140 | if ('GRANT' !== $mode && 'REVOKE' !== $mode) { |
||
141 | return -5; |
||
142 | } |
||
143 | |||
144 | $sql = $mode; |
||
145 | |||
146 | // Grant option |
||
147 | if ($this->hasGrantOption() && 'REVOKE' === $mode && $grantoption) { |
||
148 | $sql .= ' GRANT OPTION FOR'; |
||
149 | } |
||
150 | |||
151 | if (\in_array('ALL PRIVILEGES', $privileges, true)) { |
||
152 | $sql .= ' ALL PRIVILEGES'; |
||
153 | } else { |
||
154 | if ('column' === $type) { |
||
155 | $this->fieldClean($object); |
||
156 | $sql .= ' ' . \implode(" (\"{$object}\"), ", $privileges); |
||
157 | } else { |
||
158 | $sql .= ' ' . \implode(', ', $privileges); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | switch ($type) { |
||
163 | case 'column': |
||
164 | $sql .= " (\"{$object}\")"; |
||
165 | $object = $table; |
||
166 | // no break |
||
167 | case 'table': |
||
168 | case 'view': |
||
169 | case 'sequence': |
||
170 | $this->fieldClean($object); |
||
171 | $sql .= " ON \"{$f_schema}\".\"{$object}\""; |
||
172 | |||
173 | break; |
||
174 | case 'database': |
||
175 | $this->fieldClean($object); |
||
176 | $sql .= " ON DATABASE \"{$object}\""; |
||
177 | |||
178 | break; |
||
179 | case 'function': |
||
180 | // Function comes in with $object as function OID |
||
181 | $fn = $this->getFunction($object); |
||
182 | $this->fieldClean($fn->fields['proname']); |
||
183 | $sql .= " ON FUNCTION \"{$f_schema}\".\"{$fn->fields['proname']}\"({$fn->fields['proarguments']})"; |
||
184 | |||
185 | break; |
||
186 | case 'language': |
||
187 | $this->fieldClean($object); |
||
188 | $sql .= " ON LANGUAGE \"{$object}\""; |
||
189 | |||
190 | break; |
||
191 | case 'schema': |
||
192 | $this->fieldClean($object); |
||
193 | $sql .= " ON SCHEMA \"{$object}\""; |
||
194 | |||
195 | break; |
||
196 | case 'tablespace': |
||
197 | $this->fieldClean($object); |
||
198 | $sql .= " ON TABLESPACE \"{$object}\""; |
||
199 | |||
200 | break; |
||
201 | |||
202 | default: |
||
203 | return -1; |
||
204 | } |
||
205 | |||
206 | // Dump |
||
207 | $first = true; |
||
208 | $sql .= ('GRANT' === $mode) ? ' TO ' : ' FROM '; |
||
209 | |||
210 | if ($public) { |
||
211 | $sql .= 'PUBLIC'; |
||
212 | $first = false; |
||
213 | } |
||
214 | // Dump users |
||
215 | foreach ($usernames as $v) { |
||
216 | if ($first) { |
||
217 | $sql .= "\"{$v}\""; |
||
218 | $first = false; |
||
219 | } else { |
||
220 | $sql .= ", \"{$v}\""; |
||
221 | } |
||
222 | } |
||
223 | // Dump groups |
||
224 | foreach ($groupnames as $v) { |
||
225 | if ($first) { |
||
226 | $sql .= "GROUP \"{$v}\""; |
||
227 | $first = false; |
||
228 | } else { |
||
229 | $sql .= ", GROUP \"{$v}\""; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | // Grant option |
||
234 | if ($this->hasGrantOption() && 'GRANT' === $mode && $grantoption) { |
||
235 | $sql .= ' WITH GRANT OPTION'; |
||
236 | } |
||
237 | |||
238 | // Cascade revoke |
||
239 | if ($this->hasGrantOption() && 'REVOKE' === $mode && $cascade) { |
||
240 | $sql .= ' CASCADE'; |
||
241 | } |
||
242 | |||
243 | return $this->execute($sql); |
||
244 | } |
||
411 |