Pahout — A pair programming partner for writing better PHP

Kazuma Watanabe
Aug 26, 2017 · 3 min read

Pair-programming is a good approach for developing better software. For example, do you know about “multiple-catch” entered in PHP 7.1?

<?phptry {
foo();
} catch (FooException $e) {
// handling exception
} catch (BarException $e) {
// handling exception (Redundant action...)
}

The above code was common sense up to PHP 7.0. If you can use “multipe-catch”, you can write:

<?phptry {
foo();
} catch (FooException | BarException $e) {
// handling exception (Great!)
}

Even if you do not know this fact, you can learn from pair-programming. However, pair-programming requires a good partner. This was a serious problem.

In order to solve this problem, I developed a linter named Pahout.

Pahout inspects your PHP files automatically. it detects the following common patterns and tells you how to fix it.

Redundant Ternary Operator

For example, I think you have seen the following code:

<?php$config = get_config();
$config = $config ? $config : default_config();
$ignore = isset($config['ignore']) ? $config['ignore'] : [];

The above code has redundant ternary operators. Pahout gives the following hints.

$ pahout test.php
test.php:3
ElvisOperator: Use elvis operator instead of ternary operator. [https://github.com/wata727/pahout/blob/master/docs/ElvisOperator.md]
test.php:4
NullCoalescingOperator: Use null coalecing operator instead of ternary operator. [https://github.com/wata727/pahout/blob/master/docs/NullCoalescingOperator.md]
1 files checked, 2 hints detected.

Here you have learned the two PHP features, the “elvis operator” and the “null coalescing operator”. With these features you can rewrite more smartly.

<?php$config = get_config();
$config = $config ?: default_config();
$ignore = $config['ignore'] ?? [];

Long Array Syntax

Do you know that array declaration can be done in several ways in PHP? What is common the declaration as following:

<?php$array = array(1, 2, 3);

However, in PHP 5.6 and later, “short array syntax” is available. This one is smarter.

<?php$array = [1, 2, 3];

Also, in PHP 7.1, “short array syntax” can be used instead of list() function.

<?phplist($a, $b) = get_array(); // This is not a bit pretty...
[$a, $b] = get_array(); // Elegant!

Of course, there are also included in Pahout’s hints.

$ pahout test.php
test.php:3
ShortArraySyntax: Use [...] syntax instead of array(...) syntax. [https://github.com/wata727/pahout/blob/master/docs/ShortArraySyntax.md]
test.php:4
SymmetricArrayDestructuring: Use [...] syntax instead of list(...) syntax. [https://github.com/wata727/pahout/blob/master/docs/SymmetricArrayDestructuring.md]
1 files checked, 2 hints detected.

Weak Functions

PHP has functions that should not be used due to the length of its history. For example, there is no reason to use crypt() function now.

<?php$hash = crypt('secret text', generate_salt());

The password_hash() function introduced in PHP 5.5 is an excellent wrapper for the crypt() function.

<?php$hash = password_hash('secret text', PASSWORD_DEFAULT);

Pahout also tells us the use of these functions.

$ pahout test.php
test.php:2
PasswordHash: Use of `password_hash()` is encouraged. [https://github.com/wata727/pahout/blob/master/docs/PasswordHash.md]
1 files checked, 1 hints detected.

Conclusion

The example given above is only a part. Do you think Pahout that gives such hints is convenient? I would like to increase more effective hints.

Unfortunately, There are not many hints yet… If you have a valuable idea, please make an issue on GitHub. I hope that everyone’s review knowledge will make it easier for everyone to use it.

)

Kazuma Watanabe

Written by

Software engineer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade