PHP - static type checking a-la PhpStorm
Asked Answered
S

3

16

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.

Spicate answered 27/1, 2014 at 22:3 Comment(9)
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?Dumpish
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.Ghirlandaio
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... ;)Ghirlandaio
@Ghirlandaio Jenkins uses testing tools as CodeSniffer and other tools. It does not have its testing toolsThyme
@Thyme Exactly. Isnt that what he wants?Ghirlandaio
@Ghirlandaio - 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)Thyme
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.Ghirlandaio
@Ghirlandaio - you are right, I have changes my answerSpicate
I was edited my answer, and added the answer for your changed questionThyme
R
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.

Revolutionize answered 6/2, 2014 at 3:7 Comment(1)
The real player here is Scrutinizer (first link) but unfortunately it is not open source :(Asafoetida
T
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.

Thyme answered 30/1, 2014 at 9:24 Comment(2)
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.Presumptive
Yes, but author changed question after I have answeredThyme
U
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.

Upchurch answered 14/12, 2022 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.