Problem/Motivation

This is a part of #2572645: [Meta] Fix 'Drupal.Commenting.FunctionComment' coding standard

This issue is created to enable the sub-sniff Drupal.Commenting.FunctionComment.InvalidNoReturn.

Steps to reproduce

Proposed resolution

Remaining tasks

An issue or issues are needed for the following. These are declaring a return type based on what an overriding method returns.

     1     core/lib/Drupal/Component/Plugin/PluginManagerBase.php
     2     core/lib/Drupal/Core/Database/Connection.php
     3     core/lib/Drupal/Core/Database/Schema.php
     4     core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php
     5     core/lib/Drupal/Core/FileTransfer/FileTransfer.php
     6     core/lib/Drupal/Core/StreamWrapper/ReadOnlyStream.php
     7     core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php
     8     core/modules/views/src/Plugin/views/argument_validator/ArgumentValidatorPluginBase.php

User interface changes

API changes

Data model changes

Release notes snippet

Comments

anoopjohn created an issue. See original summary.

anoopjohn’s picture

Please find a patch that attempts to fix all but 4 of the reported errors. It would be good if somebody else could chip in with the fixes for the four remaining errors. I have attached the errors that have to be looked into though.

Also the patch will have to be reviewed carefully as there are code changes to reflect some of the comments as I felt that there were comments that served to document some of the functions in classes that were expected to be overridden by child classes. I think there were genuine errors in terms of omitted return statements in some of the functions. Not sure if these were to be created as separate issues eg: node_revision_delete in node.module

anoopjohn’s picture

Status: Active » Needs review
mile23’s picture

Status: Needs review » Needs work

Ooo a thorny one. :-)

