Conditions | 5 |
Paths | 16 |
Total Lines | 56 |
Lines | 21 |
Ratio | 37.5 % |
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 |
||
100 | public static function activate( \wpdb $wpdb ) { |
||
101 | $wpdb->show_errors(); |
||
102 | $db = new Database( $wpdb ); |
||
103 | |||
104 | View Code Duplication | if ( ! $db->get_table( self::reports_table_name() ) ) { |
|
105 | $sql = "CREATE TABLE IF NOT EXISTS `" . self::reports_table_name() . "` ( |
||
106 | `id` INT(4) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, |
||
107 | `title` varchar(191) NOT NULL UNIQUE, |
||
108 | `description` text NOT NULL, |
||
109 | `mime_type` varchar(50) NOT NULL DEFAULT 'text/html', |
||
110 | `file_extension` varchar(10) DEFAULT NULL COMMENT 'If defined, this report will be downloaded.', |
||
111 | `template` text NOT NULL COMMENT 'The Twig template used to display this report.' |
||
112 | ) ENGINE=InnoDB;"; |
||
113 | $wpdb->query( $sql ); |
||
114 | } |
||
115 | // Reduce length of title column to floor(767/4) = 191 characters. GH60. |
||
116 | $title_size = $db->get_table( self::reports_table_name() )->get_column( 'title' )->get_size(); |
||
117 | if ( $title_size == 200 ) { |
||
118 | $wpdb->query( "ALTER TABLE `" . self::reports_table_name() . "` MODIFY `title` VARCHAR(191) NOT NULL" ); |
||
119 | } |
||
120 | |||
121 | View Code Duplication | if ( ! $db->get_table( self::report_sources_table_name() ) ) { |
|
122 | $sql = "CREATE TABLE IF NOT EXISTS `" . self::report_sources_table_name() . "` ( |
||
123 | `id` INT(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, |
||
124 | `report` INT(4) unsigned NOT NULL, |
||
125 | FOREIGN KEY (`report`) REFERENCES `" . self::reports_table_name() . "` (`id`), |
||
126 | `name` varchar(50) NOT NULL, |
||
127 | `query` text NOT NULL |
||
128 | ) ENGINE=InnoDB;"; |
||
129 | $wpdb->query( $sql ); |
||
130 | } |
||
131 | |||
132 | if ( '0' === $wpdb->get_var( "SELECT COUNT(*) FROM `" . self::reports_table_name() . "`" ) ) { |
||
133 | // Create the default report, to list all reports. |
||
134 | $template_string = "<dl>\n" |
||
135 | . "{% for report in reports %}\n" |
||
136 | . " <dt><a href='{{ admin_url('admin.php?page=tabulate&controller=reports&id='~report.id) }}'>{{report.title}}</a></dt>\n" |
||
137 | . " <dd>{{report.description}}</dd>\n" |
||
138 | . "{% endfor %}\n" |
||
139 | . "</dl>"; |
||
140 | $sql1 = "INSERT INTO `" . self::reports_table_name() . "` SET" |
||
141 | . " id = " . self::DEFAULT_REPORT_ID . ", " |
||
142 | . " title = 'Reports', " |
||
143 | . " description = 'List of all Reports.'," |
||
144 | . " template = %s;"; |
||
145 | $wpdb->query( $wpdb->prepare( $sql1, array( $template_string ) ) ); |
||
146 | // And the query for the above report. |
||
147 | $query = "SELECT * FROM " . self::reports_table_name(); |
||
148 | $sql2 = "INSERT INTO `" . self::report_sources_table_name() . "` SET " |
||
149 | . " report = " . self::DEFAULT_REPORT_ID . "," |
||
150 | . " name = 'reports'," |
||
151 | . " query = %s;"; |
||
152 | $wpdb->query( $wpdb->prepare( $sql2, array( $query ) ) ); |
||
153 | } |
||
154 | |||
155 | } |
||
156 | } |
||
157 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: