Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Christian Mackerprang:
How terrible code gets written by perfectly sane people
Nov 30, 2016 @ 12:16:26

Christian Mackerprang has an interesting post to his site sharing some of his thoughts about why terrible code gets written by sane people - developers that know what they're doing but, for other reasons, write code that's a mess of anti-patterns and inconsistency.

What I discovered after some months working there [on a legacy Python project], was that the authors were actually an experienced group of senior developers with good technical skills. What could lead a team of competent developers to produce and actually deliver something like this? What I’ve come up is a list. These are some bad habits that even experienced teams can get into which will severely affect your end product, more than any static code checker or development methodology could rescue it from.

His list of reasons covers six of the reasons he sees for the "good people, bad code" situation happening:

  • Giving excessive importance to estimates
  • Giving no importance to project knowledge
  • Focusing on poor metrics such as “issues closed” or “commits per day”
  • Assuming that good process fixes bad people
  • Ignoring proven practices such as code reviews and unit testing
  • Hiring developers with no “people” skills

For each item in the list he briefly covers why it's a bad thing for your engineering group and references to other sources on good suggestions to fix the situation.

tagged: terrible code sane people opinion reasons

Link: http://chrismm.com/blog/how-terrible-code-gets-written-by-perfectly-sane-people/

QaFoo Blog:
Using Traits With PHPUnit
Nov 29, 2016 @ 12:26:19

The QaFoo site has a new post showing an interesting practice that could be used in your PHPUnit tests to provide additional functionality without the need for complicated inheritance - the use of traits.

As we already wrote that "Code Reuse By Inheritance" has lots of problems and we consider it a code smell. You should always aim to use Dependency Injection, most likely Constructor Injection. But with test cases in PHPUnit we cannot do this because we have no control about how and when our test cases are created. There are a similar problem in other frameworks, like we discussed in "Object Lifecycle Control". We also blogged about traits as a Code Smell, but let me show and explain why they might be fine to use in your test cases.

They provide an example of where the use of traits might be acceptable starting with a simple test case to check the login behavior with an invalid password. This uses an "is a" inheritance relationship with a parent test class with setUp/tearDown method. This refactored a bit to make use of traits to provide common login functionality based on methods in a trait. The post wraps up talking about traits as a "code smell" despite them seemingly making the test code cleaner, mostly that it limits the ability to change functionality by simply changing the associated code.

tagged: traits phpunit tests code smell example tutorial

Link: https://qafoo.com/blog/092_using_traits_with_phpunit.html

Zend Developer Zone:
Scheduling ElePHPants (DateTime math is HARD)
Nov 18, 2016 @ 12:49:38

On the Zend Developer Zone there's a new post talking about scheduling in applications ("scheduling elePHPants") including both library recommendations and advice about code reuse.

It was while I was creating the 100th or so cronjob to do some very similar to the other 99 that I thought, “Hey! Why not just put all this in a database and check it once a minute instead?” [...] It would be so much easier to deal with in PHP. Also, cron does not scale well at all either in performance or management.

The problem is that cron is an elegant solution for “Do this at that time” type of problems. Every solution I came up with was basically re-creating cron. That in itself isn’t a bad thing, but the logic involved in doing what cron does is mind-melting.

[...] Then it hit me, I am probably not the first person that has had this need. There have probably been other people who needed to implement “Do this at that time” within a PHP application. So I started looking around. What I found was encouraging.

The author then mentions several packages that he went through searching for the right solution to his problem, noting that while Laravel-based solutions seemed nice, they wouldn't work with his framework choice (Slim). He decided on the cron-expression package, finding it to be the best fit for the project's needs.

I had spent countless hours trying to create the solution myself. [...] I got so lost in solving the problem, I forgot to look to see if someone had already solved it. [...] After I finally came to my senses, I tweeted that out to remind myself to “Use the Source”.
tagged: schedule task cron experience package code reuse datetime

Link: https://devzone.zend.com/7418/scheduling-elephpants-datetime-math-is-hard/

Andreas Creten:
Does code need to be perfect?
Nov 11, 2016 @ 09:55:57

On his Medium.com blog Andreas Creten has written up a post that tries to answer the question "Does code need to be perfect?" As developers we have a drive to take pride in our work and want it to be the best code possible. However, that can lead to some bad practices...

