| @@ -46,10 +46,10 @@ | ||
| 46 | 46 | * |
| 47 | 47 | * @access public |
| 48 | 48 | * @param string $filename The path to the file. |
| 49 | 49 | */ |
| 50 | - public function __construct( $filename = null ) { | |
| 51 | - $this->_filename = trim( $filename ); | |
| 50 | + public function __construct( $filename = '' ) { | |
| 51 | + $this->_filename = trim( (string) $filename ); | |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
| 55 | 55 | * Fetches series information. |
| @@ -76,26 +76,24 @@ | ||
| 76 | 76 | // read series types |
| 77 | 77 | $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | + $labels = array_filter( $labels ); | |
| 81 | + $types = array_filter( $types ); | |
| 82 | + | |
| 80 | 83 | if ( ! $labels || ! $types ) { |
| 84 | + $this->_error = esc_html__( 'File should have a heading row (1st row) and a data type row (2nd row). Please try again.', 'visualizer' ); | |
| 81 | 85 | return false; |
| 82 | 86 | } |
| 83 | 87 | |
| 84 | - // if no types were setup, re read labels and empty types array | |
| 85 | 88 | $types = array_map( 'trim', $types ); |
| 86 | 89 | if ( ! self::_validateTypes( $types ) ) { |
| 87 | - // re open the file | |
| 88 | - fclose( $handle ); | |
| 89 | - $handle = $this->_get_file_handle(); | |
| 90 | - | |
| 91 | - // re read the labels and empty types array | |
| 92 | - $labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ); | |
| 93 | - $types = array(); | |
| 90 | + $this->_error = esc_html__( 'Invalid data types detected in the data type row (2nd row). Please try again.', 'visualizer' ); | |
| 91 | + return false; | |
| 94 | 92 | } |
| 95 | 93 | |
| 96 | 94 | for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) { |
| 97 | - $default_type = $i == 0 ? 'string' : 'number'; | |
| 95 | + $default_type = $i === 0 ? 'string' : 'number'; | |
| 98 | 96 | |
| 99 | 97 | $labels[ $i ] = $this->toUTF8( $labels[ $i ] ); |
| 100 | 98 | |
| 101 | 99 | $this->_series[] = array( |
| @@ -116,10 +114,8 @@ | ||
| 116 | 114 | * @param string $filename Optional file name to get handle. If omitted, $_filename is used. |
| 117 | 115 | * @return resource File handle resource on success, otherwise FALSE. |
| 118 | 116 | */ |
| 119 | 117 | protected function _get_file_handle( $filename = false ) { |
| 120 | - // set line endings auto detect mode | |
| 121 | - ini_set( 'auto_detect_line_endings', true ); | |
| 122 | 118 | // open file and return handle |
| 123 | 119 | return fopen( $filename ? $filename : $this->_filename, 'rb' ); |
| 124 | 120 | } |
| 125 | 121 | |
| @@ -133,8 +129,9 @@ | ||
| 133 | 129 | */ |
| 134 | 130 | public function fetch() { |
| 135 | 131 | // if filename is empty return false |
| 136 | 132 | if ( empty( $this->_filename ) ) { |
| 133 | + $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' ); | |
| 137 | 134 | return false; |
| 138 | 135 | } |
| 139 | 136 | |
| 140 | 137 | // read file and fill arrays |
| @@ -145,10 +142,10 @@ | ||
| 145 | 142 | return false; |
| 146 | 143 | } |
| 147 | 144 | |
| 148 | 145 | // fetch data |
| 146 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition | |
| 149 | 147 | while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) { |
| 150 | - | |
| 151 | 148 | $this->_data[] = $this->_normalizeData( $data ); |
| 152 | 149 | } |
| 153 | 150 | |
| 154 | 151 | // close file handle |