Skip to content

UnsupportedStringOffsetOperations ​

error on by default PHP ≥ 7.1

Group: Language level migration · PhpStorm name: UnsupportedStringOffsetOperationsInspection

Writing through a string offset as if it were a nested array ($str[0][1] = …) or appending to a string with [] ($str[] = …) is a fatal error since PHP 7.1. When the written-to value is known to be a string, such writes are bugs.

Example ​

php
<?php
/** @param string[] $words */
function mangle(string $text, array $words, $other) {
    $first = $text[0][0];
    $copy  = [$text[1][0]];
    $text[2] = 'z';

    $text[3][1] = 'y';
    $text['a']['b'] .= 'q';
    [$text[4]['c'], $tail] = $words;
    $text[] = 'w';
    $text[5][] = 'v';
    $words[0][1][2] = 'u';

    $words[0][] = 't';
    $words[] = 'ok';
    $other[0][0] = 1;
    $_SESSION['cart'][] = 'item';
    $_COOKIE['a']['b'] = 'c';
}

$str = 'top';
$str[0][0] = 'x';

Reported:

  • line 8: String offsets cannot be written as nested arrays (fatal error).
  • line 9: String offsets cannot be written as nested arrays (fatal error).
  • line 10: String offsets cannot be written as nested arrays (fatal error).
  • line 11: Appending with [] is not supported on strings (fatal error).
  • line 12: String offsets cannot be written as nested arrays (fatal error).
  • line 13: String offsets cannot be written as nested arrays (fatal error).
  • line 15: Appending with [] is not supported on strings (fatal error).

Configure ​

In custos.json:

json
{
  "rules": {
    "UnsupportedStringOffsetOperations": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore UnsupportedStringOffsetOperations

/**
 * @noinspection UnsupportedStringOffsetOperationsInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.