Problem/Motivation

When running PHPCS in our builds for the Governor, we ran into one mild issue with some doc commenting.

Steps to reproduce

Run PHPCS in your build, find the following:

+ vendor/bin/phpcs
.............................E.............................. 60 / 62 (97%)
..                                                           62 / 62 (100%)
FILE: ...tlassian/pipelines/agent/build/web/themes/custom/mytheme/mytheme.theme
----------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------
 43 | ERROR | [x] There must be exactly one blank line before the
    |       |     tags in a doc comment
    |       |     (Drupal.Commenting.DocComment.SpacingBeforeTags)
 43 | ERROR | [x] Trailing punctuation for @see references is not
    |       |     allowed.
    |       |     (Drupal.Commenting.FunctionComment.SeePunctuation)
 45 | ERROR | [ ] Parameter tags must be defined first in a doc
    |       |     comment
    |       |     (Drupal.Commenting.DocComment.ParamNotFirst)
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Time: 366ms; Memory: 14MB

Proposed resolution

Create and test patch to change the comments to avoid php code sniffer errors.

CommentFileSizeAuthor
#6 3401702_phpcs_fix.patch2.16 KBindranil roy

Issue fork governor-3401702

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

saltednut created an issue. See original summary.

saltednut’s picture

Title: PHPStan errors found after creating sub-theme » PHPCS errors found after creating sub-theme
saltednut’s picture

saltednut’s picture

As a followup, there is also PHPStan issue that we found but it'll require either rewriting a function or just renaming it in the sub-theme when one is generated.

PHP Fatal error: Cannot redeclare _is_page_link_visible() (previously declared in /Users/myuser/Work/client/web/themes/contrib/governor/governor.theme:55) in /Users/myuser/Work/client/web/themes/custom/client/client.theme on line 70

The workaround, for now, is to rename the global function inside client.theme - but this could be fixed permanently perhaps if a service was provided by the base theme. If needed, I can file another issue for that.

indranil roy’s picture

StatusFileSize
new2.16 KB

Hey @saltednut, I have added the patch please verify it.

indranil roy’s picture

Status: Active » Needs review
saltednut’s picture

Thanks @Indranil Roy, there is a merge request pending so we may not need a patch?

kurttrowbridge’s picture

Status: Needs review » Reviewed & tested by the community

Hello! I tested this, first confirming that I saw the PHPCS errors mentioned when the patch was not yet applied. After applying the MR as a patch, the PHPCS errors are no longer present. Marking RTBC as a result.

(That said, I should note that line 60 in the .theme file—the comment starting with "Calculate the minimum and maximum"—warns me, both before and after, that it's lengthier than the 80-character limit in Drupal's standards. Not sure why it doesn't appear in your testing, so I didn't flip back to Needs Work in case it's just something I'm doing differently on my end.)

spicy.werewolf’s picture

Assigned: Unassigned » spicy.werewolf
Status: Reviewed & tested by the community » Needs work

