Problem/Motivation

The code contains non-canonical scalar type casts, like (boolean) and (integer), which are deprecated in PHP 8.5. The alternate type names are not supported in parameter, return, or property types, either.

https://php.watch/versions/8.5/boolean-double-integer-binary-casts-depre...

Steps to reproduce

  protected function applyFormStateToConfiguration(FormStateInterface $form_state) {
    $values = $form_state->getValues();
    $default_configuration = $this->defaultConfiguration();
    foreach ($values as $key => $value) {
      if (array_key_exists($key, $this->configuration)) {
        if (is_bool($default_configuration[$key])) {
          $this->configuration[$key] = (boolean) $value;
        }
        elseif (is_int($default_configuration[$key])) {
          $this->configuration[$key] = (integer) $value;
        }
        else {
          $this->configuration[$key] = $value;
        }
      }
    }
  }

and

  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration = $form_state->getValues();
    $this->configuration['debug'] = (boolean) $this->configuration['debug'];
  }

and more.

Search the code using the regex \(integer\)|\(boolean\)|\(double\)|\(binary\) to find type casts. There may be parameters, return types, or property types that need to be updated as well.

Proposed resolution

int instead of integer
float instead of double
bool instead of boolean
string instead of binary

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork webform-3558621

Command icon 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

solideogloria created an issue. See original summary.

solideogloria’s picture

Issue summary: View changes
solideogloria’s picture

Issue summary: View changes
solideogloria’s picture

Issue summary: View changes
liam morland’s picture

The proposed resolution looks good to me.

solideogloria changed the visibility of the branch 3558621-php-8.5-compatibility-2 to hidden.

solideogloria’s picture

This is a section I'm not sure about:

    $form['additional']['cast'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Cast posted element value and custom data'),
      '#description' => $this->t('If checked, posted element values will be cast to integers, floats, and booleans as needed. Custom data can be cast by placing the desired type in parentheses before the value or token. (i.e. "(int) [webform_submission:value:total]" or "(int) 100")') .
      '<br/>' .
      '<br/>' .
      $this->t('For custom data, the casts allowed are:') .
      '<ul>' .
      '<li>' . $this->t('@cast - cast to @type', ['@cast' => '(int), (integer)', '@type' => 'integer']) . '</li>' .
      '<li>' . $this->t('@cast - cast to @type', ['@cast' => '(float), (double), (real)', '@type' => 'float']) . '</li>' .
      '<li>' . $this->t('@cast - cast to @type', ['@cast' => '(bool), (boolean)', '@type' => 'boolean']) . '</li>' .
      '</ul>',
      '#return_value' => TRUE,
      '#default_value' => $this->configuration['cast'],
    ];

Should the allowed casts be updated in this element? I left this as-is.

solideogloria’s picture

Status: Active » Needs review

liam morland’s picture

Status: Needs review » Needs work

I committed the merge request. That is the easy one. I think the text referred-to in #7 should be updated so that it only suggests using the modern casts or perhaps has the old ones in brackets.

solideogloria’s picture

If the text is updated, then should the casts themselves be deprecated if an old type? Currently, it has this:

      elseif (is_string($value) && preg_match('/^\((int|integer|bool|boolean|float|double|real)\)\s*(.+)$/', $value, $match)) {
        $type_cast = $match[1];
        $type_value = $match[2];
        switch ($type_cast) {
          case 'int':
          case 'integer':
            $data[$key] = (int) $type_value;
            break;

          case 'bool':
          case 'boolean':
            $data[$key] = (bool) $type_value;
            break;

          case 'float':
          case 'double':
          case 'real':
            $data[$key] = (float) $type_value;
            break;
        }

We could log a warning or something if an old type is used.

liam morland’s picture

It should still support the old way so that current forms will continue to work. The documentation should suggest that people use the new way.

solideogloria’s picture

Status: Needs work » Needs review

liam morland’s picture

Title: PHP 8.5 compatibility » Use canonical type names
Status: Needs review » Fixed

Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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