Develop Typesafe WordPress Plugins and Themes

A world without type errors

Facebook’s Hack language has a lot of advantages over plain old PHP, but the most convincing one is probably the type system.  When you write code in Hack, the language will help you to avoid type errors in your software by being strict about your function parameter and return types. It’s like your coding buddy, who constantly looks above your shoulder and at some points says, “Nope, you can not do that. I think you meant this”.  But even tough I love Hack, this post is not about convincing you to use it, but I rather assume that you already know what Hack is, what its advantages are and –  that’s the bummer – that it is not compatible with WordPress. But fear not! In this post I will explain how we resolved this compatibility issue.

To make Hack compatible with PHP environments and specifically with WordPress, we have to translate our Hack code to PHP, also called transpiling. As you might have guessed, we need a “Hack to PHP transpiler”. So, at this point, I have good and bad news for you.
Let’s start with the good news: Facebook has built such a transpiler.
The bad news: Facebook has dropped maintenance of the transpiler project a while ago.
Since we were using this transpiler, we had to decide how to deal with this fact. We basically had three options:

  1. switch back to plain PHP,
  2. keep using the transpiler,
  3. write our own transpiler.

After having experienced the awesome workflow of using Hack to develop WordPress products and getting rid of all type errors in production, we threw option 1 out of the window. Option 2 cannot be a long term solution, since you will lock your development process on this particular version of the transpiler. Thus, we decided to use the available transpiler while working on our own transpiler in parallel. And today we are happy to announce that we open sourced our transpiler hack2php, so all you guys can utilize it and possibly contribute 🙂

 

Getting started

We use VSCode for our Hack development, because it is slick, runs on all operating systems and has a great Hack extension. In fact, you need to install this extension in your VSCode. This extension assumes that you have Hack installed on your machine, so make sure to install it.

The easiest way to start using hack2php is by cloning the example project:
git clone https://github.com/codeneric/example-hack2php-project
and installing hack2php there: hhvm composer.phar install
This example project has a src folder which contains an example Hack file index.php.  Now you can transpile it to PHP: ./vendor/bin/hack2php src/index.php

The transpiler will transpile:

<?hh //strict
function a(bool $b): string{
    return'hello world!';
}

to:

<?php //strict
function a($b){
    return'hello world!';
}

Known Issues

There are currently two main issues. Not all Hack features are covered and the transpiler is quite slow. However, the most relevant features are already available and the transpiler will throw an error if it encounters unknown Hack features, which it cannot transpile. Our highest priority is correctness, this is also why the system is quite slow.

However, both issues can be resolved and we hope for your contribution 🙂

Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt
0
0
0

Start typing and press Enter to search