Fix "Method ::save() should return int but return statement is missing"

Created on 13 March 2023, over 1 year ago
Updated 15 March 2023, over 1 year ago

Problem/Motivation

\Drupal\Core\Entity\EntityFormInterface:;save() is declared as returning int:

   * @return int
   *   Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

However, 43 implementations of ::save() do not return an int, according to the PHPStan baseline:

 "#^Method Drupal\\\\action\\\\Form\\\\ActionFormBase\\:\\:save\\(\\) should return int but return statement is missing\\.$#"
 "#^Method Drupal\\\\block_content\\\\BlockContentForm\\:\\:save\\(\\) should return int but return statement is missing\\.$#"
 "#^Method Drupal\\\\block_content\\\\BlockContentTypeForm\\:\\:save\\(\\) should return int but return statement is missing\\.$#"
 "#^Method Drupal\\\\book\\\\Form\\\\BookOutlineForm\\:\\:save\\(\\) should return int but return statement is missing\\.$#"
 "#^Method Drupal\\\\comment\\\\CommentForm\\:\\:save\\(\\) should return int but return statement is missing\\.$#"
...

In practice, the return value appears to be unused. ::save() is attached to the Save button in EntityForm::actions(), but the return value is ignored:

    $actions['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
      '#submit' => ['::submitForm', '::save'],
    ];

It seems likely that in almost all cases, this is the only place that ::save() is called.

Steps to reproduce

Proposed resolution

Remove the declared return value from the interface and clean up the baseline.

Remaining tasks

Decide whether to also remove the actual return value from ::save() implementations that do return a value.

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Closed: duplicate

Version

10.1

Component
Entity 

Last updated about 17 hours ago

Created by

🇬🇧United Kingdom longwave UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024