In #318016: Type hint array parameters it was suggested to use type hinting in function signatures, i.e. function foo(array $form, stdClass $node). Doing this across the whole code base is a huge task. This issue addresses a subset of that.

This patch adds type hinting for all (I hope) occurrences of $node. I also includes the patch from #592572: Always pass nodes as objects that is currently a requirement for this to work.

I haven't investigated whether the patch has a negative effect on performance.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

c960657’s picture

FileSize
110.22 KB

Updated patch, because #592572: Always pass nodes as objects was just committed.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for slogging through all those functions.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

c960657’s picture

Status: Closed (fixed) » Needs review
FileSize
104.39 KB

Looks like this wasn't committed to CVS.

Here is a fresh reroll.

Dries’s picture

Status: Needs review » Fixed

Committed to CVS HEAD. Thanks!

chx’s picture

I am glad a 100+ kb patch saw adequate reviews and discussion.

moshe weitzman’s picture

chx - your moody sarcasm is not at all helpful. state your objection if you don't like this patch. subscribe to the RSS feed for CNR or RTBC if you you want to review pending patches.

dmitrig01’s picture

Status: Fixed » Needs work

I don't support this, because if someone wanted to override $node with a real class, now they can't because all the functions are expecting an stdClass. Arrays are fine but objects aren't for type hinting IMO (at least in most places)

chx’s picture

yeah. type hinting with an interface, awesome. with a class? uh oh.

chx’s picture

@moshe and if i subscribe then i magically will have the time? why me? noone said a word about this patch and it got in just like that past freeze. It's not enhancing DX significantly, it's not enhancing pretty much anything, what's the point?

c960657’s picture

Status: Needs work » Needs review
FileSize
107.89 KB

I'm not sure how you would replace $node with a real class, but you could make that class extend stdClass.

I think type hints are particularly relevant for Drupal, because of the many arrays that are passed around. If you by accident pass the wrong argument, the error message may be very subtle, because the real error often occurred several levels above it in the stack trace. Adding type hints doesn't completely solve this, but it helps in some cases.

However, here is a patch for backing out the change.

Crell’s picture

I don't know off hand if you can "extend" stdClass, or if all classes conform to stdClass. But I do know of one really crazy D5 project that added real classes for users, so there are certainly use cases.

OK, I just tested it and no, stdClass is not an implicit parent of all classes, and explicitly extending stdClass means that you cannot extend some other class. So this patch is a regression and should be rolled back.

As chx said, we should only type hint on interfaces or arrays, never classes. That's in the coding standards, too, although it's a recent addition: http://drupal.org/node/608152

Status: Needs review » Needs work

The last submitted patch failed testing.

Crell’s picture

Title: Use type hinting for $node » Rollback type hinting for $node
Priority: Normal » Critical

Renaming and bumping priority, as this is an API regression.

moshe weitzman’s picture

I'm fine with rollback if thats what people want though I have not heard of a single valid use case. "really crazy D5 project that added real classes for users, so there are certainly use cases". thats a non-sequitur for me.

Frando’s picture

Well, it won't be possible without hacking core but in D7 with PDO and entity loading you'd only need to change very few LOCs to fetch entities into an actual class instead of an stdClass upon loading (I tried this, it works). So use cases are much more likely than in D5 or D6. At least it offers room for improvisation regarding using actual classes for entities, which this patch makes impossible without applying a 100k patch first (that does not have any actual improvement of its own). Therefore, I support the rollback.

c960657’s picture

Status: Needs work » Needs review
FileSize
104.88 KB

Reroll.

Crell’s picture

Status: Needs review » Reviewed & tested by the community

Quick, before it breaks again. :-) (I didn't look at every line, but it looks like all its doing is taking stdClass back out of function signatures, which is correct.)

Dries’s picture

Welp, I was under the impression that stdClass was the parent class of all classes. Must be my Java background. I'm happy to rollback if that is the consensus.

chx’s picture

Yes that's the consensus :)

Crell’s picture

chx and I agree on something related to PHP 5 OO features. I don't think you can get a better sign that this should be committed. :-)

Status: Reviewed & tested by the community » Needs work

The last submitted patch failed testing.

bcn’s picture

Status: Needs work » Needs review
FileSize
56.54 KB

reroll for changes in head...

moshe weitzman’s picture

Status: Needs review » Needs work

latest patch is half the size of the prior ones. probably not intended.

chx’s picture

Status: Needs work » Needs review
FileSize
58.27 KB
chx’s picture

The 100K version had was likely rolled with -c 9 or something, look at it, there are 9 lines instead of 3 around the changes.

chx’s picture

Here is another reroll. I have checked that it contains only function header changes:

chx@veyron:/var/www/drupal$ bzr diff > why_does_every_sort_of_crap_get_committed_to_drupal_7.patch
chx@veyron:/var/www/drupal$ grep '^- ' why_does_every_sort_of_crap_get_committed_to_drupal_7.patch |grep -v function
chx@veyron:/var/www/drupal$ grep '^+ ' why_does_every_sort_of_crap_get_committed_to_drupal_7.patch |grep -v function
chx@veyron:/var/www/drupal$
sun’s picture

Status: Needs review » Reviewed & tested by the community

Looks good.

As chx mentioned, the difference comes solely from the increased diff context. And it's visible by reviewing it. Interesting.

Although this patch will most likely break many others of mine in the queue right now, I support it.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed #28 to HEAD.

Pasqualle’s picture

Title: Rollback type hinting for $node » type hinting for $node
Priority: Critical » Normal
Status: Fixed » Active

does not have any actual improvement of its own

That is not true. The improvement is that you can't pass an array for the $node function parameter. $node is an object.
In Drupal we (not me, someone else) had a tendency to allow all kind of variable types for the same function parameter. And not even allowing different types but giving different meanings for the same parameter:
http://api.drupal.org/api/function/node_load/6
http://api.drupal.org/api/function/hook_nodeapi/6
That is just insane, and should be fixed everywhere as it generates developer errors. It is simply a wrong code style which should be fixed and forgotten.

So is it possible to make the $node an object interface (in Drupal 8,9,10) and then use type hinting? I would not close this issue yet..

Crell’s picture

Title: type hinting for $node » Rollback type hinting for $node
Status: Active » Fixed

If we make a proper Interface out of nodes in Drupal 8, sure. We deal with that then. For now, $nodes are still anonymous objects and that's not changing for Drupal 7, so this issue remains fixed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Dave Reid’s picture

Hrm, I don't get why this was rolled back without thinking the same logic could be applied to the *lot* of other places where stdClass is used as a typehint (see most of file.inc).

I hate PHP for not allowing a general 'object' type hint...