{"record":{"id":"426b7b8440219040","repo":"symfony/polyfill-mbstring","slug":"mb-str-pad-argument-4-pad-type-must-be-str-pad-left-str-pad","errorCode":null,"errorMessage":"mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH","messagePattern":"mb_str_pad\\(\\): Argument #4 \\(\\$pad_type\\) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Mbstring.php","lineNumber":913,"sourceCode":"\n        if (self::mb_strlen($pad_string, $encoding) <= 0) {\n            if (\\PHP_VERSION_ID < 80000) {\n                trigger_error('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string', \\E_USER_WARNING);\n\n                return false;\n            }\n\n            throw new \\ValueError('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string');\n        }\n\n        if (!\\in_array($pad_type, [\\STR_PAD_RIGHT, \\STR_PAD_LEFT, \\STR_PAD_BOTH], true)) {\n            if (\\PHP_VERSION_ID < 80000) {\n                trigger_error('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH', \\E_USER_WARNING);\n\n                return false;\n            }\n\n            throw new \\ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH');\n        }\n\n        $paddingRequired = $length - self::mb_strlen($string, $encoding);\n\n        if ($paddingRequired < 1) {\n            return $string;\n        }\n\n        switch ($pad_type) {\n            case \\STR_PAD_LEFT:\n                return self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding).$string;\n            case \\STR_PAD_RIGHT:\n                return $string.self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding);\n            default:\n                $leftPaddingLength = floor($paddingRequired / 2);\n                $rightPaddingLength = $paddingRequired - $leftPaddingLength;\n\n                return self::mb_substr(str_repeat($pad_string, $leftPaddingLength), 0, $leftPaddingLength, $encoding).$string.self::mb_substr(str_repeat($pad_string, $rightPaddingLength), 0, $rightPaddingLength, $encoding);","sourceCodeStart":895,"sourceCodeEnd":931,"githubUrl":"https://github.com/symfony/polyfill-mbstring/blob/d3d318bad5e7a1bfbd026009c8bfb8d8f99ae6b6/Mbstring.php#L895-L931","documentation":"Mbstring::mb_str_pad() throws this ValueError when $pad_type is not one of STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH. The polyfill validates the pad type exactly as native PHP does; on PHP < 8.0 it raises an E_USER_WARNING and returns false instead.","triggerScenarios":"Calling mb_str_pad($s, $len, $pad, 4), an arbitrary integer, a string like 'left', or an out-of-scope constant; passing a constant defined by another library that happens to collide numerically.","commonSituations":"Pad type read from config as a string ('left'/'right') instead of the STR_PAD_* constants; typos or invalid mapping tables translating user options to pad types; copying code using constants that don't exist in scope.","solutions":["Pass exactly STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH","Map string config values ('left','right','both') to the constants before calling","in_array($pad_type, [STR_PAD_RIGHT, STR_PAD_LEFT, STR_PAD_BOTH], true) check before calling","Catch \\ValueError on PHP 8+ or rely on warning/false on PHP < 8"],"exampleFix":"// before\nmb_str_pad($s, 10, '.', 'right');\n// after\nmb_str_pad($s, 10, '.', STR_PAD_RIGHT);","handlingStrategy":"validation","validationCode":"$map = ['left' => STR_PAD_LEFT, 'right' => STR_PAD_RIGHT, 'both' => STR_PAD_BOTH];\n$padType = $map[strtolower((string) $padType)] ?? STR_PAD_RIGHT;\nmb_str_pad($string, $length, $padString, $padType);","typeGuard":"function isValidPadType($padType): bool\n{\n    return in_array($padType, [STR_PAD_LEFT, STR_PAD_RIGHT, STR_PAD_BOTH], true);\n}","tryCatchPattern":"try {\n    $padded = mb_str_pad($s, $len, $pad, $padType);\n} catch (\\ValueError $e) {\n    $padded = mb_str_pad($s, $len, $pad, STR_PAD_RIGHT);\n}","preventionTips":["Always use the STR_PAD_* constants, never raw ints or strings","Map string config options to constants before calling","Strict in_array check on pad types coming from user input","Avoid reusing identically named constants from other libraries"],"tags":["php","mbstring","polyfill","valueerror","pad-type"],"backgroundTag":"invalid-enum-value","analyzedSha":"d3d318bad5e7a1bfbd026009c8bfb8d8f99ae6b6","analyzedAt":"2026-09-13T19:34:02.044Z","contentChangedAt":"2026-09-13T19:34:02.044Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}