Conditions | 11 |
Paths | 78 |
Total Lines | 51 |
Code Lines | 32 |
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 |
||
30 | public function buildKeyDefinitionFromArray($k, $options) |
||
31 | { |
||
32 | if (is_string($options)) { |
||
33 | $options = ['type' => $options]; |
||
34 | } elseif (!is_array($options)) { |
||
|
|||
35 | throw new DatabaseStatementException('Key value can only be a string or an array'); |
||
36 | } elseif (!isset($options['type'])) { |
||
37 | throw new DatabaseStatementException('Key type must be defined.'); |
||
38 | } |
||
39 | $type = strtolower($options['type']); |
||
40 | $k = $this->asTickedString($k); |
||
41 | $cols = isset($options['cols']) ? $options['cols'] : $k; |
||
42 | $typeIndex = in_array($type, [ |
||
43 | 'key', 'unique', 'primary', 'fulltext', 'index' |
||
44 | ]); |
||
45 | if ($typeIndex === false) { |
||
46 | throw new DatabaseStatementException("Key of type `$type` is not valid"); |
||
47 | } |
||
48 | switch ($type) { |
||
49 | case 'unique': |
||
50 | $type = strtoupper($type) . ' KEY'; |
||
51 | break; |
||
52 | case 'primary': |
||
53 | // Use the key name as the KEY keyword |
||
54 | // since the primary key does not have a name |
||
55 | $k = 'KEY'; |
||
56 | // fall through |
||
57 | default: |
||
58 | $type = strtoupper($type); |
||
59 | break; |
||
60 | } |
||
61 | |||
62 | // Format columns including size |
||
63 | $colsFormatted = []; |
||
64 | |||
65 | if (!is_array($cols)) { |
||
66 | $cols = [$cols]; |
||
67 | } |
||
68 | |||
69 | foreach ($cols as $key => $value) { |
||
70 | // No Size |
||
71 | if (General::intval($key) !== -1) { |
||
72 | $colsFormatted[] = $this->asTickedString($value); |
||
73 | // Size |
||
74 | } else { |
||
75 | $colsFormatted[] = $this->asTickedString($key) . '(' . $value . ')'; |
||
76 | } |
||
77 | } |
||
78 | $colsFormatted = implode(self::LIST_DELIMITER, $colsFormatted); |
||
79 | |||
80 | return "$type $k ($colsFormatted)"; |
||
81 | } |
||
83 |