15

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.

Dan
  • 15,948
  • 20
  • 63
  • 92
  • PHPStorm usually alerts you **if** it knows there is a type mismatch. So if you have a function `/** @returns int */ getInt()`, and you say `/** @var boolean $boolean */ $boolean = getInt()` there would be an error since PHPStorm `getInt()` returns an integer not a boolean. Anything more intelligent would probably need to be able to run the PHP (not pre-compiled), so a debugging suite like PHPUnit or xDebug may help? – Sam Jan 27 '14 at 22:14
  • 2
    Did you consider using a Build Server like Jenkins? You could automate deployment to your target servers there and make it check unit tests and all kinds of syntax and code style tools before allowing deployment. All of this can be done automatically after any SVN commit. – ToBe Jan 30 '14 at 09:41
  • 3
    Seems like noone yet understood your question. Am I correct that you are using for an automated check on types as PHPStorm does via phpdoc comments? You are NOT searching for info on how to create code using types or how to make PHPStorm do that? IF so, you might need to clarify your question as current answers got that wrong... ;) – ToBe Jan 30 '14 at 09:44
  • @ToBe Jenkins uses testing tools as CodeSniffer and other tools. It does not have its testing tools – Dmitriy.Net Jan 30 '14 at 10:06
  • @Dmitriy.Net Exactly. Isnt that what he wants? – ToBe Jan 30 '14 at 10:09
  • @ToBe - I think he wants automatic type hinting, and jenkins is one way from many others )) I'm not critics you, i'm added your answer) – Dmitriy.Net Jan 30 '14 at 10:21
  • Oh, i thought he wanted automatic checking of the type hinting he already has using his phpdoc comment. I think im out of here. Needs clarification i guess. – ToBe Jan 30 '14 at 10:23
  • @ToBe - you are right, I have changes my answer – Dan Jan 30 '14 at 14:39
  • I was edited my answer, and added the answer for your changed question – Dmitriy.Net Jan 31 '14 at 15:23

3 Answers3

10

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.

Michal Brašna
  • 2,293
  • 13
  • 17
5

Automatic check for type mismatches

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>

Manual check for type mismatches

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.

Dmitriy.Net
  • 1,476
  • 13
  • 24
  • 1
    wow, this answer... He is asking for a way to automate this with phpcs or similar. and your answer is write "correct code" using TDD. Here are my proving references to "php doc" and "php unit" for a final nail in irrelavancy coffin. I totally agree with you but I'd also suggest him to walk around the developers with a spiked club and hit them hard when they don't follow Dmitry.Net coding standarts. Because that is just easier than a command line script automation. – kali Jan 31 '14 at 10:46
  • Yes, but author changed question after I have answered – Dmitriy.Net Jan 31 '14 at 11:07
0

You can use Psalm, a free & open-source static analysis tool for PHP: https://psalm.dev/

It can be run from the command line, and reports type errors.

andraaspar
  • 796
  • 6
  • 10