sebastianbergmann/phpunit · error · InvalidPhptFileException

--%s-- section is empty

Error message

--%s-- section is empty

What it means

ensureCodeIsNotEmpty() is the last stop of parse(): when no early return applies (FILE is absent or empty) and the code section is FILE or FILEEOF, it throws InvalidPhptFileException(sprintf('--%s-- section is empty', $codeSection)). The code section exists but has no content, so there is nothing to run.

Source

Thrown at src/Runner/Phpt/Parser.php:340

    /**
     * @param array<non-empty-string, string> $sections
     * @param non-empty-string                $codeSection
     *
     * @throws InvalidPhptFileException
     */
    private function ensureCodeIsNotEmpty(array $sections, string $codeSection): void
    {
        if (isset($sections['FILE']) && $sections['FILE'] !== '') {
            return;
        }

        if ($codeSection === 'FILE_EXTERNAL') {
            throw new InvalidPhptFileException(
                'File referenced by --FILE_EXTERNAL-- section is empty',
            );
        }

        throw new InvalidPhptFileException(
            sprintf(
                '--%s-- section is empty',
                $codeSection,
            ),
        );
    }
}

View on GitHub (pinned to f123cdb2a2)

Solutions

  1. Add the PHP code to the empty --FILE-- (or --FILEEOF--) section.
  2. If the test genuinely runs no code, reconsider whether it should be a PHPT test at all — a plain PHPUnit TestCase is a better fit.

Example fix

-- before
--FILE--
--EXPECT--
Hello

-- after
--FILE--
echo 'Hello';
--EXPECT--
Hello
Defensive patterns

Strategy: validation

Validate before calling

// verify the --FILE-- section has a body (next line after the header is not another header)
$body = (string) file_get_contents($phptFile);
if (preg_match('/--FILE--\R(--[_A-Z]+--|\s*$)/', $body) === 1) {
    throw new RuntimeException('--FILE-- section is empty in ' . $phptFile);
}

Prevention

When it happens

Trigger: A .phpt whose --FILE-- section contains no lines (header immediately followed by the next header), or a --FILEEOF-- section whose rtrim()ed content is empty.

Common situations: Creating a test from a skeleton and not filling in the code yet; editing away the body; misunderstanding FILEEOF (its content is rtrim()ed of trailing newlines and used as FILE).

Related errors


AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23). Data as JSON: /api/errors/84e6a94b226e7b4a. Report an issue: GitHub.