| Conditions | 27 |
| Paths | 1768 |
| Total Lines | 179 |
| Code Lines | 84 |
| Lines | 15 |
| Ratio | 8.38 % |
| Changes | 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 |
||
| 21 | public static function upgrade() |
||
| 22 | { |
||
| 23 | // 2.3dev |
||
| 24 | if (version_compare(self::$existing_version, '2.3dev', '<=')) { |
||
| 25 | Symphony::Configuration()->set('version', '2.3dev', 'symphony'); |
||
| 26 | Symphony::Configuration()->set('useragent', 'Symphony/2.3dev', 'general'); |
||
| 27 | |||
| 28 | // Add Publish Label to `tbl_fields` |
||
| 29 | if (!Symphony::Database()->tableContainsField('tbl_fields', 'publish_label')) { |
||
| 30 | Symphony::Database()->query('ALTER TABLE `tbl_fields` ADD `publish_label` VARCHAR(255) DEFAULT NULL'); |
||
| 31 | } |
||
| 32 | |||
| 33 | // Migrate any Checkbox's Long Description to Publish Label |
||
| 34 | try { |
||
| 35 | $checkboxes = Symphony::Database()->fetch("SELECT `field_id`, `description` FROM `tbl_fields_checkbox`"); |
||
| 36 | |||
| 37 | foreach ($checkboxes as $field) { |
||
|
|
|||
| 38 | if (!isset($field['description'])) { |
||
| 39 | continue; |
||
| 40 | } |
||
| 41 | |||
| 42 | Symphony::Database()->query(sprintf(" |
||
| 43 | UPDATE `tbl_fields` |
||
| 44 | SET `publish_label` = '%s' |
||
| 45 | WHERE `id` = %d |
||
| 46 | LIMIT 1; |
||
| 47 | ", |
||
| 48 | $field['description'], |
||
| 49 | $field['field_id'] |
||
| 50 | )); |
||
| 51 | } |
||
| 52 | |||
| 53 | Symphony::Database()->query("ALTER TABLE `tbl_fields_checkbox` DROP `description`"); |
||
| 54 | } catch (Exception $ex) { |
||
| 55 | } |
||
| 56 | |||
| 57 | // Removing unused settings |
||
| 58 | Symphony::Configuration()->remove('allow_page_subscription', 'symphony'); |
||
| 59 | Symphony::Configuration()->remove('strict_error_handling', 'symphony'); |
||
| 60 | Symphony::Configuration()->remove('character_set', 'database'); |
||
| 61 | Symphony::Configuration()->remove('character_encoding', 'database'); |
||
| 62 | Symphony::Configuration()->remove('runtime_character_set_alter', 'database'); |
||
| 63 | |||
| 64 | if (Symphony::Configuration()->get('pagination_maximum_rows', 'symphony') === '17') { |
||
| 65 | Symphony::Configuration()->set('pagination_maximum_rows', '20', 'symphony'); |
||
| 66 | } |
||
| 67 | |||
| 68 | Symphony::Configuration()->write(); |
||
| 69 | } |
||
| 70 | |||
| 71 | // 2.3 Beta 1 |
||
| 72 | if (version_compare(self::$existing_version, '2.3beta1', '<=')) { |
||
| 73 | Symphony::Configuration()->set('version', '2.3beta1', 'symphony'); |
||
| 74 | Symphony::Configuration()->set('useragent', 'Symphony/2.3 Beta 1', 'general'); |
||
| 75 | |||
| 76 | Symphony::Configuration()->write(); |
||
| 77 | } |
||
| 78 | |||
| 79 | // 2.3 Beta 2 |
||
| 80 | if (version_compare(self::$existing_version, '2.3beta2', '<=')) { |
||
| 81 | // Migrate Publish Labels (if created) to the Label field |
||
| 82 | // Then drop Publish Label, we're going to use element_name and label |
||
| 83 | // to take care of the same functionality! |
||
| 84 | try { |
||
| 85 | if (Symphony::Database()->tableContainsField('tbl_fields', 'publish_label')) { |
||
| 86 | $fields = Symphony::Database()->fetch('SELECT `publish_label`, `label`, `id` FROM `tbl_fields`'); |
||
| 87 | |||
| 88 | foreach ($fields as $field) { |
||
| 89 | if (!$field['publish_label']) { |
||
| 90 | continue; |
||
| 91 | } |
||
| 92 | |||
| 93 | Symphony::Database()->query(sprintf(" |
||
| 94 | UPDATE `tbl_fields` |
||
| 95 | SET `label` = '%s' |
||
| 96 | WHERE `id` = %d |
||
| 97 | LIMIT 1; |
||
| 98 | ", |
||
| 99 | $field['publish_label'], |
||
| 100 | $field['id'] |
||
| 101 | )); |
||
| 102 | } |
||
| 103 | |||
| 104 | Symphony::Database()->query("ALTER TABLE `tbl_fields` DROP `publish_label`"); |
||
| 105 | } |
||
| 106 | } catch (Exception $ex) { |
||
| 107 | Symphony::Log()->notice($ex->getMessage()); |
||
| 108 | } |
||
| 109 | |||
| 110 | // Add uniqueness constraint for the Authors table. #937 |
||
| 111 | try { |
||
| 112 | Symphony::Database()->query("ALTER TABLE `tbl_authors` ADD UNIQUE KEY `email` (`email`)"); |
||
| 113 | } catch (DatabaseException $ex) { |
||
| 114 | // 1061 will be 'duplicate key', which is fine (means key was added earlier) |
||
| 115 | // 1062 means the key failed to apply, which is bad. |
||
| 116 | // @see http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html |
||
| 117 | if ($ex->getDatabaseErrorCode() === 1062) { |
||
| 118 | Symphony::Log()->error( |
||
| 119 | __("You have multiple Authors with the same email address, which can cause issues with password retrieval. Please ensure all Authors have unique email addresses before updating. " . $ex->getMessage()) |
||
| 120 | ); |
||
| 121 | |||
| 122 | return false; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | // Update the version information |
||
| 127 | Symphony::Configuration()->set('version', '2.3beta2', 'symphony'); |
||
| 128 | Symphony::Configuration()->set('useragent', 'Symphony/2.3 Beta 2', 'general'); |
||
| 129 | |||
| 130 | Symphony::Configuration()->write(); |
||
| 131 | } |
||
| 132 | |||
| 133 | // 2.3 Beta 3 |
||
| 134 | if (version_compare(self::$existing_version, '2.3beta3', '<=')) { |
||
| 135 | // Refresh indexes on existing Author field tables |
||
| 136 | $author_fields = Symphony::Database()->fetchCol("field_id", |
||
| 137 | "SELECT `field_id` FROM `tbl_fields_author`"); |
||
| 138 | |||
| 139 | View Code Duplication | foreach ($author_fields as $id) { |
|
| 140 | $table = 'tbl_entries_data_' . $id; |
||
| 141 | |||
| 142 | // MySQL doesn't support DROP IF EXISTS, so we'll try and catch. |
||
| 143 | try { |
||
| 144 | Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP INDEX `entry_id`"); |
||
| 145 | } catch (Exception $ex) { |
||
| 146 | } |
||
| 147 | |||
| 148 | try { |
||
| 149 | Symphony::Database()->query("CREATE UNIQUE INDEX `author` ON `" . $table . "` (`entry_id`, `author_id`)"); |
||
| 150 | Symphony::Database()->query("OPTIMIZE TABLE " . $table); |
||
| 151 | } catch (Exception $ex) { |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | // Move section sorting data from the database to the filesystem. #977 |
||
| 156 | if (Symphony::Database()->tableContainsField('tbl_sections', 'entry_order')) { |
||
| 157 | $sections = Symphony::Database()->fetch("SELECT `handle`, `entry_order`, `entry_order_direction` FROM `tbl_sections`"); |
||
| 158 | foreach ($sections as $s) { |
||
| 159 | Symphony::Configuration()->set('section_' . $s['handle'] . '_sortby', $s['entry_order'], |
||
| 160 | 'sorting'); |
||
| 161 | Symphony::Configuration()->set('section_' . $s['handle'] . '_order', |
||
| 162 | $s['entry_order_direction'], 'sorting'); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | // Drop `local`/`gmt` from Date fields, add `date` column. #693 |
||
| 167 | $date_fields = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_date`"); |
||
| 168 | |||
| 169 | foreach ($date_fields as $id) { |
||
| 170 | $table = 'tbl_entries_data_' . $id; |
||
| 171 | |||
| 172 | // Don't catch an Exception, we should halt updating if something goes wrong here |
||
| 173 | // Add the new `date` column for Date fields |
||
| 174 | if (!Symphony::Database()->tableContainsField($table, 'date')) { |
||
| 175 | Symphony::Database()->query("ALTER TABLE `" . $table . "` ADD `date` DATETIME DEFAULT NULL"); |
||
| 176 | Symphony::Database()->query("CREATE INDEX `date` ON `" . $table . "` (`date`)"); |
||
| 177 | } |
||
| 178 | |||
| 179 | if (Symphony::Database()->tableContainsField($table, 'date')) { |
||
| 180 | // Populate new Date column |
||
| 181 | if (Symphony::Database()->query("UPDATE `" . $table . "` SET date = CONVERT_TZ(SUBSTRING(value, 1, 19), SUBSTRING(value, -6), '+00:00')")) { |
||
| 182 | // Drop the `local`/`gmt` columns from Date fields |
||
| 183 | if (Symphony::Database()->tableContainsField($table, 'local')) { |
||
| 184 | Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP `local`;"); |
||
| 185 | } |
||
| 186 | |||
| 187 | if (Symphony::Database()->tableContainsField($table, 'gmt')) { |
||
| 188 | Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP `gmt`;"); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | Symphony::Database()->query("OPTIMIZE TABLE " . $table); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | // Update the version information |
||
| 198 | return parent::upgrade(); |
||
| 199 | } |
||
| 200 | |||
| 209 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.