Link Post: Your Greatest Code Quality Threats and How to Solve Them

I find that heterogeneity can really hurt productivity. I’ll sometime brood over how someone didn’t use braces for an if statement or didn’t tab the correct number of times (really, how hard is this!) and I’ll get lost in the fact that I’m there looking to fix a bug. The other items on this list are good too.

http://blog.codacy.com/2014/06/19/your-greatest-code-quality-threats-and-how-to-solve-them/

A Fix For MySQLs 1292 Error

I was trying to add a new column to an old table and I ran into a problem:

Error running query: ALTER TABLE table ADD COLUMN column1 VARCHAR(255) NOT NULL
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: ’0000-00-00′ for column ‘column’ at row 2

It turns on something similar to this problem has existed for a long time (http://bugs.mysql.com/bug.php?id=33240) and the fix is to run the command below before running the alter:

set SQL_MODE = '';

Looping Your Unit Tests

When I write my code I like to have my unit tests run automatically but sometimes I need to focus on a single test but I don’t want to tab to the window where you’re running your unit tests to run them (I know, how lazy can you be). When this happen I like to do two things.

The first is that I can run phpunit in a continuous loop with a one second delay so I can read the screen:

while(true); do vendor/bin/phpunit -c tests --filter <filter>; sleep 1; done

This works well but you end up having the tests run constantly which can drain your battery (if like me you aren’t plugged in every so often). In this case I use the following:

while(true); do vendor/bin/phpunit -c tests --filter <filter>; read -p "Press Enter..."; done

Which will wait for an enter key before running the tests a second time.

Working With CSV Files in PHP

Working with CSV files sucks. You have to handle different line endings and entries can be wrapped in double quotess which could have a comma inside of it preventing you from just using explode(). Thankfully, PHP provides two functions for working with CSV files that are super helpful.

fgetcsv

The first is fgetcsv which reads from a file handle and brings in single line broken into an array. If you’ve every worked with processing csv files before you know how awesome this is. As an example,

// this prevents problems with different line endings
// see http://www.thisprogrammingthing.com/2012/oddity-with-fgetcsv/
ini_set('auto_detect_line_endings', true);

$file = fopen('filePath', 'r') or die('Unable to open file!');

while(($row = fgetcsv($file)) !== false){
    // work with the lines here
}

fclose($file);

fputcsv

The other is fputcsv which writes to a file with the correct formatting for a CSV file:

$file = fopen('filePath', 'w') or die('Unable to open file!');

foreach($array as $row)
    fputcsv($file, $row);
}

fclose($file);

Output to the Browser

Occasionally, we want to create a CSV file for a user to download. We could save the file to disk and then redirect to the output but I would rather just output the results:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"output.csv\";" );
header("Content-Transfer-Encoding: binary"); 

// we're going to write directly to the output buffer
$file = fopen('php:://output', 'w') or die('Unable to open file!');

// output the header row
fputcsv($file, array_get_keys($array[0]));

foreach($array as $row)
    fputcsv($file, $row);
}

fclose($file);

Converting a CSV File Into an Associative Array

Using fgetcsv we can convert a CSV file into an associative array. I like this approach because then it doesn’t mater what order the columns are in the CSV.

// this prevents problems with different line endings
// see http://www.thisprogrammingthing.com/2012/oddity-with-fgetcsv/
ini_set('auto_detect_line_endings', true);

$file = fopen('test.csv', 'r') or die('Unable to open file!');

$returnVal = array();
$header = null;

while(($row = fgetcsv($file)) !== false){
    if($header === null){
        $header = $row;
        continue;
    }

    $newRow = array();
    for($i = 0; $i<count($row); $i++){
        $newRow[$header[$i]] = $row[$i];
    }

    $returnVal[] = $newRow;
}

fclose($file);

// $returnVal now contains the contents of the CSV file
var_dump($returnVal);

Troubleshooting No Diskspace

A couple weeks ago I was working on an older development VM that was setup with a smaller hard drive and I started getting unexpected end of file error messages. I couldn’t find the problem in my file so I was going to run git diff on the file to see what I’ve changed. I started do this and ran into a problem:

user@VM:/var/www/$ ls -l <tab>bash: cannot create temp file for here-document: No space left on device
bash: cannot create temp file for here-document: No space left on device

Shit!

Well, that explains why I couldn’t find the problem…
Continue reading

Display the Progress of a MySQL Restore

At work every week or so I need to restore a 5+ GB mysql database because someone deleted or overwrote something accidentally (this has gotten better now that we don’t delete anything). But it still happens and during the restore we’re presented with a nice blank screen as it’s doing it’s work.

You can use the pv command to monitor the progress of the data going through a pipe. In our case we backup the data using mysqldump and then pipe it through gzip to take it down to a more reasonable file size. In the restore process we have to unzip it but we can restore it using a one liner:

pv mysqldump.sql.gz | gunzip |  mysql -uusername -p databasename