Problem/Motivation

All methods should have a scope. This is part of the existing coding standards - see https://www.drupal.org/docs/develop/coding-standards/object-oriented-cod...

Proposed resolution

Add the Squiz.Scope.MethodScope.Missing rule
Make all methods missing scope public as this is the default and therefore no API change.

I ran the following replaces:
^ static function with public static function
^ function with public function
^ abstract function with abstract public function

Scripted version:

find ./core -name "*.php*" -exec sed -i 's/^  function/  public function/g' {} \;
find ./core -name "*.php*" -exec sed -i 's/^  static function/  public static function/g' {} \;
find ./core -name "*.php*" -exec sed -i 's/^  abstract function/  abstract public function/g' {} \;
curl https://www.drupal.org/files/issues/fix-post-script.patch | git apply

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Status: Active » Needs review
FileSize
932.56 KB

Status: Needs review » Needs work

The last submitted patch, 2: 2854529-2.patch, failed testing.

alexpott’s picture

Status: Needs work » Needs review
FileSize
9.4 KB
923.84 KB

Ughs I dislike the faking of procedural code in unit tests. It's just not a unit test anymore.

alexpott’s picture

Issue summary: View changes
Chi’s picture

alexpott’s picture

@Chi thanks for relating that issue - let's proceed here because this is coming with a coding standard and automated checking - I will created everyone on that issue here and mark it as a duplicate.

alexpott credited balagan.

alexpott credited dawehner.

Anonymous’s picture

alexpott credited vaplas.

alexpott’s picture

Chi’s picture

@alexpott that issue is not only about missing method scopes but also about wrong scopes (test methods should not be protected).

alexpott’s picture

@Chi well let's just deal with the protected ones there then. See new comment on that issue.

Status: Needs review » Needs work

The last submitted patch, 4: 2854529-4.patch, failed testing.

alexpott’s picture

Status: Needs work » Needs review
FileSize
1.2 KB
923.18 KB

Fixing the final fails.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Nice! Thank you alex. My rudimentary ag/grep skills give a +1 here.

alexpott’s picture

I'll backport this to 8.3.x once it lands in 8.4.x - it doesn't apply.

alexpott’s picture

Issue summary: View changes
Mixologic’s picture

+1 testbots saw the new rule and ran it and phpcs didnt find any issues:

00:00:49.091 ----------------   Starting phpcs   ----------------
00:00:49.091 Directory created at /var/lib/drupalci/workspace/jenkins-drupal_patches-3016/ancillary/phpcs
00:00:49.092 PHPCS sniffing the project.
00:00:49.092 Checking for phpcs.xml(.dist) file.
00:00:49.092 Checking for PHPCS config file: /var/www/html/core/phpcs.xml*
00:00:49.093 test -e /var/www/html/core/phpcs.xml*
00:00:49.269 Using existing PHPCS config file.
00:00:49.270 Checking for phpcs tool in codebase.
00:00:49.270 test -e /var/www/html/vendor/squizlabs/php_codesniffer/scripts/phpcs
00:00:49.477 PHPCS config file modified, sniffing entire project.
00:00:49.478 Executing PHPCS.
00:00:49.478 cd /var/www/html/core && /var/www/html/vendor/squizlabs/php_codesniffer/scripts/phpcs --warning-severity=2 --report-full=/var/lib/drupalci/workdir/phpcs/codesniffer_results.txt --report-checkstyle=/var/lib/drupalci/workdir/phpcs/checkstyle.xml --report-diff=/var/lib/drupalci/workdir/phpcs/codesniffer_fixes.patch /var/www/html/core
00:03:40.342 cd /var/www/html/core && /var/www/html/vendor/squizlabs/php_codesniffer/scripts/phpcs -e  --warning-severity=2 --report-full=/var/lib/drupalci/workdir/phpcs/codesniffer_results.txt --report-checkstyle=/var/lib/drupalci/workdir/phpcs/checkstyle.xml --report-diff=/var/lib/drupalci/workdir/phpcs/codesniffer_fixes.patch /var/www/html/core > /var/lib/drupalci/workdir/phpcs/phpcs_sniffs.txt
00:03:40.629 Directory created at /var/lib/drupalci/workspace/jenkins-drupal_patches-3016/artifacts/phpcs
00:03:40.630 Adjusting paths in report file: /var/lib/drupalci/workspace/jenkins-drupal_patches-3016/ancillary/phpcs/checkstyle.xml
00:03:40.635 ---------------- Finished phpcs in 171.55637288094 ---------------- 
alexpott’s picture

