mybb / mybb2

The repository for the MyBB 2 forum software. Not to be used on live boards.
https://www.mybb.com
BSD 3-Clause "New" or "Revised" License
112 stars 45 forks source link

Type Hinting #284

Open Azareal opened 7 years ago

Azareal commented 7 years ago

Type Hinting was one of the big features which landed in PHP7, I'm assuming you already have an idea as to what it is, but in short you can mark methods, functions, etc. up with little type hints as to what it will expect and the language will scream when you violate those constraints.

It should help to find cases where strange inputs are passed to a method / function, usually the sign of a bug. And it helps methods / functions to be self-documenting. I briefly mentioned it on Discord to @euantorano and he seemed supportive of the idea.

I'm working on a pull request for this. Type hinting is actually already used in MyBB2, albeit rarely. My upcoming pull request will help to greatly expand the coverage. #286

ghost commented 7 years ago

Type Hinting was already in PHP5, just with PHP7 you can now hint scalar types and declare return types.

Azareal commented 7 years ago

I know, but type-hints are practically useless before PHP7.

ghost commented 7 years ago

Maybe you've never type-hint callables or classes, but others have (like me) and that's a) useful and b) usable in PHP5.

Azareal commented 7 years ago

Considering that it's missing the most useful features, and it's barely usable, I could barely call that support. How many bugs are really going to be prevented with a few callables or classes? Perhaps, they might work for perfectionism's sake, but they're not that useful in practice.

With scalar type-hints, there are hundreds upon hundreds of bugs which will be caught and eradicated before they cause any damage in a gold release. I know this from experience, as many of the bugs I've dealt with in PHP involve scalars doing stupid things at stupid times. It's not perfect, but it's a big step forward.

Locking the type of a variable to stop it accidentally mutating would be another good step for PHP, but it's unknown when they'll do that.