+++ b/core/lib/Drupal/Core/Config/ImmutableConfig.php
@@ -49,6 +49,7 @@ public function save($has_trusted_data = FALSE) {
   public function delete() {
     throw new ImmutableConfigException("Can not delete immutable configuration {$this->getName()}. Use \\Drupal\\Core\\Config\\ConfigFactoryInterface::getEditable() to retrieve a mutable configuration object");
+    return $this;
   }
 

+++ b/core/lib/Drupal/Core/Database/Connection.php
@@ -173,7 +173,9 @@ public function __construct(\PDO $connection, array $connection_options) {
    * @return \PDO
    *   A \PDO object.
    */
-  public static function open(array &$connection_options = array()) { }
+  public static function open(array &$connection_options = array()) {
+    return $this->connection;
+  }

+++ b/core/lib/Drupal/Core/FileTransfer/FileTransfer.php
@@ -75,6 +75,7 @@ function __construct($jail) {
    */
   static function factory($jail, $settings) {
     throw new FileTransferException('FileTransfer::factory() static method not overridden by FileTransfer subclass.');
+    return new stdClass();
   }

+++ b/core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php
@@ -198,7 +198,7 @@ public function getTranslateRoute() {
    *   The menu link ID.
    */
   protected function getUuid() {
-    $this->getDerivativeId();
+    return $this->getDerivativeId();
   }

+++ b/core/modules/node/node.module
@@ -470,7 +470,7 @@ function node_revision_load($vid = NULL) {
  *   TRUE if the revision deletion was successful; otherwise, FALSE.
  */
 function node_revision_delete($revision_id) {
-  entity_revision_delete('node', $revision_id);
+  return entity_revision_delete('node', $revision_id);
 }

If these code changes are made to match the documentation, then they're bugs and should be a separate issue against 8.1.x.

I'm really kind of amazed that tests passed with these changes, for at least two reasons. :-)

Beyond that, the doc changes will require careful examination of the methods to review.

anoopjohn’s picture

The first three are functions that are to be overridden by inherited classes and hence the errors when not overridden. So by default these functions should not have resulted in any test failures right?

I have raised separate issues for the node_revision_delete return #2737743: node_revision_delete() not returning bool in node.module as well as the getUUid return #2737741: Drupal\menu_link_content\Plugin\Menu\MenuLinkContent::getUuid() does not return any value issues.

Remove the last two from the patch? What about the first three? What needs to be done there?

anoopjohn’s picture

Status: Needs work » Needs review
mile23’s picture

Status: Needs review » Postponed

Let's just postpone this until the other issues are fixed, because we shouldn't patch phpcs.xml.dist before they are.

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

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mfernea’s picture

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

idebr’s picture

Status: Postponed » Needs review

The blocking issues are committed, so this issue is now unblocked.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

longwave’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
abhisekmazumdar’s picture

Status: Needs work » Needs review
StatusFileSize
new8.67 KB

Tried to re-roll the patch from #2.

longwave’s picture

Issue tags: -Needs reroll
StatusFileSize
new19.42 KB

Thanks for the reroll.

I ran phpcs across the whole codebase and found a few more issues and tried to fix them. Two issues remain:

For the case of \Drupal\Core\FileTransfer\FileTransfer:

  public static function factory($jail, $settings) {
    throw new FileTransferException('FileTransfer::factory() static method not overridden by FileTransfer subclass.');
  }

Can't we just declare this method abstract?

In \Drupal\Core\Database\Schema:

  /**
   * Finds the columns for the primary key, unique keys and indexes of a table.
   *
   * @param string $table
   *   The name of the table.
   *
   * @return array
   *   A schema array with the following keys: 'primary key', 'unique keys' and
   *   'indexes', and values as arrays of database columns.
   *
   * @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
   *   If the specified table doesn't exist.
   * @throws \RuntimeException
   *   If the driver does not implement this method.
   */
  protected function introspectIndexSchema($table) {
    if (!$this->tableExists($table)) {
      throw new SchemaObjectDoesNotExistException("The table $table doesn't exist.");
    }
    throw new \RuntimeException("The '{$this->connection->driver()}' database driver does not implement " . __METHOD__);
  }

I'm not sure what to do here, as adding a dead-code return seems wrong as well.

daffie’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Component/PhpStorage/FileStorage.php
    @@ -88,9 +88,6 @@ protected function ensureDirectory($directory, $mode = 0777) {
    -   *
    -   * @return bool
    -   *   TRUE if the directory exists or has been created, FALSE otherwise.
    

    I do not see why this change is needed. The method returns a boolean value.

  2. +++ b/core/lib/Drupal/Core/Database/Connection.php
    @@ -271,7 +271,9 @@ public function __construct(\PDO $connection, array $connection_options) {
    +  public static function open(array &$connection_options = []) {
    +    return $this->connection;
    +  }
    

    This change is wrong. $this->connection is not a PDO object. Lets make the method an abstract method as every by core supported driver implements the method as does the ones for SQL Server and OracleDB. Also, the method now does not work for any driver. Lets change the code to: abstract public static function open(array &$connection_options = []);.

  3. +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
    @@ -1452,9 +1452,6 @@ protected function addTableDefaults(&$schema) {
        * @param array $schema
    ...
       protected function processDataTable(ContentEntityTypeInterface $entity_type, array &$schema) {
    
    @@ -1469,9 +1466,6 @@ protected function processDataTable(ContentEntityTypeInterface $entity_type, arr
        * @param array $schema
    ...
       protected function processRevisionDataTable(ContentEntityTypeInterface $entity_type, array &$schema) {
    

    Can we change these parameter documentation to: @param array &$schema.

  4. +++ b/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php
    @@ -219,7 +219,9 @@ protected static function extractAllowedValues($string, $has_data) {
        * @return string
    ...
    +    return NULL;
    

    As we are returning null here the return value in the docblock must be changed to: @return string|null.

  5. For the case of \Drupal\Core\FileTransfer\FileTransfer:

      public static function factory($jail, $settings) {
        throw new FileTransferException('FileTransfer::factory() static method not overridden by FileTransfer subclass.');
      }
    

    Can't we just declare this method abstract?

    +1 for me. In its docblock it is saying: "lasses that extend this class must override the factory() static method. They should return a new instance of the appropriate FileTransfer subclass.".

  6. As for the method \Drupal\Core\Database\Schema::introspectIndexSchema(). The method is not usable without every database driver overridding it. Every by core suported driver does so and the same for SQL Server and OracleDB. Lets change the method to an abstract one.
andypost’s picture

Maybe instead of removal of return null, use void?

ravi.shankar’s picture

StatusFileSize
new19.31 KB

Added reroll of patch #21.

longwave’s picture

@andypost PHPDoc standards say "@return void" can be omitted when methods do not return anything: https://docs.phpdoc.org/3.0/guide/references/phpdoc/tags/return.html#return

longwave’s picture

Status: Needs work » Needs review
StatusFileSize
new21.38 KB
new6.75 KB

Addressed all comments in #22, also cleaned up one more docblock to use inheritdoc instead of "Overrides..." and reworded the ImageTest mock setup methods as they don't "Get" anything really.

Status: Needs review » Needs work

The last submitted patch, 26: 2722647-26.patch, failed testing. View results

renatog’s picture

We should include a full stop on this comment
The link definitions to be altered. Each link has a key that is the machine
It can be a problem because with 1 more char it'll have more than 80 chars and we need to split in a second line

longwave’s picture

@RenatoG I don't think a full stop needs adding, the first line is being replaced but the sentence continues on the next line:

 *   The link definitions to be altered. Each link has a key that is the machine
 *   name, which must be unique. By default, use the route name as the
longwave’s picture

Status: Needs work » Needs review
StatusFileSize
new22.74 KB
new1.36 KB

Implemented two abstract methods in test classes.

renatog’s picture

Status: Needs review » Reviewed & tested by the community

Yes, it's true @longwave/ It really makes sense

#30 looks good

andypost’s picture

It could use follow-up looking on winning never for PHP 8.1 https://wiki.php.net/rfc/noreturn_type

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/Database/Schema.php
@@ -520,15 +520,8 @@ protected function findPrimaryKeyColumns($table) {
-   * @throws \RuntimeException
-   *   If the driver does not implement this method.
    */
-  protected function introspectIndexSchema($table) {
-    if (!$this->tableExists($table)) {
-      throw new SchemaObjectDoesNotExistException("The table $table doesn't exist.");
-    }
-    throw new \RuntimeException("The '{$this->connection->driver()}' database driver does not implement " . __METHOD__);
-  }
+  abstract protected function introspectIndexSchema($table);

+++ b/core/lib/Drupal/Core/FileTransfer/FileTransfer.php
@@ -73,9 +70,7 @@ public function __construct($jail) {
-  public static function factory($jail, $settings) {
-    throw new FileTransferException('FileTransfer::factory() static method not overridden by FileTransfer subclass.');
-  }
+  abstract public static function factory($jail, $settings);

These are API changes that we shouldn't be making in a coding standards change. The FileTransferException seems more okay but it should get its own issue. The introspectIndexSchema is definitely not okay. This method exists only for testing purposes - see SchemaIntrospectionTestTrait - that's the only place this stuff is called. There's no need for a contrib driver to declare this method and making this change could break a contrib driver.

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’s picture

Status: Needs work » Needs review
StatusFileSize
new22.75 KB
new24.47 KB

Rerolled and also made a few changes so the commit code checks pass locally.

quietone’s picture

Status: Needs review » Needs work
quietone’s picture

Component: base system » other
Status: Needs work » Needs review
StatusFileSize
new2.92 KB
new22.96 KB

Make changes for #33.

quietone’s picture

StatusFileSize
new4.48 KB
new27.65 KB

Remove lines from phpstan-baseline.neon

quietone’s picture

StatusFileSize
new1.42 KB
new27.65 KB

Restore drupalci

smustgrave’s picture

Status: Needs review » Needs work

Have not reviewed

Seems to have a failure in #43

quietone’s picture

Status: Needs work » Needs review
StatusFileSize
new1.42 KB
new26.14 KB

Seems that I did not restore drupalci.yml as I intended.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

At first was confused why it was removing return statement and realized they weren't used haha.

Think this is an excellent cleanup idea and will keep the comments very clean.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 45: 2722647-45.patch, failed testing. View results

quietone’s picture

Status: Needs work » Needs review

Unrelated failure. Retesting

1) Drupal\Tests\menu_ui\Functional\MenuUiTest::testMenuAdministration
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'/subdirectory/admin/structure/menu/manage/x7kqjmks'
+'/subdirectory/admin/structure/menu/manage/x38ro07w'
quietone’s picture

Status: Needs review » Reviewed & tested by the community

Tests are passing, restoring RTBC

spokje’s picture

Issue tags: +PHPStan-1
quietone’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.5 KB
new25.61 KB

I'm triaging RTBC issues.

I looked at this late last night and after reading the IS and comments I don't see anything left to do.

Today, I skimmed the code and found this.

+++ b/core/modules/search/src/Form/SearchPageAddForm.php
@@ -42,6 +42,7 @@ public function save(array $form, FormStateInterface $form_state) {
+    // foo.

This looks wrong.

I removed that line and made a new patch.

quietone’s picture

Status: Needs review » Reviewed & tested by the community

Removing that unusual comment hasn't cause any test failures. I think it is OK to restore the RTBC here.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new85 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

quietone’s picture

Status: Needs work » Needs review
StatusFileSize
new942 bytes
new25.66 KB

A simple reroll due to changes in the phpstan baseline.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Reroll looks gook. All green.

longwave’s picture

Status: Reviewed & tested by the community » Needs work

#54 failed standards checks:

FILE: /var/www/html/core/lib/Drupal/Core/Database/Connection.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 1395 | ERROR | @return doc comment specified, but function has no
      |       | return statement
      |       | (Drupal.Commenting.FunctionComment.InvalidNoReturn)
----------------------------------------------------------------------
quietone’s picture

Status: Needs work » Needs review
StatusFileSize
new656 bytes
new26.3 KB

Update for changes made in #3364706: Refactor transactions.

quietone’s picture

Status: Needs review » Reviewed & tested by the community

It is a simple reroll, restoring RTBC

xjm’s picture

Status: Reviewed & tested by the community » Needs work

Wow, got a lot more than I expected here. Thanks everyone for your work on this suprisingly challenging issue.

As I started reviewing this I quickly concluded that the change set is way too broad. We are mixing three things:

  1. Simple cleanups where we were declaring null return types which is against the standard but still accurate docs.
  2. Possible underlying bugs where we're declaring the wrong return types -- each of these should be researched with git blame etc. to validate why the difference between documentation and variables exists, to ensure there's not a bug or an inconsistency stretching into other parts of the codebase. Like unused variable removals, they should get their own issues rather than being part of a bulk cleanup.
  3. Actual changes to functionality to return empty string or etc. These changes should come with test coverage!

So, I think we need to split this up. I think there should be:

  • One child issue for the the trivial cases in #1
  • Another for the DB APIs (since they're all intertangled)
  • Various other child issues as we groups that are possibly a single problem in multiple places.
  • And so on from there.

Since the only real code reviews so far are mostly related to the issues in the DB API docs, I think maybe we should create a new meta, and convert this to be the child about DB issues, moving the existing code to the meta (I'd created a combined MR on the meta) and then child issues as appropriate.

Also, API docs issues are much easier to review in GitLab than in patches, because the reviewer needs to be able to read the code that the docs are about. With a diff of a docblock in a patch, I have to open the class locally or on api.d.o, find the relevant lines, and compare. On GitLab I can just expand the shown lines in the same interface I'm doing the review. So, let's go with MRs for this and all the other issues we'll split off.

My notes from the code review I started are below:

  1. +++ b/core/lib/Drupal/Component/Plugin/PluginManagerBase.php
    @@ -118,9 +118,6 @@ protected function handlePluginNotFound($plugin_id, array $configuration) {
    -   * @return string
    -   *   The id of an existing plugin to use when the plugin does not exist.
    -   *
    
    +++ b/core/lib/Drupal/Core/Database/Connection.php
    @@ -1392,9 +1392,6 @@ public function transactionManager(): TransactionManagerInterface|FALSE {
    -   * @return \Drupal\Core\Database\Transaction\TransactionManagerInterface
    -   *   The transaction manager.
    

    Unlike most of the changes, this is a place where the documented behavior is different from what actually happens, which could indicate an underlying bug. Have we researched this? (Will check after I post my diff review.) I think this should also be a separate scope, possibly with tests.

    Edit: Same with the DB connection method.

  2. +++ b/core/lib/Drupal/Core/Config/ConfigBase.php
    @@ -197,11 +197,12 @@ public function set($key, $value) {
    +   * The function does not return anything but will raise an exception
    +   * if there are issues with keys passed.
    +   *
    
    +++ b/core/lib/Drupal/Core/Config/StorableConfigBase.php
    @@ -140,13 +140,14 @@ protected function getSchemaWrapper() {
    +   * The function does not return anything but will raise an exception if
    +   * the value is unsupported in the configuration.
    

    Isn't this documentation redundant with the @throws? Maybe it's worth explaining because it's odd for the method not to return a status, but OTOH ConfigBase::validateName() has the same behavior already with no docs change here. So, if anything, I think this is out of scope and should be in a followup if we want to improve the docs like so.

  3. +++ b/core/lib/Drupal/Core/Database/Query/Merge.php
    @@ -351,6 +351,7 @@ public function key($field, $value = NULL) {
       public function __toString() {
    +    return '';
       }
    

    This is an actual behavior change and seems like it needs its own issue, with tests.

Sorry to be a downer but there is a lot more needed here, and we can get at least parts of it in faster if we split it up. Thanks everyone!

quietone’s picture

Title: Fix Drupal.Commenting.FunctionComment.InvalidNoReturn » [Meta] Fix Drupal.Commenting.FunctionComment.InvalidNoReturn
Issue summary: View changes
Status: Needs work » Active

Downer indeed. :-(

Converting to a Meta

quietone’s picture

Issue summary: View changes
quietone’s picture

Issue summary: View changes
Status: Active » Needs work
quietone’s picture

Status: Needs work » Active
quietone’s picture

Issue summary: View changes
quietone’s picture

Issue summary: View changes
quietone’s picture

Issue summary: View changes
quietone’s picture

Status: Active » Fixed

Thank you to everyone who helped to complete this issue. The last child was recently committed. So, I am closing this as fixed.

Status: Fixed » Closed (fixed)

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

xjm’s picture

Amending attribution.