{"record":{"id":"4afb50e8ed768da2","repo":"sebastianbergmann/phpunit","slug":"option-s-requires-a-positive-integer-value","errorCode":null,"errorMessage":"Option %s requires a positive integer value","messagePattern":"Option (.+?) requires a positive integer value","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/TextUI/Configuration/Cli/Builder.php","lineNumber":1712,"sourceCode":"    {\n        if ($value === null || $value === '') {\n            throw new Exception(\n                sprintf('Option %s requires a non-empty value', $option),\n            );\n        }\n\n        return $value;\n    }\n\n    /**\n     * @throws Exception\n     *\n     * @return positive-int\n     */\n    private function requirePositiveIntValue(?string $value, string $option): int\n    {\n        if (!is_numeric($value)) {\n            throw new Exception(\n                sprintf('Option %s requires a positive integer value', $option),\n            );\n        }\n\n        $intValue = (int) $value;\n\n        if ($intValue < 1) {\n            throw new Exception(\n                sprintf('Option %s requires a positive integer value', $option),\n            );\n        }\n\n        return $intValue;\n    }\n}\n","sourceCodeStart":1694,"sourceCodeEnd":1728,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/TextUI/Configuration/Cli/Builder.php#L1694-L1728","documentation":"requirePositiveIntValue() (src/TextUI/Configuration/Cli/Builder.php:1709) currently validates exactly one option: --diff-context (the context lines shown in failure diffs, Builder.php:1279). The first check fails when is_numeric() is false, i.e. the value is not even numeric. --diff-context accepts only an integer line count, not keywords or ranges.","triggerScenarios":"`vendor/bin/phpunit --diff-context=auto` (users expect a keyword); `--diff-context=3,5`; `--diff-context=none`; a value injected from an environment variable that contains a non-numeric string.","commonSituations":"Copying diff(1) or git diff -U semantics onto PHPUnit; CI configuration templating injecting a wrong type; documentation confusion about what --diff-context means.","solutions":["Pass a plain integer: `--diff-context=3`","Check the env/CI variable that supplies the value and correct its type","Omit the flag to use PHPUnit's default context size"],"exampleFix":"# before\nvendor/bin/phpunit --diff-context=auto\n\n# after\nvendor/bin/phpunit --diff-context=3","handlingStrategy":"validation","validationCode":"$diffContext = getenv('PHPUNIT_DIFF_CONTEXT') ?: '3';\n\nif (!is_numeric($diffContext)) {\n    fwrite(STDERR, \"--diff-context must be an integer, got: {$diffContext}\" . PHP_EOL);\n    exit(2);\n}\n\n$argv[] = '--diff-context=' . (int) $diffContext;","typeGuard":null,"tryCatchPattern":"try {\n    (new \\PHPUnit\\TextUI\\CliArguments\\Builder)->fromParameters($argv);\n} catch (\\PHPUnit\\TextUI\\CliArguments\\Exception $e) {\n    if (str_contains($e->getMessage(), 'requires a positive integer value')) {\n        // fix the --diff-context value in whatever produced argv\n    }\n}","preventionTips":["--diff-context takes an integer line count only; there is no 'auto' or 'none' keyword","Type-check values sourced from env vars or CI templates before forwarding them","When in doubt, omit the option and accept the default"],"tags":["phpunit","cli","option-value","numeric-validation","diff-output"],"backgroundTag":"invalid-cli-option-value","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}