Part of meta-issue #2572645: [Meta] Fix 'Drupal.Commenting.FunctionComment' coding standard

Step 0: Reading

Read xjm's comment on the fixing type hints.

Step 1: Preparation

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

Step 2: Install & configure PHPCS

Install PHP CodeSniffer and the ruleset from the Coder module:

$ composer install
$ ./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.

Comments

hash6 created an issue. See original summary.

hash6’s picture

Status: Needs work » Needs review
StatusFileSize
new1.78 KB
hash6’s picture

Assigned: hash6 » Unassigned
snehalgaikwad’s picture

Status: Needs review » Reviewed & tested by the community

This patch is working fine.

alexpott’s picture

Title: Type hint missing in FieldInstanceWidgetSettings and FieldSettings for $widget_settings and $global_settings » Fix Drupal.Commenting.FunctionComment.TypeHintMissing
Status: Reviewed & tested by the community » Needs work
Parent issue: » #2572645: [Meta] Fix 'Drupal.Commenting.FunctionComment' coding standard

These come under #2572645: [Meta] Fix 'Drupal.Commenting.FunctionComment' coding standard ... it doesn't appear that there is an issue to do Drupal.Commenting.FunctionComment.TypeHintMissing.

Any chance this issue could be refactored to fix that one. Now would be a fantastic time to address this.

What you can do:

  • Edit core/phpcs.xml.dist and remove the line that excludes Drupal.Commenting.FunctionComment.TypeHintMissing
  • Run composer run phpcs -- ./core -ps
  • Fix the numerous fails that will include this one
hash6’s picture

Assigned: Unassigned » hash6

Thanks @alexpott would work on it.

hash6’s picture

$ composer run phpcs -- ./core -ps
> phpcs --standard=core/phpcs.xml.dist --runtime-set installed_paths $($COMPOSER_BINARY config vendor-dir)/drupal/coder/coder_sniffer -- './core' '-ps'
ERROR: Referenced sniff "Drupal.Arrays.Array" does not exist

Run "phpcs --help" for usage information

Script phpcs --standard=core/phpcs.xml.dist --runtime-set installed_paths $($COMPOSER_BINARY config vendor-dir)/drupal/coder/coder_sniffer -- handling the phpcs event returned with error code 3

I get the above error while trying to run the command In my phpcs.xml.dist I see the above error.
I found this related issue but https://www.drupal.org/node/3003756 , but it has all the mentioned lines.
@alexpott can you help on this ?

PHP Version: 7.4.1
PHPCS Version: 3.3.2

alexpott’s picture

@hash6 I'm guessing that either your checkout was not clean or you'd not run composer install to update your dependencies. Before updating core/phpcs.cml.dist do something like git checkout 9.0.x && git pull && composer install. Note that this issue needs to be done on 9.0.x first (and maybe only).

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

Drupal 9.0.10 was released on December 3, 2020 and is the final full bugfix release for the Drupal 9.0.x series. Drupal 9.0.x will not receive any further development aside from security fixes. Sites should update to Drupal 9.1.0 to continue receiving regular bugfixes.

Drupal-9-only bug reports should be targeted for the 9.1.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.2.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Assigned: hash6 » quietone
Issue summary: View changes

@hash6, it's been over a year since you worked on this so unassigning.

II hope to get upload a patch before the end of today.

quietone’s picture

StatusFileSize
new526.24 KB

This is incomplete, just did the easy fixes.

quietone credited RobLoach.

quietone’s picture

Closed #318016: Type hint array parameters as a duplicate, adding credit.

quietone’s picture

kapilv’s picture

Assigned: quietone » kapilv
kapilv’s picture

kapilv’s picture

Assigned: kapilv » Unassigned
monojithalder’s picture

Assigned: Unassigned » monojithalder
quietone’s picture

Issue summary: View changes

Add reference to xjm's comment about the challenges of fixing type hints.

quietone’s picture

Issue summary: View changes

quietone’s picture

Version: 9.1.x-dev » 9.2.x-dev
Component: field system » other
Assigned: monojithalder » quietone
Status: Needs work » Active
StatusFileSize
new12.04 KB
new536.12 KB

@monojithalder, apologies for stepping in here. I didn't notice that it was assigned to you. Actually, it would be better to hold off working on this until I bring in the changes from older issues. Do you mind? There are lots of older issues for me to check. I am going to be bold and re-assign to myself. I think it will save us all some work.

This rerolls the patch in #11 and adds changes from #2643890: Add missing typehint for FormElementInterface::valueCallback(). I did not make an diff for the reroll, it was a change to schema.inc. The interdiff is just the additions. Credit to legolasbo for the changes.

quietone’s picture

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone credited acrosman.

quietone credited sarhugo.

quietone’s picture

quietone’s picture

Assigned: quietone » Unassigned
Status: Active » Closed (duplicate)
Related issues: +#3503268: Remove Drupal.Commenting.FunctionComment.TypeHintMissing from phpcs.xml.dist

This sniff was removed from coder. Closing as a duplicate and moving credit to the other issue