{"record":{"id":"feb5e78afb170ea6","repo":"sebastianbergmann/phpunit","slug":"s-section-occurs-more-than-once","errorCode":null,"errorMessage":"--%s-- section occurs more than once","messagePattern":"--(.+?)-- section occurs more than once","errorType":"exception","errorClass":"DuplicatePhptSectionException","httpStatus":null,"severity":"error","filePath":"src/Runner/Phpt/Parser.php","lineNumber":133,"sourceCode":"            throw new CannotLoadPhptFileException;\n        }\n\n        $lineNr = 0;\n\n        foreach ($lines as $line) {\n            $lineNr++;\n\n            if (preg_match('/^--([_A-Z]+)--/', $line, $result) === 1) {\n                $section = $result[1];\n\n                if (!in_array($section, self::SUPPORTED_SECTIONS, true) &&\n                    !in_array($section, self::UNSUPPORTED_SECTIONS, true) &&\n                    !in_array($section, self::IGNORED_SECTIONS, true)) {\n                    throw new UnknownPhptSectionException($section);\n                }\n\n                if (isset($sections[$section])) {\n                    throw new DuplicatePhptSectionException($section);\n                }\n\n                $sections[$section]             = '';\n                $sections[$section . '_offset'] = (string) $lineNr;\n\n                continue;\n            }\n\n            if ($section === '') {\n                throw new InvalidPhptFileException(\n                    sprintf(\n                        'PHPT file must not contain text before its first section (line %d)',\n                        $lineNr,\n                    ),\n                );\n            }\n\n            assert(isset($sections[$section]));","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Runner/Phpt/Parser.php#L115-L151","documentation":"In the same line scan, once a section header passes the known-section check, the parser checks isset($sections[$section]); if the same header appears twice anywhere in the file, DuplicatePhptSectionException('--%s-- section occurs more than once') is thrown. Each real section must be unique — section content is not concatenated across occurrences.","triggerScenarios":"A .phpt file with two --EXPECT-- blocks, two --FILE-- sections, or duplicated --INI--/--ENV-- blocks where the author intended settings to accumulate or merge.","commonSituations":"Merging or copy-pasting PHPT tests; appending an updated expectation at the bottom instead of replacing the old one; splitting a test into variants and forgetting to delete the original section.","solutions":["Delete the duplicate so each section header appears exactly once.","When merging content (for example two INI blocks), combine the settings into a single section.","If you need two variants of the same test, split them into two separate .phpt files."],"exampleFix":"-- before\n--EXPECT--\nHello\n--EXPECT--\nHello World\n\n-- after\n--EXPECT--\nHello World","handlingStrategy":"validation","validationCode":"function assertNoDuplicatePhptSections(string $phptFile): void\n{\n    $seen = [];\n\n    foreach (file($phptFile) as $i => $line) {\n        if (preg_match('/^--([_A-Z]+)--/', $line, $m) === 1) {\n            if (isset($seen[$m[1]])) {\n                throw new RuntimeException(sprintf('Duplicate section --%s-- at line %d', $m[1], $i + 1));\n            }\n            $seen[$m[1]] = true;\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    $sections = (new \\PHPUnit\\Runner\\Phpt\\Parser())->parse($phptFile);\n} catch (\\PHPUnit\\Runner\\Phpt\\DuplicatePhptSectionException $e) {\n    // $e->getMessage() names the duplicated section; fix the file and retry\n}","preventionTips":["After merging or copy-pasting PHPT tests, scan the file for repeated headers before running.","Prefer editing an existing section over appending an alternative one."],"tags":["phpunit","phpt","parser","duplicate","section-header"],"backgroundTag":"duplicate-section-definition","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}