phpunit travis ci undefined offset

or

How I learned to stop hating on PHP

PHPUnit Travis CI Undefined Offset

TL;DR - "Undefined offset" is an E_STRICT level PHP error, and travis-ci.org uses E_STRICT+ by default. Put error_reporting( E_ALL ); at the top of your phpunit test suite to find and fix all your warnings and notices to fix Travis builds.

pHpaters

Today Laura and I chatted briefly about djangocon presenters bashing PHP. I mentioned that the Promote MDN code is some of my favorite code - in any language. As with my Zend_Rest code, the two keys I've found to writing good PHP code are:

  1. Use a coding standard
  2. Write unit tests

Coding Standard

I use Chris Adams WordPress Coding Standards for PHP_CodeSniffer to stick to that. It was amazing how much nicer the PHP code was when I took just an hour to clean up what I inherited from SEO Smart Links. The PHP community would do well to embrace, encourage, and even enforce coding standards more - the way the Python community does with PEP8.

Unit Tests

I write unit tests for Promote MDN too. My tests/doubles.php is hacky, but it Freaking Works™; I didn't have to build out a set of fixture data or couple the test suite to a running WordPress instance. Like WordPress, I just (ab)use the global namespace to (re-)define dependency functions with doubles.

I also took the opportunity to try out Travis CI. After fixing the travis.yaml and phpunit.xml files just right I got a bunch of test errors with the message "Undefined offset: 1" which frustrated me because I didn't see them locally. While we chatted about PHP, Laura reminded me about PHP's error reporting levels, and she was spot-on. I bumped up to E_ALL locally and got the same errors as Travis.

Error Reporting Level

I spent the next 10 minutes fixing my warnings and notices and realized - I should always use a higher error level in PHP. Python will rightfully gripe at me if I try to access a dictionary element by a key that doesn't exist. But PHP can be equally strict if I set my error level to E_STRICT or higher. So, I'm adding it to my list:

  1. Use a coding standard
  2. Write unit tests
  3. Set error level to E_STRICT

I really think these 3 practices could make any PHP code into good solid code. Instead of simply bashing on PHP, from now on I'm going to simply advise PHP dev's to stick to these practices - it's good for the developers and good for the community.

Question or comment about this post? Tell me on GitHub.

phpunit travis ci undefined offset / groovecoder by groovecoder is licensed under a Creative Commons Attribution-ShareAlike CC BY-SA
  1. I think you can put this in your phpunit.xml, a little nicer than setting it somewhere in your test suite
  2. The PHP Framework Interoperability Group, consisting of lead devs from most of the popular PHP frameworks and libraries, currently work together to define autoloading standards and coding standards. The framework and library authors agree to adopt the standards set forth by the group to increase interoperability and collaboration between each others' frameworks and libraries. http://www.php-fig.org/faq/ https://github.com/php-fig/fig-standards Your post says you were working with WordPress, whose authors were approached about joining the FIG group at one time, but to which they declined.
  3. Ah, I had never heard of php fig. https://github.com/klaussilveira/phpcs-psr is a very useful link for that too. ;)
phpunit travis ci undefined offset / groovecoder by groovecoder is licensed under a Creative Commons Attribution-ShareAlike CC BY-SA