Problem/Motivation
Postponed on #2857906: Enforce short array syntax for Drupal 8 api docblock comments
This is a follow-up to #2868078: Use new array syntax in PHP files outside of /core..
The coding standards for arrays require short array syntax (except for Drupal 7 core and contributed modules). Does this apply to code samples inside @code blocks? If so, then we need to
- Clarify this requirement in the documentation.
- Check array syntax inside
@codeblocks in our CodeSniffer sniffs. - Update Drupal core to use the short syntax inside
@codeblocks.
Steps to reproduce
Proposed resolution
Add the new sniff, <rule ref="Drupal.Commenting.DocCommentLongArraySyntax"/> to phpcs.xml.dist
Run phpcs on core
Fix the errors found
Remaining tasks
See #46 Followup for use @code/@endcode where possible #3454142: Convert long array syntax in comments
See #47 Followup to fix array() in sentences. #3454159: Convert use of array() syntax in sentences
Patch
Review
Commit
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-2874067
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
Comment #2
benjifisherThe attached patch is a start. It is the same as the interdiff from #2868078-22: Use new array syntax in PHP files outside of /core.. It only changes array syntax inside
@codeblocks in files outside thecore/directory.Comment #4
benjifisherThe patch may apply after #2868078: Use new array syntax in PHP files outside of /core. goes in.
Comment #6
benjifisherOops, I think I got the patch reversed.
Comment #8
MerryHamster commentedhere my variant the patch for 8.6.x
when I have been beginning I did not think that I will have found so many places where need to do changes
Comment #9
MerryHamster commentedComment #10
sk33lz commentedPatch #8 applies cleanly to 8.6.x with no offsets. I don't know if this updates all code samples throughout core, but all the ones I reviewed that it updated look great so far.
Comment #17
quietone commentedClosed #2857771: Convert core to array syntax coding standards - API docs and #2857772: Convert core to array syntax coding standards - The D8 handbook as duplicates.
Coding standards are fixed by sniff now. Postponing this on #2857906: Enforce short array syntax for Drupal 8 api docblock comments.
Adding to the coding standards meta.
Comment #18
dwwI foolishly opened #3273865: Correct use of array() to [] in comments (to remove a 'Needs followup' tag at #3265929: Rewrite examples of form options to be less culturally specific) without first searching. @quietone helpfully pointed me here.
However, the coder sniff linked here only handles stuff inside
@codeblocks. My initial manual effort at #3273865 found some spots wherearray()was used in comments that were missing@codeblocks. I started added the@code/@endcode. But now it's a bit of a tangled mess. Perhaps we should re-purpose/re-open #3273865 to specifically handle all the edge cases where we're missing@codeblocks?Comment #19
quietone commentedYes, that sounds good to me. And I will make a patch from the MR over there and post it here so that any fixes you made can be merged into this one at a later date.
Comment #20
quietone commentedUploaded the patch from duplicate issue here as it contains files not changed in the existing patch. Adding credit for dww.
Comment #21
dwwThe coder issue is now fixed. Guess we need to regenerate a patch in here now to use that, so back to NW.
Comment #24
kunalgautam commentedHere patch for 10.1.x
Comment #25
jay jangid commentedComment #26
jay jangid commentedI have applied the patch #24 in Drupal 10.1.x version and it is working fine.
Thanks for the patch.
Results :-Fix short array syntax in "core.api.php".
Comment #27
smustgrave commentedComparing #24 with #8 seems to be missing a big chunk of the fixes.
Some of them may have naturally been fixed but the few files I checked they have not.
Comment #28
kunalgautam commentedThanks @smustgrave this is the updated patch
Comment #29
kunalgautam commentedComment #30
smustgrave commentedCI failure #28
Comment #31
kunalgautam commentedUpdated the patch
Comment #32
kunalgautam commentedComment #33
quietone commentedNice to see progress here!
Fixing PHP coding standards is done by first adding a Coder rule, or sniff. The current patch is missing that so it needs to be added.We do this so that new code does not introduce regressions. When a patch is tested various checks are run, such as coding standard checks, before the tests run.
So, what needs to happen here is to start by just adding the sniff. Then run phpcs to find what needs to be fixed. Then fix the comments and run phpcs core until there are no errors. The latest patch will have some of those fixes but it also contains out of scope changes. This patch is to fix array syntax in comments, not code.
I have updated the proposed resolution in the Issue Summary with some details.
Comment #36
nikolay shapovalov commentedComment #37
nikolay shapovalov commentedI update phpcs.xml and remove unwanted changes.
I was not able to apply patch 32, so I made some changes to it, I added manually created interdiff.
Please review.
Comment #38
nikolay shapovalov commentedOops, made mistake when creating a patch file.
Provide new patch file and interdiff.
Comment #39
nikolay shapovalov commentedUpdate interdiff #32 and #38.
Comment #40
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue.
While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)
Comment #42
nikolay shapovalov commentedI created MR as requested by Needs Review Queue Bot.
Comment #43
nikolay shapovalov commentedHide patch files.
Comment #44
smustgrave commentedFailure is random and unrelated, ran it a few times and sometimes it passes sometimes fails but is not impacted by this
#3317520: [random test failure] Drupal\Tests\settings_tray\FunctionalJavascript\SettingsTrayBlockFormTest::testEditModeEnableDisable
Large MR but not terrible to read.
Think if anything was missed the phpcs line added would of got it.
Comment #45
xjmAs the person who reviewed and committed the 12-gigabyte
array()to[]conversion, I feel it's my very great honor to review this one as well.Comment #46
xjmSomething odd is going on with the docblock of
EntityFormDisplayInterface::buildForm(); there are more array openings than closures. It appears to be pseudocode rather than example code, and at least one array closure was missed. That's an existing problem and out of scope for a color-words issue like this, so tagging for a followup.Comment #47
xjmI reviewed this locally with
git diff --color-wordsto verify that all the changes were complete and correct replacements ofarray()with[]in API documentation.There was another existing issue in
core/modules/views/src/Plugin/views/cache/CachePluginBase.phpwith a missing closing paren that's fixed here. I opened the file locally to confirm that it's fixed correctly.I also grepped the codebase to check for remaining
array(). There are many in inline comments, but that seems to be outside the scope of this issue, which is all docblocks. So, I think we need a second followup for inline comments.However, there are somehow also numerous instances in docblocks that are apparently not being caught by the rule?
Setting NR to look into those. Several of them appear to be in sentences rather than
@code/@endcode-- maybe that is the common factor? If that's confirmed, we could have a third followup to properly use@codewhere possible and a fourth to fix them in sentences.Thanks everyone!
Comment #48
smustgrave commentedThen how come the rule didn’t catch those?
Comment #49
quietone commented@smustgrave, yes that is the question that xjm is asking those working on this issue to answer.
To help move this along I did the following. I started by searching the vendor directory for DocCommentLongArraySyntax and found the class implementing the sniff.
The summary line confirms what xjm suspected in #47. And as I read it then the remainder of the work in #47 can proceed.
I have added #46 and #47 to the remaining tasks.
Comment #50
smustgrave commentedFor the follow ups of 46 + 47
Comment #51
quietone commentedRebased the MR. Then, I searched for the same pattern used in #47 and then checked the files and I am confident these are not detected because they are not in @code @endcode blocks. I have updated the issue summary with the followups that need to be made.
Comment #52
smustgrave commentedSo applied the MR and ran grep -r "\barray(" * | grep -F \* | grep -v "vendor" | grep -v "node_modules"
Which spot checking appear to be outside of @code blocks.
For #45 follow up is that covered by #3454142: Convert long array syntax in comments or #3454159: Convert use of array() syntax in sentences ?
Comment #53
xjmThere is a hunk in
core/modules/views/src/Plugin/views/field/FieldPluginBase.phpthat has out-of-scope changes. It could either be a bad merge, or someone correcting the documentation while fixing this bug.If it's the latter, we should make a followup issue to correct the keys; regardless, the hunk should only include the changes to
array().Going to do a local review of this with
git diff --color-words; if someone wants to fix the hunk and file a followup if appropriate, we could get this in still as a beta phase(-ish) cleanup.Saving issue credits.
Comment #54
xjmFollowups were already created.
Tagging "Novice" for the task described in #53.
Comment #55
smustgrave commented#3455127: Update use of arguments used in comments opened, already tagged for title update cuz that one is bad.
Comment #56
smustgrave commentedOops did that before I got the novice email.
Comment #57
xjmNo worries, I prefer to just land the issue in this case. :D
Comment #60
xjmCommitted to 11.x and 11.0.x.
It cherry-picked cleanly to 10.4.x with some auto-merging, but I'm going to open a quick 10.4.x MR here based on that cherry-pick just to ensure there aren't additional instances in D10 that would cause the rule to fail there.
Comment #63
xjmGood thing I tested that backport before pushing it; looks like the 10.4.x backport will need some additions. :D
Comment #65
b_sharpe commentedFixed up 10.4 backport, passing phpcs now.
Comment #66
smustgrave commented10.4 rebase seems fine.
Comment #67
xjmThanks @b_sharpe, nice work!
Comment #69
xjmSince 10.3.x is in RC, it is not eligible to have new rules enabled. So, instead of testing a 10.3.x backport, I backported the 10.4.x changes only for parity, then removed the rule addition, with the following commands:
Thanks everyone!