I am a strongly-typed language supporter but I am working on a big PHP project.
I have been using PhpStorm and I love the extra type hinting you can provide, with comments like these:
/** @var \Payments $payment */
/** @property \Payments $payment */
PhpStorm is great because it gives you some warning when types don't match.
I was wondering if it is possible to have this kind of check also statically, outside of PhpStorm. Some kind of command line precompiler that would go through the code, checking also those extra hints, to show some error if a mismatch is detected.
Is that something that can be done with phpcs?
My ultimate goal is to have an automated tool that will alert me for type mismatches.
I was even considering switching to HHVM to have far better type hinting. But I don't trust HHVM yet to run on production.
Ideally it would be great to develop on HHVM for the extra type hinting but the code wouldn't run on the Zend PHP engine that I want to keep on production.
Any idea?
Thanks.
For static code analysis, specifically helping with type error detection, one can use tools like
PHPCS aka PHP_CodeSniffer is Coding Standard checker, not really helpful with type error detection.
Phantm
If you want to check your code for type mismatches you can use Phantm. You can use this with Jenkins or use with svn hook post-commit.
From official site
Phantm is a tool written in Scala that can be used to detect potential programming errors inside PHP applications. It will both work for small isolated script as well as full-blown applications. phantm stands for “PHp ANalyzer for Type Mistakes”.
Phantm needs
ant
sbt
Java 1.6 or higher
Scala 2.9.1 or higher
Usage
$ ./phantm <target.php>
PHPLint
Another way for checks your php code for type mismatches is PHPLint. You can use it with Jenkins, svn or another tools.
From official site
PHPLint is a validator and documentator for PHP 4 and PHP 5 programs. PHPLint extends the PHP language through transparent meta-code that can drive the parser to a even more strict check of the source. PHPLint is not simply a checker: it implements a new, strong typed, language implemented over the PHP language. You can build your programs from scratch with PHPLint in mind, or you can check and fix existing programs, or you can follow the quick-and-dirty PHP programming way and then add the PHPLint meta-code later once the program is finished. Whatever is the strategy you choose, PHPLint makes your programs safer, more secure, well documented and with drastically less bugs. PHPlint have nice online test tool.
More information about working with types by PHPLint
Usage
$ phplint <target.php>
If you want to check your variables types using PHPStorm, you can use PHPDoc metadata. But much better use php for this: you can enable E_NOTICE for report uninitialized variables for catch variable name misspellings. After that you can use instance check at your functions:
/**
* @param array $data - highlight for PHPStorm
* @param MySuperClass $row - highlight for PHPStorm
*/
function(array $data, MySuperClass $row)
{
// Error if $data is not an array
// Error if $row is not instance of MySuperClass
/* do something .... */
}
/**
* @param array $data - highlight for PHPStorm
* @param MySuperClass $row - highlight for PHPStorm
*/
function($data, $row)
{
if (!is_array($data))
{
throw new TypeException('$data is must be array');
}
if (!($row instanceof MySuperClass))
{
throw new TypeException('$data is must be array');
}
}
I think is the best way for type checking, when developer is writing correct code using TDD method. Very good solution you can read at official php documentation or at PHPUnit official.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With