In the past months I have asked myself a lot why we always strive to write perfect code. Picking up coding again for an internal project made me realise our team (and probably a large part of the rest of the software development world) spend a lot of time on writing perfectly formatted, ordered, patterned and tested code. But is this really necessary?

[...] The engineers want to write perfect code using the latest techniques, make sure that the code is well documented so they can fully understand how everything works and that it has tests so they can easily update things later. Product owners on the other hand just want things to be done, fast and cheap, so they can ship new features or convince new clients. How can you make these conflicting views work together?

He offers a few different suggestions for those developers wanting to craft the perfect codebase including coding for "now" not the future and the fact that "perfect code" just doesn't exist. He offers some suggestions for dealing with that "non-perfect code" you come across in your codebase, when starting from scratch makes sense and thinking about how "perfect" the code needs to be at the outset.

tagged: perfect code opinion development practices

Link: https://medium.com/we-are-madewithlove/does-code-need-to-be-perfect-a53f36ad7163#.jdqre42fu

Exakat.io Blog:
Automatically Enforcing Coding Reference For PHP
Nov 10, 2016 @ 09:42:07

On the Exakat.io blog there's a post continuing on from a "dos and donts" article about programmer practices. In this new post cover some of the rules in a bit more detail from the original article.

Last week, I ran into ‘PHP Dos and Don’ts aka Programmers I Don’t Like‘ on reddit’s PHP group. It features a list of 11 points that the author hates finding in PHP code. [...] The most interesting point is that coding references are used to asses code. They may very well be automated, thanks to static analysis. I thought it was a good challenge for Exakat.

The list covers several of the rules in the list, providing a brief explanation (and a bit of how it relates back to PHP) including:

  • Unnecessary casting
  • Extra bracket and braces
  • Lack of coding standard
  • Too many nested if statements

He ends the post with a bit of detail about how the Exakat service is checking for these "failures" in it's newer "RadwellCode" report. He even gives the scores of some of the more well-known and used PHP projects including the Zend Framework (v1.12), WordPress, Composer and even Exakat itself.

tagged: enforce coding reference exakat code staticanalysis tool oliverradwell

Link: https://www.exakat.io/enforcing-coding-reference/

SitePoint PHP Blog:
Is It Possible to Write and Run PHP Code on an iPad?
Oct 25, 2016 @ 11:23:52

In this new tutorial from the SitePoint PHP blog Christopher Pitt once again takes on an unconventional question around the use of PHP: "is it possible to write/run PHP code on an iPad?"

I love the iPad. It’s a fantastic form factor for media consumption and gaming; and it also works well as an e-reader. The trouble is I don’t use it nearly as much as I could. Most of the time I’m consuming media (Netflix, Twitch, YouTube), I’m coding in parallel.

I can do that on my MacBook, but I’ve never been able to do that until now. [...] Today I’m going to show you how I code on an iPad. I won’t pretend it’s a perfect workflow (what workflow is?), but this is as exciting for me as the first time I used a laptop instead of a desktop.

He briefly covers some of the hardware you'll need to get started including a good keyboard you're comfortable with and a way to hook it up (either Bluetooth or manually via USB). He then gets into the software and mentions DraftCode and WorkingCopy as his tools of choice. He then helps you get started writing your first PHP script, using SQLite in-memory and using Working Copy to pull the latest code from your external source. Now that you're comfortable with the setup, he shows you how to install a Laravel application and the changes you'll need to make to get it up and running. Finally he shows the push of the code the remote source, updating the repository with these latest changes.

tagged: write run code ipad workingcopy draftcode laravel tutorial

Link: https://www.sitepoint.com/is-it-possible-to-write-and-run-php-code-on-an-ipad/

Toptal.com:
Buggy PHP Code: The 10 Most Common Mistakes PHP Developers Make
Oct 07, 2016 @ 11:12:27

On the Toptal.com site a new article has been published from author Ilya Sanosyan with his list of top ten development mistakes he sees PHP developers make leading to more buggy code.

PHP makes it relatively easy to build a web-based system, which is much of the reason for its popularity. But its ease of use notwithstanding, PHP has evolved into quite a sophisticated language with many frameworks, nuances, and subtleties that can bite developers, leading to hours of hair-pulling debugging. This article highlights ten of the more common mistakes that PHP developers need to beware of.