Issue tags: +rc target

Discussed with @xjm. We're going to schedule this for the RC phase to minimise disruption. We can roll 8.3.x and 8.4.x patches then.

alexpott’s picture

Version: 8.4.x-dev » 8.3.x-dev
Status: Reviewed & tested by the community » Postponed

And therefore postponed.

Chi’s picture

All methods should have a scope.

Formally it is not a part of Drupal coding standard because Squiz.Scope.MethodScope is not listed in Drupal ruleset.xml.

alexpott’s picture

@Chi interesting - can you file an issue to add it?

alexpott’s picture

Issue summary: View changes
Chi’s picture

alexpott’s picture

Issue summary: View changes
Chi’s picture

We could check a scope for class properties in this issue as well since it is very related subject. Currently there is no errors of this type (thanks to #2706753: Fix 'PSR2.Classes.PropertyDeclaration.ScopeMissing/VarUsed' coding standard) but adding a rule for this (Squiz.Scope.MemberVarScope.Missing) would help us to prevent regression.

alexpott’s picture

Issue summary: View changes
klausi’s picture

Status: Postponed » Needs work
+++ b/core/phpcs.xml.dist
@@ -108,6 +108,7 @@
   <!-- Squiz sniffs -->
+  <rule ref="Squiz.Scope.MethodScope.Missing"/>
   <rule ref="Squiz.Strings.ConcatenationSpacing">

This change is not correct, we should use Drupal.Scope.MethodScope which has a built in fixer.

klausi’s picture

Just looked through our coding standards and realized that we don't define the order of method modifiers anywhere. Opened #2855980: Define order of object method modifiers as in PSR-12.

alexpott’s picture

Here's a new patch for 8.4.x - with a test only so people can see the fails on testbot. Fixes #34. No interdiff cuase reroll.

alexpott’s picture

Issue summary: View changes
alexpott’s picture

Adding issue credit to @klausi and @Mixologic for their reviews.

Status: Needs review » Needs work

The last submitted patch, 36: 2854529-2-36.patch, failed testing.

alexpott’s picture

Version: 8.3.x-dev » 8.4.x-dev
alexpott’s picture

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

Hmmm ok this will eventually go into the rc in 8.3.x but we need to test against and do 8.4.x first... need to remember to set the version on patch upload.

alexpott’s picture

Status: Needs work » Needs review

The test-only patch failed with loads of messages - as expected... just need the patch to run against 8.4.x.

alexpott’s picture

Title: Fix Squiz.Scope.MethodScope.Missing - all methods should have scopes » Fix Drupal.Scope.MethodScope - all methods should have scopes
Issue summary: View changes
FileSize
11.07 KB
917.42 KB
918.2 KB

Here's all the patches for 8.3 and 8.4 post the array change.

klausi’s picture

I can't verify this patch with our phpcbf version because we would need #2857714: Upgrade Coder to 8.2.11.

Will try with my global phpcbf with restricted sniffs and see if that matches.

alexpott’s picture

@klausi but the verification of the patch doesn't need phpcbf - as long as the patch doesn't fail the rules when applied and the fails the rules when the new rule is added then is that not verified? It is not exactly unheard of fixers to even introduce coding standards regressions. For example (not a phpcbf fixer as far as I know but still) - #2857822: Fix coding standards issues introduced mostly by array syntax conversion

klausi’s picture

Status: Needs review » Postponed

Yep, let's make phpcs clean first with #2857822: Fix coding standards issues introduced mostly by array syntax conversion, the other phpcs fails are confusing.

I don't want to review your patch by hand - I want to confirm with phpcbf that the patch is complete and my fixers in Coder are as good as advertised :)

xjm’s picture

Status: Postponed » Needs review

The trailing commas are fixed now, so this can proceed.

xjm’s picture

The title of this issue is fun to read aloud.

Mixologic’s picture

Im not sure how well advertised some of these new phpcs features are in drupalci, but you can see which sniffs actually ran, as well as the standard output from phpcs if you go into the artifacts dir, and go into the phpcs dir on the dispatcher:

https://dispatcher.drupalci.org/job/drupal_patches/4452/artifact/jenkins...

xjm’s picture

Im not sure how well advertised some of these new phpcs features are in drupalci, but you can see which sniffs actually ran, as well as the standard output from phpcs if you go into the artifacts dir, and go into the phpcs dir on the dispatcher:

!!! Holy smokes!

omg it even makes a patch!

xjm’s picture

Okay the way to navigate to it is Click on the test results bubble -> View results on dispatcher -> Click on Build artifacts -> Click on artifacts (end of the path) -> Click on phpcs -> results and a fixer patch are there.

So https://dispatcher.drupalci.org/job/drupal_patches/4529/artifact/jenkins... this one looks clean.

Anonymous’s picture

xjm’s picture

The other issue to fix the protected test methods was committed on its own, so resetting issue credit back to the contributors for this issue.

xjm’s picture

Oh look, they're also reported on the d.o integration:

Anonymous’s picture

#53 fair!
#54 fine! But with css cursor: pointer; it will be absolutely fine!)

So.. we waiting #2857714: Upgrade Coder to 8.2.11 before RTBC, right (#44)?

xjm’s picture

I think so, yah. We just need someone to confirm the 8.3.x backport of the 8.2.11 patch because we don't want to have the branches in different states. It also sounds like klausi wants to do a review with 8.2.11 committed first to make sure the fixer is working as intended.

Once that's all in place and the patch is current, then we'll freeze commits briefly until it's committed. I don't think I've committed anything that would conflict with or violate the standard but we can double-check since it's like, you know, a megabyte patch. :)

