{"record":{"id":"2b27e360ab3ec8f4","repo":"rectorphp/rector","slug":"is-invalid-for-the-start-character-in-the-brac","errorCode":null,"errorMessage":"\"-\" is invalid for the start character in the brackets","messagePattern":"\"-\" is invalid for the start character in the brackets","errorType":"validation","errorClass":"InvalidEregException","httpStatus":null,"severity":"error","filePath":"rules/Php70/EregToPcreTransformer.php","lineNumber":216,"sourceCode":"        }\n        // retype\n        $i = (int) $i;\n        return $i;\n    }\n    /**\n     * @return float[]|int[]|string[]\n     */\n    private function processSquareBracket(string $s, int $i, int $l, string $cls, bool $start): array\n    {\n        do {\n            if ($s[$i] === '[' && $i + 1 < $l && strpos('.=:', $s[$i + 1]) !== \\false) {\n                /** @var string $cls */\n                [$cls, $i] = $this->processCharacterClass($s, $i, $cls);\n            } else {\n                $a = $s[$i];\n                ++$i;\n                if ($a === '-' && !$start && ($i >= $l || $s[$i] !== ']')) {\n                    throw new InvalidEregException('\"-\" is invalid for the start character in the brackets');\n                }\n                if ($i < $l && $s[$i] === '-') {\n                    $b = $s[++$i];\n                    if ($b === ']') {\n                        $cls .= $this->_ere2pcre_escape($a) . '\\-';\n                        break;\n                    }\n                    if (ord($a) > ord($b)) {\n                        $errorMessage = sprintf('an invalid character range %d-%d\"', (int) $a, (int) $b);\n                        throw new InvalidEregException($errorMessage);\n                    }\n                    $cls .= $this->_ere2pcre_escape($a) . '-' . $this->_ere2pcre_escape($b);\n                    ++$i;\n                } else {\n                    $cls .= $this->_ere2pcre_escape($a);\n                }\n            }\n            $start = \\false;","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/rules/Php70/EregToPcreTransformer.php#L198-L234","documentation":"Inside processSquareBracket(), a '-' was consumed as a class member at a position where it is not the first character and is not immediately followed by ']' — i.e. it is neither a leading/trailing literal dash nor the start of a valid range, which POSIX ERE rejects (EregToPcreTransformer.php:216). The InvalidEregException with message '\"-\" is invalid for the start character in the brackets' aborts the conversion.","triggerScenarios":"Rector (Php70 set / EregToPregMatchRector, target PHP >= 7.0) visits an ereg-family call whose literal pattern has a '-' mid-class where a character should start, e.g. `ereg('[0-9-x]', $s)` (after the range 0-9, '-' is followed by 'x', not ']'), or `split('[abc-', $s)` style content where '-' precedes a non-']' character at class end. Note '[a-]' and '[-a]' are accepted; '[ab-9]' is not.","commonSituations":"Character classes edited by appending '-x' to an existing range ('[0-9]' -> '[0-9-x]') which silently changes '-' into an atom position; glob/habit thinking where '-' separates members; legacy patterns that relied on lenient behavior of other engines.","solutions":["Rewrite the class in the flagged file so '-' is a range start, range end, or escaped/edge literal: '[0-9-x]' -> '[0-9x]' or '[0-9\\-x]'.","If the dash is literal, move it to the end ('[0-9x-]') or escape it ('[0-9\\-x]').","Re-run rector; preflight literal ereg patterns for '-' not adjacent to '[' or ']' inside classes."],"exampleFix":"// before - '-' at atom position inside class\nereg('[0-9-x]', $subject);\n\n// after - dash handled explicitly\nereg('[0-9x-]', $subject); // rector: preg_match('#[0-9x-]#m', $subject)","handlingStrategy":"try-catch","validationCode":"// quick lint: '-' mid-class not adjacent to '[' or ']'\nfunction dashMisplacedInClass(string $p): bool {\n    return (bool) preg_match('~(?<!^)(?<!\\[)-(?![\\]\\\\])~', $p);\n}","typeGuard":null,"tryCatchPattern":"try {\n    $pcre = $eregToPcreTransformer->transform($pattern, $ignoreCase);\n} catch (InvalidEregException $e) {\n    // '-' used where a class member must start — move it to the class edge or escape it\n    continue;\n}","preventionTips":["Put literal dashes at the very start or end of a class ('[-a]', '[a-]') or escape them ('\\-').","When appending to an existing range class, re-check where '-' lands: '[0-9-x]' is invalid, '[0-9x-]' is not.","Review character classes once before the migration instead of per-rector-run."],"tags":["rector","php","regex","posix-ere","character-class","migration"],"backgroundTag":"invalid-regex-syntax","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}