Included in his list are common issues like:

  • Leaving dangling array references after foreach loops
  • Confusion about returning by reference vs. by value
  • Performing queries in a loop
  • Assuming $_POST will always contain your POST data
  • Ignoring coding standards

For each item on his list he provides good code examples and explanation of both why it's an issue and what can be done to prevent it.

tagged: top10 common mistakes programmers developers list example code

Link: https://www.toptal.com/php/10-most-common-mistakes-php-programmers-make

Jani Hartikainen:
How many tests is too many?
Sep 13, 2016 @ 09:21:21

While not specific to PHP Jani Hartikainen asks an interesting question in his latest post - how many tests are too many?. He gives an example of the number of tests in a widely used open source project and how, sometimes, more tests doesn't mean better code.

Some time ago I stumbled upon some crazy stuff… Specifically, I found out that SQLite has 787 times more tests than they have actual code! It’s no joke, it’s documented right on their website. While they have about 116 300 lines of source code, they have 91 577 300 lines of test code.

That sounds completely insane. [...] I bet you’ve sometimes wondered what is the right amount of tests to write. In some cases, it’s easy to end up with more tests than code. [...] When thinking of how many tests is enough, we need to think of what the goals are – both for the tests and our actual project.

He focuses in on this last idea, talking more about the SQLite project and its test suite. He then helps answer the main question - how do you know how many tests are enough? Should you "bend over backwards" to make tests for every possible scenario just because you can? He suggests a few things that can help the situation including refactoring where testing is difficult and writing regression tests for bugs fixed.

tagged: testing code opinion toomany unittest sqlite project

Link: http://codeutopia.net/blog/2016/09/10/how-many-tests-is-too-many/

Toptal.com:
The Six Commandments of Good Code: Write Code that Stands the Test of Time
Sep 09, 2016 @ 09:50:44

On the Toptal.com site they've posted a guide that aims to help you write good code that stands the test of time. They provide six "commandments" that they think can help make your code better and easier to maintain in the future.

Specifically, “good code” is code that is easily and readily maintainable by an organization (not just by its author!) and will live for longer than just the sprint it was written in. The following are some things I’ve discovered in my career as an engineer at big companies and small, in the USA and abroad, that seem to correlate with maintainable, “good” software.

Their list includes suggestions like:

  • Treat Your Code the Way You Want Other’s Code to Treat You
  • Good Code Doesn’t Reinvent the Wheel, it Stands on the Shoulders of Giants
  • Don’t Cross the Streams!
  • When Possible, Let the Computer Do the Work

Each item on the list comes with a brief description with a bit more detail and how to apply it to your development. It's not focused on any one language, however, so there's no code samples here - just links to other resources and tools that can help in their application.

tagged: good code commandments better maintenance

Link: https://www.toptal.com/software/six-commandments-of-good-code

Master Zend Framework:
Preparing Legacy Applications for PHP 7 with Phan
Sep 08, 2016 @ 12:07:42

The Master Zend Framework site has posted a new tutorial for those making the move in their legacy applications up to the world of PHP 7. In it Matthew Setter shows how to use phan, a static analysis tool, to locate issues that could cause breakage in the upgrade.

Unless you’ve been living under a rock these last 12 - 18 months, you will have heard about PHP 7; the latest version of PHP. Not only is it fast, by some reports it’s up to twice as fast as PHP 5.6, and far less memory hungry.

[...] Unfortunately, your application’s source code may not be 100% compatible with version 7. Upgrading may well leave you with a broken application and a set of unhappy customers. So before you go breaking your site in the interests of speed and being one of the cool kids, find out if your code’s compatible. How? By using a static code analyser, one which gives you all the changes you need to make on your code, so that you can upgrade with confidence.

He then shows how to install the Phan tool via Composer and execute it against your codebase. The result of his commands are a file that contains the issues found during the scan. If there are some "false positives" you'd like to ignore he also shows you how to create a config.php file with these and other values set to make it all more reusable.

tagged: php7 upgrade phan detect static analysis code tutorial

Link: http://www.masterzendframework.com/preparing-for-php7-with-phan/