xjm’s picture

#2857714: Upgrade Coder to 8.2.11 is in 8.3.x now as well, so the branches correctly share the same version again.

alexpott’s picture

Both the patches in #43 still apply and still fix all the method visibility errors in core. I've sent them both for a re-test.

klausi’s picture

Status: Needs review » Reviewed & tested by the community

Patches looks good to me, verified with ../vendor/bin/phpcs that the output is empty.

I wanted to also test this with the Drupal.Methods.MethodDeclaration sniff, but that one would also change additional method definitions, so the patches were not comparable. See #2855980: Define order of object method modifiers as in PSR-12 where we push for that.

Anonymous’s picture

I also checked 8.4.x and 8.3.x via PhpStorm Inspect Code with rule "Access modifiers shall be defined".
Before patch: 1943 problems 8.4.x (and 1942 problems with 8.3.x).
After patch: 1 problem (/core/lib/Drupal/Core/Archiver/ArchiveTar.php::loadExtension, 279 line)
Should we care about ArchiveTar?

klausi’s picture

@vaplas: no, because ArchiveTar is third party code copied into Drupal and has a @codingStandardsIgnoreFile directive on top. It is exempt from our coding standards and should not be modified.

Anonymous’s picture

@klausi thank you for provide with this moment!
#58 re-test still clean!

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.4.x and 8.3.x, thanks!

  • catch committed 01f8bd6 on 8.4.x
    Issue #2854529 by alexpott, xjm, klausi, Chi, Mixologic: Fix Drupal....
xjm’s picture

Fixing issue credit to remove credit for myself; I did not contribute anything actually relevant to this issue. :P

Status: Fixed » Closed (fixed)

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