sebastianbergmann/comparator · error · ComparisonFailure

does not match expected type " ".

Error message

%s does not match expected type "%s".

What it means

Thrown by TypeComparator::assertEquals, the fallback comparator whose accepts() returns true for any pair of values. It fires whenever the expected and actual values have different PHP types (e.g. expected int but actual string) — it is a generic type-mismatch guard, and the faulting input is the actual value whose gettype() differs from the expected value's.

Solutions

  1. Check gettype()/get_debug_type() on both values; the mismatch is a type difference, not a value difference.
  2. Cast or convert the actual value to the expected type only when the type coercion is intentional and safe.
  3. Tighten input sources (strict_types=1, parameter type declarations) so values arrive with the correct type.
  4. Fix the producing code path (e.g. a function returning null or string instead of int) rather than loosening the assertion.
  5. Ensure a more specific comparator accepts the pair first if a structural comparison was intended instead of a type check.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at src/TypeComparator.php:35 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of sebastianbergmann/comparator@00837a9d22 (2026-09-15). Data as JSON: /api/errors/f599273dd2f538c1. Report an issue: GitHub.

Appendix: source

Thrown at src/TypeComparator.php:35

 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for sebastian/comparator
 *
 * @internal This class is not covered by the backward compatibility promise for sebastian/comparator
 */
final class TypeComparator extends Comparator
{
    public function accepts(mixed $expected, mixed $actual): bool
    {
        return true;
    }

    /**
     * @throws ComparisonFailure
     * @throws ObjectNotSupportedException
     */
    public function assertEquals(mixed $expected, mixed $actual, float $delta = 0.0, bool $canonicalize = false, bool $ignoreCase = false): void
    {
        if (gettype($expected) !== gettype($actual)) {
            throw new ComparisonFailure(
                $expected,
                $actual,
                // we don't need a diff
                '',
                '',
                sprintf(
                    '%s does not match expected type "%s".',
                    $this->exporter()->shortenedExport($actual),
                    gettype($expected),
                ),
                $this->contextLines(),
            );
        }
    }
}

View on GitHub (pinned to 00837a9d22)