Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
| 27 | class EEH_Export { |
||
| 28 | /** |
||
| 29 | * Gets the 'normal' column named for fields |
||
| 30 | * @param EE_Model_Field_Base $field |
||
| 31 | * @return string |
||
| 32 | */ |
||
| 33 | public static function get_column_name_for_field(EE_Model_Field_Base $field){ |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Writes $data to the csv file open in $filehandle. uses the array indices of $data for column headers |
||
| 39 | * |
||
| 40 | * @param string $filepath |
||
| 41 | * @param array $data 2D array, first numerically-indexed, |
||
| 42 | * and next-level-down preferably indexed by string |
||
| 43 | * @param boolean $write_column_headers whether or not we should add the keys in the bottom-most array |
||
| 44 | * as a row for headers in the CSV. |
||
| 45 | * Eg, if $data looked like: |
||
| 46 | * array( |
||
| 47 | * 0=>array('EVT_ID'=>1,'EVT_name'=>'monkey'...), |
||
| 48 | * 1=>array(...,...) |
||
| 49 | * ) |
||
| 50 | * |
||
| 51 | * @return boolean if we successfully wrote to the CSV or not. If there's no $data, |
||
| 52 | * we consider that a success (because we wrote everything there was...nothing) |
||
| 53 | * @throws EE_Error |
||
| 54 | */ |
||
| 55 | public static function write_data_array_to_csv( $filepath, $data, $write_column_headers = true ){ |
||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * |
||
| 90 | * Writes a row to the csv file |
||
| 91 | * @param array $row - individual row of csv data |
||
| 92 | * @param string $delimiter - csv delimiter |
||
| 93 | * @param string $enclosure - csv enclosure |
||
| 94 | * @param bool $mysql_null - allows php NULL to be overridden with MySQl's insertable NULL value |
||
| 95 | * @return string of text for teh csv file |
||
| 96 | */ |
||
| 97 | View Code Duplication | public static function get_csv_row ( array $row, $delimiter = ',', $enclosure = '"', $mysql_null = false ) { |
|
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * Shortcut for preparing a database result for display |
||
| 126 | * @param EEM_Base $model |
||
| 127 | * @param string $field_name |
||
| 128 | * @param string $raw_db_value |
||
| 129 | * @param boolean|string $pretty_schema true to display pretty, a string to use a specific "Schema", or false to NOT display pretty |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | View Code Duplication | public static function prepare_value_from_db_for_display( $model, $field_name, $raw_db_value, $pretty_schema = true ) { |
|
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Gets the date format to use in exports. filterable |
||
| 152 | * @param string $current_format |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public static function get_date_format_for_export( $current_format = null ) { |
||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Gets the time format we want to use in exports. Filterable |
||
| 163 | * @param string $current_format |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public static function get_time_format_for_export( $current_format = null ) { |
||
| 169 | |||
| 170 | |||
| 171 | |||
| 172 | } |
||
| 173 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.