Conditions | 12 |
Paths | 450 |
Total Lines | 84 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 1 |
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 |
||
73 | function xoops_module_update_randomquote(&$module, $installedVersion = null) |
||
74 | { |
||
75 | xoops_loadLanguage('admin', $module->dirname()); |
||
76 | $errors = 0; |
||
77 | if (tableExists($GLOBALS['xoopsDB']->prefix('citas'))) { |
||
78 | |||
79 | $sql = sprintf('ALTER TABLE ' |
||
80 | . $GLOBALS['xoopsDB']->prefix('citas') |
||
81 | . ' CHANGE `citas` `quote` TEXT' |
||
82 | ); |
||
83 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
84 | if (!$result) { |
||
85 | $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED0); |
||
86 | ++$errors; |
||
87 | } |
||
88 | |||
89 | $sql = sprintf('ALTER TABLE ' |
||
90 | . $GLOBALS['xoopsDB']->prefix('citas') |
||
91 | . " ADD COLUMN `quote_status` int (10) NOT NULL default '0'," |
||
92 | . " ADD COLUMN `quote_waiting` int (10) NOT NULL default '0'," |
||
93 | . " ADD COLUMN `quote_online` int (10) NOT NULL default '0';" |
||
94 | ); |
||
95 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
96 | if (!$result) { |
||
97 | $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED1); |
||
98 | ++$errors; |
||
99 | } |
||
100 | |||
101 | $sql = sprintf('ALTER TABLE ' |
||
102 | . $GLOBALS['xoopsDB']->prefix('citas') |
||
103 | . ' RENAME ' |
||
104 | . $GLOBALS['xoopsDB']->prefix('randomquote_quotes') |
||
105 | ); |
||
106 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
107 | if (!$result) { |
||
108 | $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED2); |
||
109 | ++$errors; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | if ($installedVersion < 211) { |
||
114 | // add column for quotes table for date created |
||
115 | $result = $GLOBALS['xoopsDB']->queryF("SHOW COLUMNS FROM " . $GLOBALS['xoopsDB']->prefix('randomquote_quotes') . " LIKE 'create_date'"); |
||
116 | $foundCreate = $GLOBALS['xoopsDB']->getRowsNum($result); |
||
117 | if (empty($foundCreate)) { |
||
118 | // column doesn't exist, so try and add it |
||
119 | $success = $GLOBALS['xoopsDB']->queryF("ALTER TABLE " . $GLOBALS['xoopsDB']->prefix('reandomquote_quotes') . " ADD create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP AFTER quote_status"); |
||
120 | if (!$success) { |
||
121 | $module->setErrors(sprintf(_AM_RANDOMQUOTE_ERROR_COLUMN, 'create_date')); |
||
122 | ++$errors; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | // change status to indicate quote waiting approval |
||
127 | $sql = "UPDATE " . $GLOBALS['xoopsDB']->prefix('randomquote_quotes') . " SET quote_status=2 WHERE `quote_waiting` > 0"; |
||
128 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
129 | if (!$result) { |
||
130 | $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED1); |
||
131 | ++$errors; |
||
132 | } |
||
133 | |||
134 | // change status to indicate quote online |
||
135 | $sql = "UPDATE " . $GLOBALS['xoopsDB']->prefix('randomquote_quotes') . " SET quote_status=1 WHERE `quote_online` > 0"; |
||
136 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
137 | if (!$result) { |
||
138 | $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED1); |
||
139 | ++$errors; |
||
140 | } |
||
141 | |||
142 | // drop the waiting and online columns |
||
143 | $sql = sprintf('ALTER TABLE ' |
||
144 | . $GLOBALS['xoopsDB']->prefix('randomquote_quotes') |
||
145 | . " DROP COLUMN `quote_waiting`," |
||
146 | . " DROP COLUMN `quote_online`;" |
||
147 | ); |
||
148 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
149 | if (!$result) { |
||
150 | $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED1); |
||
151 | ++$errors; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | return ($errors) ? false : true; |
||
156 | } |
||
157 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.