Since I am picking this up cold, I ran phpcs before attempting to apply the fixes in the MR and found a bunch of other errors and warnings. (So @kurttrowbridge, it wasn't just a local quirk!) I'm curious to know why the same test is showing people different results, but not enough to dig into that part. Here's what I am seeing:

drupal_sandbox > code-review:

     [echo] $> vendor/bin/phpcs
     [exec] ...............................EW.E. 36 / 36 (100%)
     [exec] 
     [exec] 
     [exec] 
     [exec] FILE: /var/www/html/docroot/themes/custom/governor/governor.theme
     [exec] ------------------------------------------------------------------------------------------
     [exec] FOUND 3 ERRORS AND 1 WARNING AFFECTING 3 LINES
     [exec] ------------------------------------------------------------------------------------------
     [exec]  43 | ERROR   | [x] There must be exactly one blank line before the tags in a doc comment
     [exec]  43 | ERROR   | [x] Trailing punctuation for @see references is not allowed.
     [exec]  45 | ERROR   | [ ] Parameter tags must be defined first in a doc comment
     [exec]  59 | WARNING | [ ] Line exceeds 80 characters; contains 84 characters
     [exec] ------------------------------------------------------------------------------------------
     [exec] PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
     [exec] ------------------------------------------------------------------------------------------
     [exec] 
     [exec] 
     [exec] FILE: /var/www/html/docroot/themes/custom/governor/governor.info.yml
     [exec] -----------------------------------------------------------------------------------------------------------
     [exec] FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
     [exec] -----------------------------------------------------------------------------------------------------------
     [exec]  1 | WARNING | Remove "version" from the info file, it will be added by drupal.org packaging automatically
     [exec] -----------------------------------------------------------------------------------------------------------
     [exec] 
     [exec] 
     [exec] FILE: /var/www/html/docroot/themes/custom/governor/src/StarterKit.php
     [exec] -------------------------------------------------------------------------
     [exec] FOUND 4 ERRORS AND 2 WARNINGS AFFECTING 6 LINES
     [exec] -------------------------------------------------------------------------
     [exec]   10 | ERROR   | [x] Missing class doc comment
     [exec]   43 | ERROR   | [ ] Description for the @return value is missing
     [exec]   65 | ERROR   | [x] Expected 1 blank line after function; 2 found
     [exec]   78 | WARNING | [ ] Line exceeds 80 characters; contains 95 characters
     [exec]  153 | ERROR   | [ ] Parameter $machine_name is not described in comment
     [exec]  154 | WARNING | [ ] Line exceeds 80 characters; contains 115 characters
     [exec] -------------------------------------------------------------------------
     [exec] PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
     [exec] -------------------------------------------------------------------------
     [exec] 
     [exec] Time: 72ms; Memory: 6MB
     [exec] 

BUILD FAILED

as @saltednut pointed out, we need an MR, not a patch, so I'm going to fix the rest of these errors, and get it all merged.

spicy.werewolf’s picture

Status: Needs work » Active

spicy.werewolf changed the visibility of the branch 1.0.x to hidden.

spicy.werewolf’s picture

Version: 1.0.0-alpha1 » 1.0.x-dev
spicy.werewolf’s picture

Point of clarification: I am running code tests both on the starterkit itself, and an instance of the theme that gets spun up from running the starterkit script.

spicy.werewolf’s picture

I am not able to replicate the PHPStan error mentioned in #5 but that is probably because my test config isn't set up to check /themes/contrib/. It seems like if it did, this error would happen for every helper function, or functions that don't contain the theme name, as they would slip passed being renamed by the starterkit script.

I can't think of a use case where you would have both the starterkit, and the generated project theme installed at the same time, so I can't think of an instance where you'd actually hit this error in the wild. That said, if I change the name of the function in the starterkit to include "governer" after the underscore like this: _governor_is_page_link_visible when I run the script the function name is updated as expected, so even if one did have both the starterkit and the generated theme installed at the same time, you wouldn't get this error. I'll add this update to the MR.

spicy.werewolf’s picture

Update:
The errors and warnings above coming from src/StarterKit.php can probably be ignored. Given all the work that has been done to spin up new custom themes using the Core generate-theme script with just the starterkit.yml file, it looks as though we might not need that file at all. And it is removed from the generated theme, so isn't problematic there, since, you know, it doesn't exist there.

The error thrown by the missing description in the generated theme can be avoided by adding a default theme description in the starterkit.yml file. Something like:

info:
  hidden: null
  starterkit: null
  version: 1.0.0
  description: Generated by the Governor starterkit. Please update this description for your specific project.

The warning getting thrown about the "Version", however, appears to be a Core bug relating to how the generate-theme script handles Version tracking. I am able to replicate the error using the Core starterkit_theme. In order for the generate-theme script to run at all, a version must be specified in the starterkit theme's info.yml file. However, the script adds a generated version as well. As a result, the generated theme's info.yml file will have both. e.g.

name: gov_theme_test
...
version: 1.0.0
...
generator: 'governor:1.0.0'

The generated version number gets pulled from governor.starterkit.yml:

info:
  version: 1.0.0

Remove the Version key, the script won't run, leave it in, and it throws an error. Don't specify a version in code>governor.starterkit.yml and an error is thrown in the generated info.yml file.

The easiest way to get rid of the error is to manually delete "Version: 1.0.0" from the generated info.yml file in the newly minted custom theme. But the 'correct' solution is for the generate-theme script not to add the Version key to the generated info.yml file in the first place, since, "version" is special: we want to allow it in core, but not anywhere else." (See vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/InfoFiles/AutoAddedKeysSniff.php line 78).
I'll open a core issue to address this. I just wanted to get all my thoughts out here first.

spicy.werewolf’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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