Comments

tstoeckler’s picture

Issue tags: +Novice

Now that the philosophical debate has been decided as you point out, I think this is a Novice issue:
To-do list:
1. Go into core/modules/comment
2. Check all of the containing files for functions which receive $comment as a parameter
3. Add the type-hint to the parameter, i.e.:

// Before
function comment_confirm_delete($form, &$form_state, $comment) {

// After
function comment_confirm_delete($form, &$form_state, Comment $comment) {
underq’s picture

Status: Active » Needs review
StatusFileSize
new12.05 KB

I tried to do a patch for this without forgot any $comment ;)

Status: Needs review » Needs work

The last submitted patch, comment-add_type_hinting-1480866-2.patch, failed testing.

underq’s picture

Status: Needs work » Needs review
StatusFileSize
new12.85 KB

I try new patch ;)

Status: Needs review » Needs work

The last submitted patch, comment-add_type_hinting-1480866-4.patch, failed testing.

hansyg’s picture

Here's another shot at this patch

hansyg’s picture

Better when its not empty ;)

hansyg’s picture

Status: Needs work » Needs review

needs review

Status: Needs review » Needs work

The last submitted patch, comment-add_type_hinting-1480866-5727526.patch, failed testing.

tstoeckler’s picture

+++ b/core/modules/comment/comment.test
@@ -104,7 +104,7 @@ class CommentHelperCase extends DrupalWebTestCase {
+  function commentExists(Comment $comment, $reply = FALSE) {
     if ($comment && is_object($comment)) {

This should be:

function commentExists(Comment $comment = NULL, $reply = FALSE) {
  if ($comment) {
...

I.e. the argument should be optional and the is_object() can be removed.

Otherwise looks good. :)

EDIT: Added the type-hint in my example code. *slapsforehead*

hansyg’s picture

Status: Needs work » Needs review
StatusFileSize
new6.97 KB

Made the above changes and rerolled the patch, thanks for the help and review @tstoeckler

lars toomre’s picture

Shouldn't type hinting also be done for the variables in the @param and @return directives in the docblocks? That way the documentation will indicate the expected type of $comment variable.

fago’s picture

Status: Needs review » Needs work

Good progress!

yep, we also should add the class the do

+++ b/core/modules/comment/comment.api.php
@@ -19,7 +19,7 @@
  * @param $comment
  *   The comment object.

Should be...

+++ b/core/modules/comment/comment.api.php
@@ -19,7 +19,7 @@
  * @param Comment $comment
  *   The comment entity.
hansyg’s picture

Status: Needs work » Needs review
StatusFileSize
new9.19 KB

Added to the docblocks

klausi’s picture

Status: Needs review » Needs work
+++ b/core/modules/comment/comment.api.php
@@ -49,10 +49,10 @@ function hook_comment_update($comment) {
- * @param $comments
+ * @param Comment $comments
  *  An array of comment objects indexed by cid.
  */
-function hook_comment_load($comments) {
+function hook_comment_load(Comment $comments) {

This does not make sense. We have an array for $comments here.

+++ b/core/modules/comment/comment.module
@@ -1467,10 +1467,10 @@ function comment_access($op, $comment) {
- * @param $comment
+ * @param Comment $comment
  *   A comment object.

should be "A comment entity.". Please check all occurences of "comment object".

pounard’s picture

There is no need to write "A comment object" nor a "A comment entity" here since the type hinting is self documenting. Just put the "@param Comment $comment" with no additional information is enough.

tstoeckler’s picture

Re #16. That is incorrect. We have a standard that each parameter needs a description. If you disagree with that standard please open a new issue.

lars toomre’s picture

The docblocks in comment.module seem to vary from documentation standards. For instance, comment_submit() is missing a @param directive and description. That needs to be added here. Additionally, when adding type hinting to one variable in a docblock, please do so for all of the @param variables.

fago’s picture

+++ b/core/modules/comment/comment.test
@@ -96,7 +96,7 @@ class CommentHelperCase extends DrupalWebTestCase {
-   * @param object $comment
+   * @param object Comment $comment

objects needs to be removed.

+++ b/core/modules/comment/comment.test
@@ -123,10 +123,10 @@ class CommentHelperCase extends DrupalWebTestCase {
-   * @param object $comment
+   * @param object Comment $comment

objects needs to be removed.

hansyg’s picture

Rerolled the patch with #19 @fago

hansyg’s picture

needs review

lars toomre’s picture

+++ b/core/modules/comment/comment.api.phpundefined
@@ -49,10 +49,10 @@ function hook_comment_update($comment) {
+ * @param Comment $comments
  *  An array of comment objects indexed by cid.

This does not make sense. It should be @param array $comments.

+++ b/core/modules/comment/comment.moduleundefined
@@ -1889,7 +1889,7 @@ function comment_form_build_preview($form, &$form_state) {
  *
  * @see comment_form_build_preview()
  */
-function comment_preview($comment) {

No @param directive here?? Appears @param Comment $comment is missing.

+++ b/core/modules/comment/comment.moduleundefined
@@ -1987,7 +1987,7 @@ function comment_form_validate($form, &$form_state) {
 /**
  * Prepare a comment for submission.
  */
-function comment_submit($comment) {

Ditto.

+++ b/core/modules/comment/comment.moduleundefined
@@ -2435,7 +2435,7 @@ function comment_unpublish_by_keyword_action_submit($form, $form_state) {
  *
  * @ingroup actions
  */
-function comment_save_action($comment) {

Ditto.

hansyg’s picture

Status: Needs work » Needs review
StatusFileSize
new9.18 KB

Changes from #22

klausi’s picture

Status: Needs review » Needs work

Thanks for working on this!

+++ b/core/modules/comment/comment.api.php
@@ -49,10 +49,10 @@ function hook_comment_update($comment) {
- * @param $comments
+ * @param Comment $comments
  *  An array of comment objects indexed by cid.
  */
-function hook_comment_load($comments) {
+function hook_comment_load(Comment $comments) {

this is still wrong.

hansyg’s picture

Status: Needs work » Needs review
StatusFileSize
new9.35 KB

Whoops, attached wrong patch. Corrected

aspilicious’s picture

Status: Needs review » Needs work
+++ b/core/modules/comment/comment.api.phpundefined
@@ -49,10 +49,10 @@ function hook_comment_update($comment) {
- * @param $comments
+ * @param array $comments
  *  An array of comment objects indexed by cid.
  */
-function hook_comment_load($comments) {
+function hook_comment_load(Comment $comments) {
   $result = db_query('SELECT cid, foo FROM {mytable} WHERE cid IN (:cids)', array(':cids' => array_keys($comments)));

This is impossible. First it's an array than a Comment object.
This should brake the testbot somehow... So we aren't testing this?

+++ b/core/modules/comment/comment.moduleundefined
@@ -1887,9 +1887,11 @@ function comment_form_build_preview($form, &$form_state) {
+ * @param Comment $comment
+ * ¶
  * @see comment_form_build_preview()

Needs a line of explanation and has a trailing whitespace

+++ b/core/modules/comment/comment.moduleundefined
@@ -2432,10 +2437,12 @@ function comment_unpublish_by_keyword_action_submit($form, $form_state) {
+ * ¶
+ * @param Comment $comment
  *

Same

pounard’s picture

Why would it need an explaination, would you write "A comment object." ? Is "Comment $comment" not self-descripting enough?

That said, you're right about first points, it's an array not a Comment instance.

aspilicious’s picture

Because it's our standard...

pounard’s picture

This particular piece of standard is about documentation, and this particular piece of documentation isn't needed anymore. I think in this case the standard doesn't fit the need anymore and must be revisited.

xjm’s picture

Regarding #29: Each @param does need a description. Reference: http://drupal.org/node/1354#param-return-data-type

I'd agree that this:

@param Comment $comment
  A Comment object.

is redundant and not so useful. However, instead of removing the parameter description, we should make it more useful and provide detail about the parameter. And, having a human-readable line explaining each parameter makes it easier to understand in the API docs.

That said, it's off-topic for this issue. We should follow the current standard as long as it is the standard. I suggested to pounard in IRC to open a separate coding standards issue to consider whether the standard should be changed if he thinks it should be.

pounard’s picture

Yes sorry for that, opened #1512338: Revisit Coding Standard about parameters documentation for discussion.

hansyg’s picture

Status: Needs work » Needs review
StatusFileSize
new9.34 KB

I removed the extra whitespace from #26, I'd be happy to re-roll with additional changes not sure what needs to happen though.

aspilicious’s picture

Status: Needs review » Needs work

See #26, Comment should be array

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new9.34 KB

Re-rolled the patch with the things mentioned in #26 fixed.

By the way, Netbeans 7.2 has support for @param Comment[] $comments, which is way nicer than @param array $comments but that's something for another issue and day to look at ;)

@aspilicious: This can not be covered by tests as the hook example implementations are dead code, they are never executed and just serve as an example.

tstoeckler’s picture

Status: Needs review » Needs work

Darn it, found another one:

+++ b/core/modules/comment/comment.api.php
@@ -49,10 +49,10 @@ function hook_comment_update($comment) {
+function hook_comment_load(Comment $comments) {

That should also be "array" instead of "Comment". (Or nothing, I've seen us type-hinting arrays elsewhere but I'm not sure it is a standard. Can't hurt though, I guess.)

berdir’s picture

StatusFileSize
new9.42 KB

Well, it would help if I would not re-upload the old patch but my own.

Head -> Keyboard.

berdir’s picture

Status: Needs work » Needs review
cosmicdreams’s picture

StatusFileSize
new14.24 KB

This patch adds additional type hinting to:
comment.module

  • comment_uri
  • comment_view
  • comment_build_content
  • comment_links
  • comment_access
  • comment_save
  • comment_publish_action
  • comment_unpublish_action
  • comment_unpublish_by_keyword_action
  • Also, there seems to be some function in comment.test that define a Comment object as a generic object. Is that right?

Status: Needs review » Needs work

The last submitted patch, 1480866_38_comment_type_hint.patch, failed testing.

berdir’s picture

Looks like there are some cases in comment.module where we're not loading comments as entities, for example:

 $comment = db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.signature_format, u.picture, u.data FROM {comment} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = :cid AND c.status = :status', array(
          ':cid' => $pid,
          ':status' => COMMENT_PUBLISHED,
        ))->fetchObject();
        if ($comment) 

That should be changed I guess but probably not in this issue.

Edit: Given that we have entity caching, I guess it's actually better and much simpler to do a simple comment_load($pid) and *then* check the status in the condition.

fago’s picture

Why not just fix remaining problems in this issue? Tests probably won't pass anymore without having all uses fixed anyway.

@example from #40:
Looks like we could convert this directly to an entity_load() call making use of a condition for status, i.e.

$comments = comment_load_multiple(FALSE, array('pid' => $pid, 'status' => COMMENT_PUBLISHED);
$comment = reset($comments);
aspilicious’s picture

fago isn't there a way to NOT write two lines for a single load? Do we have to go through comment_load_multiple?

berdir’s picture

As I said, I'd suggest this:

$comment = comment_load($pid);
if ($comment->status == COMMENT_PUBLISHED) {

}

Using $conditions is deprecated and removes the possibility of getting the entity from cache and the status check is only a sanity check to not unpublish unpublished comments, so...

fago’s picture

fago isn't there a way to NOT write two lines for a single load? Do we have to go through comment_load_multiple?

Not when using conditions.. But, indeed #43 looks clean. (Actually, my example was wrong as we need to load the parent comment.)

Using $conditions is deprecated and removes the possibility of getting the entity from cache and the status check is only a sanity check to not unpublish unpublished comments, so...

Yep. #43++

tstoeckler’s picture

Assigned: Unassigned » tstoeckler

I'll try to finish this one off now.

tstoeckler’s picture

Assigned: tstoeckler » Unassigned
Status: Needs work » Needs review
StatusFileSize
new15.4 KB

Well, that wasn't too hard. The code mentioned above in comment.pages.inc was responsible for most of those failures.
All previously failing tests pass locally now.

xjm’s picture

Title: Add type-hinting to comments » Add type-hinting and parameter type docmentation for comment objects
+++ b/core/modules/comment/comment.pages.incundefined
@@ -47,12 +47,8 @@ function comment_reply($node, $pid = NULL) {
+        $comment = comment_load($pid);
+        if ($comment->status = COMMENT_PUBLISHED) {

Perhaps an inline comment here, e.g. "Load the parent comment."?

Edit: I take back what I said about scope; I previously failed to understand that this change is needed for tests to pass.

berdir’s picture

That is necessary to make the type hinting for comment_unpublish_action() work. Adding the type hint showed that we are in fact not passing an actual comment entity to that function.

That said, I initially suggested to do that in a follow-up issue as well. Just trying to explain why it was done.

tstoeckler’s picture

StatusFileSize
new15.43 KB

Added a comment per #47.

xjm’s picture

Status: Needs review » Reviewed & tested by the community

Alright, assuming #49 passes the bot, I think this is ready. Thanks @Berdir and @tstoeckler.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Thanks! Committed/pushed to 8.x.

adamdicarlo’s picture

Status: Fixed » Needs review
StatusFileSize
new1.05 KB

Pretty sure this is a bug:

+        if ($comment->status = COMMENT_PUBLISHED) {

Patch attached.

berdir’s picture

Issue tags: +Needs tests

D'oh.

I guess that means we don't have tests for this :)

tstoeckler’s picture

Oops, dammit, that was me. Sorry.
Will write a test for this tomorrow, if no one beats me to it.

berdir’s picture

xjm’s picture

Still need the test coverage here, no?

tstoeckler’s picture

StatusFileSize
new1.64 KB
new973 bytes

Yeah, I had totally forgotten about that, sorry.
Here we go.

aspilicious’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me...

xjm’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/comment/comment.testundefined
@@ -382,6 +382,12 @@ class CommentInterfaceTest extends CommentHelperCase {
+    $this->assertText('The comment you are replying to does not exist.', t('Replying to an unpublished comment'));

The use of t() here is inverted. The first string is translated in comment.module, so we need to use t() when we check for it. And the t() on the assertion message is not needed, though with assertText() I'd recommend just omitting that parameter. (The default message is useful.)

Reference: http://drupal.org/simpletest-tutorial-drupal7#t

This is a minor cleanup, so another good novice task. When you create a new patch, be sure to upload both a test-only version and the complete patch, as in #57.

tstoeckler’s picture

Status: Needs work » Reviewed & tested by the community

I adjusted the message so it is inline with the surrounding messages. I would suggest doing the clean-up mentioned in #59 in a separate issue. If we were to change this patch the resulting code would look pretty awkward, IMO.

berdir’s picture

Status: Reviewed & tested by the community » Needs work

I went through that before as well, changed lines need to have a proper coding standard, doesn't matter if surrounding lines are wrong.

xjm’s picture

Also, the first problem (no t() on the text we are testing for) is a straight-up bug. I would have just rerolled it myself otherwise.

tstoeckler’s picture

no t() on the text we are testing for) is a straight-up bug

How is that? We know the language of the site we are testing. So we might as well test for the exact string as it appears on the site.

sheise’s picture

Status: Needs work » Needs review
StatusFileSize
new971 bytes
new1.63 KB

Here are updated patches from #57 with the changes from #59.

Status: Needs review » Needs work

The last submitted patch, 1480866-64-tests-only.patch, failed testing.

aspilicious’s picture

Status: Needs work » Reviewed & tested by the community

Actually this is rtbc (first in #64)

catch’s picture

Status: Reviewed & tested by the community » Fixed

Thanks. Committed/pushed to 8.x.

Status: Fixed » Closed (fixed)

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

yched’s picture