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.
How to run tests?
Clone changes from branch 3123069-11.x-useless-overriding-method.
Run the following command from Drupal root to launch the test:
$ phpcs --standard=core/phpcs.xml.dist -- '--report-full' '--report-summary' -p core
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.
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.
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.
Current failures on 11.x
PHP CODE SNIFFER REPORT SUMMARY
---------------------------------------------------------------------------------------------------------------------------------
FILE ERRORS WARNINGS
---------------------------------------------------------------------------------------------------------------------------------
core/lib/Drupal/Core/Action/ActionPluginCollection.php 0 1
core/lib/Drupal/Core/Condition/ConditionPluginCollection.php 0 1
core/lib/Drupal/Core/Config/InstallStorage.php 0 1
core/lib/Drupal/Core/Config/Entity/Query/Query.php 0 1
core/lib/Drupal/Core/Installer/InstallerKernel.php 0 1
core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php 0 2
core/lib/Drupal/Core/Layout/LayoutDefault.php 0 1
core/lib/Drupal/Core/Queue/QueueWorkerManager.php 0 1
core/lib/Drupal/Core/Render/ElementInfoManager.php 0 1
core/lib/Drupal/Core/Test/TestRunnerKernel.php 0 1
core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EmailValidator.php 0 1
core/modules/block/src/BlockPluginCollection.php 0 1
core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditorStylesheetsWarningTest.php 0 1
core/modules/comment/tests/src/Functional/Views/CommentApproveLinkTest.php 0 1
core/modules/config/tests/src/Functional/ConfigLanguageOverrideWebTest.php 0 1
core/modules/config_translation/src/Form/ConfigTranslationAddForm.php 0 1
core/modules/filter/src/FilterFormatAddForm.php 0 1
core/modules/filter/src/FilterPluginCollection.php 0 1
core/modules/forum/tests/src/Functional/Views/ForumIntegrationTest.php 0 1
core/modules/image/src/ImageEffectPluginCollection.php 0 1
core/modules/jsonapi/tests/src/Functional/MediaTest.php 0 1
core/modules/locale/tests/src/Functional/LocaleConfigTranslationImportTest.php 0 1
core/modules/migrate/tests/src/Kernel/Plugin/LogTest.php 0 1
core/modules/migrate/tests/src/Unit/SqlBaseTest.php 0 1
core/modules/migrate/tests/src/Unit/TestMigrateExecutable.php 0 1
core/modules/migrate/tests/src/Unit/TestSqlIdMap.php 0 3
core/modules/migrate/tests/src/Unit/destination/EntityRevisionTest.php 0 2
core/modules/migrate/tests/src/Unit/process/SubstrTest.php 0 1
core/modules/migrate_drupal/tests/modules/field_discovery_test/src/FieldDiscoveryTestClass.php 0 6
core/modules/node/src/Entity/Node.php 0 1
core/modules/node/tests/src/Functional/Views/NodeTestBase.php 0 1
core/modules/options/tests/src/Kernel/OptionsFormattersTest.php 0 1
core/modules/phpass/tests/src/Unit/PasswordVerifyTest.php 0 1
core/modules/responsive_image/src/Entity/ResponsiveImageStyle.php 0 1
core/modules/search/src/Plugin/SearchPluginCollection.php 0 1
core/modules/serialization/tests/src/Unit/Normalizer/DateTimeIso8601NormalizerTest.php 0 1
core/modules/serialization/tests/src/Unit/Normalizer/DateTimeNormalizerTest.php 0 1
core/modules/serialization/tests/src/Unit/Normalizer/TimestampNormalizerTest.php 0 1
core/modules/system/src/Plugin/ImageToolkit/Operation/gd/GDImageToolkitOperationBase.php 0 1
core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php 0 1
core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMul.php 0 1
core/modules/system/tests/src/Functional/Datetime/DrupalDateTimeTest.php 0 1
core/modules/system/tests/src/Functional/Render/UrlBubbleableMetadataBubblingTest.php 0 1
core/modules/system/tests/src/Functional/System/IndexPhpTest.php 0 1
core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php 0 1
core/modules/taxonomy/tests/src/Functional/Views/TaxonomyParentUITest.php 0 1
core/modules/tour/src/TipsPluginCollection.php 0 1
core/modules/user/tests/src/Functional/AccessRoleUITest.php 0 1
core/modules/user/tests/src/Functional/Views/AccessRoleTest.php 0 1
core/modules/views/src/DisplayPluginCollection.php 0 1
core/modules/views/tests/src/Kernel/Plugin/PluginBaseTest.php 0 1
core/tests/Drupal/KernelTests/Core/Datetime/DatelistElementFormTest.php 0 1
core/tests/Drupal/KernelTests/Core/Datetime/DatetimeElementFormTest.php 0 1
core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php 0 1
core/tests/Drupal/Tests/Component/Utility/ArgumentsResolverTest.php 0 1
core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php 0 5
core/tests/Drupal/Tests/Core/Entity/Sql/DefaultTableMappingTest.php 0 2
core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php 0 2
core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php 0 1
---------------------------------------------------------------------------------------------------------------------------------
A TOTAL OF 0 ERRORS AND 74 WARNINGS WERE FOUND IN 59 FILES
---------------------------------------------------------------------------------------------------------------------------------
Issue fork drupal-3123069
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
longwaveThis one triggers on a whole bunch of code like
which is technically a useless override, except that we are providing a better typehint for the return value.
Should we duplicate this sniff in Coder and make it ignore the case where the return value is of a different type to the parent?
Comment #3
jungleRe #2, From my understanding, If the method in parent class/interface has a return value typehint, the corresponding method with the same signature in child class should not declare a different return value typehint. So personally, I vote for No.
Comment #5
quietone CreditAttribution: quietone as a volunteer commentedComment #12
Nikolay ShapovalovI wasn't able to find any commits in the branch 3123069-fix-generic.codeanalysis.uselessoverridingmethod-coding.
Compare to 9.4.x
And I wasn't able to merge changes to 11.x, so I decide to create a new branch.
I create draft MR with some changes, and I want to get some feedback about approach.
I update issue description and change target branch for this issue.
Comment #13
longwaveI still think we need to decide what to do about #2, where the updated return type does add value for people using IDEs and perhaps static analysis too. Just removing them all outright seems like we are losing information.
Ideally we would just add return types everywhere anyway, and it is valid to return a more specific type in a subclass because of covariance, but we can't do that without breaking backward compatibility where contrib or custom code might have already extended without adding a type.
Comment #14
smustgrave CreditAttribution: smustgrave at Mobomo commentedFor #13