Follow-up to #2572649: Fix 'Drupal.Commenting.HookComment' coding standard

This was not really fixed... there is well over 100+ in core... :( and they have not been introduced since. Maybe the sniff has changed. We need a followup issue to fix the rest of these and remove the exclusion from phpcs.xml.dist

Part of #2571965: [meta] Fix PHP coding standards in core.

Approach

We are testing coding standards with PHP CodeSniffer, using the Drupal coding standards from the Coder module. Both of these packages are not installed in Drupal core. We need to do a couple of steps in order to download and configure them so we can run a coding standards check.

Step 1: Add the coding standard to the whitelist

Every coding standard is identified by a "sniff". For example, an imaginary coding standard that would require all llamas to be placed inside a square bracket fence would be called the "Drupal.AnimalControlStructure.BracketedFence sniff". There are dozens of such coding standards, and to make the work easier we have started by only whitelisting the sniffs that pass. For the moment all coding standards that are not yet fixed are simply skipped during the test.

Open the file core/phpcs.xml.dist and add a line for the sniff of this ticket. The sniff name is in the issue title. Make sure your patch will include the addition of this line.

Step 2: Install PHP CodeSniffer and the ruleset from the Coder module

Both of these packages are not installed by default in Drupal core, so we need to download them. This can be done with Composer, from the root folder of your Drupal installation:

$ composer require drupal/coder squizlabs/php_codesniffer
$ ./vendor/bin/phpcs --config-set installed_paths ../../drupal/coder/coder_sniffer

Once you have installed the phpcs package, you can list all the sniffs available to you like this:

$ ./vendor/bin/phpcs --standard=Drupal -e

This will give you a big list of sniffs, and the Drupal-based ones should be present.

Step 3: Prepare the phpcs.xml file

To speed up the testing you should make a copy of the file phpcs.xml.dist (in the core/ folder) and save it as phpcs.xml. This is the configuration file for PHP CodeSniffer.

We only want this phpcs.xml file to specify the sniff we're interested in. So we need to remove all the rule items, and add only our own sniff's rule. Rule items look like this:

<rule ref="Drupal.Classes.UnusedUseStatement"/>

Remove all of them, and add only the sniff from this issue title. This will make sure that our tests run quickly, and are not going to contain any output from unrelated sniffs.

Step 4: Run the test

Now you are ready to run the test! From within the core/ folder, run the following command to launch the test:

$ cd core/
$ ../vendor/bin/phpcs -p

This takes a couple of minutes. The -p flag shows the progress, so you have a bunch of nice dots to look at while it is running.

Step 5: Fix the failures

When the test is complete it will present you a list of all the files that contain violations of your sniff, and the line numbers where the violations occur. You could fix all of these manually, but thankfully phpcbf can fix many of them. You can call phpcbf like this:

$ ../vendor/bin/phpcbf

This will fix the errors in place. You can then make a diff of the changes using git. You can also re-run the test with phpcs and determine if that fixed all of them.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

attiks created an issue. See original summary.

attiks’s picture

Status: Closed (fixed) » Active
attiks’s picture

Sad thing is that there's no phpcf for this

attiks’s picture

Assigned: Unassigned » attiks
FileSize
96.02 KB

Attached the output

attiks’s picture

Status: Active » Needs review
FileSize
82.45 KB

Patch created using grep -r -l Implements * | xargs sed -i 's/^\( \* Implements [^(].*\)().*/\1()./g'

attiks’s picture

Assigned: attiks » Unassigned
FileSize
86.13 KB
4.83 KB

Fix remaining once manually

Status: Needs review » Needs work

The last submitted patch, 6: i2623718-6.patch, failed testing.

attiks’s picture

Status: Needs work » Needs review
FileSize
86.13 KB

Patch rebased

pfrenssen’s picture

Issue summary: View changes

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

pfrenssen’s picture

Issue summary: View changes
pfrenssen’s picture

Issue summary: View changes
pfrenssen’s picture

Issue summary: View changes
alexpott’s picture

Status: Needs review » Needs work

We've changed the way we do phpcs.xml.dist - its inclusive not exclusive.

alexpott’s picture

+++ b/core/modules/locale/locale.module
@@ -219,7 +219,7 @@ function locale_configurable_language_update(ConfigurableLanguageInterface $lang
- * Implements hook_ENTITY_TYPE_delete() for 'configurable_language'.
+ * Implements hook_ENTITY_TYPE_delete().

+++ b/core/modules/node/node.module
@@ -527,7 +527,7 @@ function node_preprocess_html(&$variables) {
- * Implements hook_preprocess_HOOK() for block templates.
+ * Implements hook_preprocess_HOOK().

@@ -1321,7 +1321,7 @@ function node_reindex_node_search($nid) {
- * Implements hook_ENTITY_TYPE_insert() for comment entities.
+ * Implements hook_ENTITY_TYPE_insert().

I'm not sure about these changes. I think we should update the rule to permit this.

dawehner’s picture

Issue tags: +Novice

AT least #15 is a good novice task.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ZeiP’s picture

The change in #15 has been submitted as issue #2821834: Loosen the Drupal.Commenting.HookComment sniff per #2623718, is that ok?

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Version: 8.4.x-dev » 8.5.x-dev

About #15 from core/vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/HookCommentSniff.php:92
Format should be "* Implements hook_foo().", "* Implements hook_foo_BAR_ID_bar() for xyz_bar().",, "* Implements hook_foo_BAR_ID_bar() for xyz-bar.html.twig.", "* Implements hook_foo_BAR_ID_bar() for xyz-bar.tpl.php.", or "* Implements hook_foo_BAR_ID_bar() for block templates."

andypost’s picture

Assigned: Unassigned » andypost
Status: Needs work » Needs review
Issue tags: -Novice
FileSize
737 bytes

This patch needs rework, here's a patch to check state

Locally

core$ ../vendor/bin/phpcs -sp --sniffs=Drupal.Commenting.HookComment --report=source .
...
PHP CODE SNIFFER VIOLATION SOURCE SUMMARY
----------------------------------------------------------------------
SOURCE                                                           COUNT
----------------------------------------------------------------------
Internal.NoCodeFound                                             279
Internal.LineEndings.Mixed                                       54
Drupal.Commenting.HookComment                                    14
Drupal.Commenting.HookComment.HookParamDoc                       4
Drupal.Commenting.HookComment.HookReturnDoc                      1
----------------------------------------------------------------------
A TOTAL OF 352 SNIFF VIOLATIONS WERE FOUND IN 5 SOURCES
----------------------------------------------------------------------
andypost’s picture

Assigned: andypost » Unassigned
FileSize
12.03 KB

Totally new patch, looks everything fixed

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

borisson_’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

This patch no longer applies, but all the changes in it look really solid. Setting to needs work so someone can reroll this.

MerryHamster’s picture

Here reroll for 8.6.x

MerryHamster’s picture

Status: Needs work » Needs review
borisson_’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs reroll

Removing the needs reroll tag, patch applies and after running composer phpcs there are no remaining errors.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/update/update.install
@@ -10,19 +10,18 @@
+ * Describes the status of the site regarding available updates. If
+ * there is no update data, only one record will be returned, indicating that
+ * the status of core can't be determined. If data is available, there will be
+ * two records: one for core, and another for all of contrib (assuming there
+ * are any contributed modules or themes enabled on the site). In addition to
+ * the fields expected by hook_requirements ('value', 'severity', and
+ * optionally 'description'), this array will contain a 'reason' attribute,
+ * which is an integer constant to indicate why the given status is being
+ * returned (UPDATE_NOT_SECURE, UPDATE_NOT_CURRENT, or UPDATE_UNKNOWN). This
+ * is used for generating the appropriate email notification messages during
+ * update_cron(), and might be useful for other modules that invoke
+ * update_requirements() to find out if the site is up to date or not.

This comment needs re-flowing - we can use the full 80 chars here.

We've got some new errors here and they are interesting...

core/modules/workspace/src/EntityAccess.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 8 WARNINGS AFFECTING 8 LINES
----------------------------------------------------------------------
 62 | WARNING | Hook implementations should not duplicate @param
    |         | documentation
    |         | (Drupal.Commenting.HookComment.HookParamDoc)
 64 | WARNING | Hook implementations should not duplicate @param
    |         | documentation
    |         | (Drupal.Commenting.HookComment.HookParamDoc)
 66 | WARNING | Hook implementations should not duplicate @param
    |         | documentation
    |         | (Drupal.Commenting.HookComment.HookParamDoc)
 69 | WARNING | Hook implementations should not duplicate @return
    |         | documentation
    |         | (Drupal.Commenting.HookComment.HookReturnDoc)
 88 | WARNING | Hook implementations should not duplicate @param
    |         | documentation
    |         | (Drupal.Commenting.HookComment.HookParamDoc)
 90 | WARNING | Hook implementations should not duplicate @param
    |         | documentation
    |         | (Drupal.Commenting.HookComment.HookParamDoc)
 92 | WARNING | Hook implementations should not duplicate @param
    |         | documentation
    |         | (Drupal.Commenting.HookComment.HookParamDoc)
 95 | WARNING | Hook implementations should not duplicate @return
    |         | documentation
    |         | (Drupal.Commenting.HookComment.HookReturnDoc)
----------------------------------------------------------------------

This is a class and not a direct hook implementation. I think the rule should be updated to only test procedural code and not classes. Once we have class based hooks they'll probably use an interface and therefore we'll have @inheritdoc.

So we need an issue filed against coder to fix the rule.

idebr’s picture

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

klausi’s picture

Status: Needs work » Postponed

Pushed a fix for Coder. It will take a couple of weeks until I will make the next release, you can help updating Coder at #3063323: Update drupal/coder to 8.3.6 in the meantime.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

longwave’s picture

Status: Postponed » Needs work

The fix for coder has been implemented, currently the failures for this sniff are in the following files, so the issue in #29 is resolved and this can be picked up again:

/core/modules/image/image.install
/core/modules/config/tests/config_install_dependency_test/config_install_dependency_test.module
/core/modules/breakpoint/breakpoint.module
/core/modules/update/update.install
/core/modules/update/update.module
/core/modules/filter/filter.module
/core/modules/language/language.module
/core/modules/content_moderation/tests/modules/content_moderation_test_views/content_moderation_test_views.module
/core/modules/search/tests/modules/search_query_alter/search_query_alter.module
/core/modules/search/tests/modules/search_date_query_alter/search_date_query_alter.module
/core/modules/system/tests/modules/form_test/form_test.module
/core/modules/system/tests/modules/common_test/common_test.module
/core/modules/system/tests/modules/module_test/module_test.module
anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
0 bytes
9.18 KB

Re-rolled for 9.2.x.

idebr’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

The patch in #38 is empty

vsujeetkumar’s picture

Status: Needs work » Needs review
FileSize
11.6 KB

Re-roll patch for 9.2.x.

vsujeetkumar’s picture

Issue tags: -Needs reroll
longwave’s picture

Status: Needs review » Needs work

This is looking close now.

  1. +++ b/core/modules/image/image.install
    @@ -33,9 +33,9 @@ function image_uninstall() {
    + * Checks the PHP GD Library.
    

    This isn't strictly true as we allow multiple image libraries - I think we can just remove this line and just say

    /**
     * Implements hook_requirements().
     */
    
  2. +++ b/core/phpcs.xml.dist
    @@ -75,6 +75,7 @@
    +  <rule ref="../vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/HookCommentSniff.php"/>
    

    This should be

    <rule ref="Drupal.Commenting.HookComment"/>
    
  3. +++ b/core/themes/bartik/bartik.theme
    @@ -115,9 +115,10 @@ function bartik_theme_suggestions_form_alter(array &$suggestions, array $variabl
     /**
      * Implements hook_form_alter().
    + *
    + * Adds classes to the search form.
      */
     function bartik_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    -  // Add classes to the search form.
    

    Is this change really in scope here?

vsujeetkumar’s picture

@longwave Changes has been done according to #42, Please have a look and advise.

vsujeetkumar’s picture

Status: Needs work » Needs review
longwave’s picture

Status: Needs review » Needs work

Thanks, those changes all look good. I ran phpcs locally over the full codebase and there is one remaining issue here:

FILE: core/modules/content_moderation/tests/modules/content_moderation_test_views/content_moderation_test_views.module
---------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
---------------------------------------------------------------------------------------------------------------------------------------------------------
 14 | WARNING | Hook implementations should not duplicate @param documentation
 16 | WARNING | Hook implementations should not duplicate @param documentation
---------------------------------------------------------------------------------------------------------------------------------------------------------

In other words the @param lines need to be removed from:

/**
 * Implements hook_views_query_alter().
 *
 * @param \Drupal\views\ViewExecutable $view
 *   The view object about to be processed.
 * @param \Drupal\views\Plugin\views\query\QueryPluginBase $query
 *   The query plugin object for the query.
 *
 * @see \Drupal\Tests\content_moderation\Kernel\ViewsModerationStateSortTest::testSortRevisionBaseTable()
 */
ankithashetty’s picture

Status: Needs work » Needs review
FileSize
11.83 KB
759 bytes

Updated the patch in #43 as suggested in #45. Thanks!

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, this looks ready to commit now.

  • catch committed 5152d66 on 9.2.x
    Issue #2623718 by attiks, vsujeetkumar, andypost, MerryHamster,...

  • catch committed 526d0d2 on 9.1.x
    Issue #2623718 by attiks, vsujeetkumar, andypost, MerryHamster,...
catch’s picture

Version: 9.2.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.2.x and 9.1.x.

+++ b/core/modules/update/update.module
@@ -390,16 +390,6 @@ function update_fetch_data_finished($success, $results) {
  * @see _update_message_text()
diff --git a/core/phpcs.xml.dist b/core/phpcs.xml.dist

diff --git a/core/phpcs.xml.dist b/core/phpcs.xml.dist
index bf22ac6842..223ae7ed72 100644

index bf22ac6842..223ae7ed72 100644
--- a/core/phpcs.xml.dist

--- a/core/phpcs.xml.dist
+++ b/core/phpcs.xml.dist

+++ b/core/phpcs.xml.dist
+++ b/core/phpcs.xml.dist
@@ -75,6 +75,7 @@

@@ -75,6 +75,7 @@
     <exclude name="Drupal.Commenting.FunctionComment.ParamMissingDefinition"/>
     <exclude name="Drupal.Commenting.FunctionComment.TypeHintMissing"/>
   </rule>
+  <rule ref="Drupal.Commenting.HookComment"/>
   <rule ref="Drupal.Commenting.GenderNeutralComment" />
   <rule ref="Drupal.Commenting.VariableComment">

Removed this hunk for the 9.1.x backport.

Status: Fixed » Closed (fixed)

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