{"record":{"id":"656ed6043f114aa9","repo":"hamcrest/hamcrest-php","slug":"each-argument-or-element-must-be-a-hamcrest-matcher","errorCode":null,"errorMessage":"Each argument or element must be a Hamcrest matcher","messagePattern":"Each argument or element must be a Hamcrest matcher","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"hamcrest/Hamcrest/Util.php","lineNumber":44,"sourceCode":"    public static function wrapValueWithIsEqual($item)\n    {\n        return ($item instanceof Matcher)\n            ? $item\n            : Core\\IsEqual::equalTo($item)\n            ;\n    }\n\n    /**\n     * Throws an exception if any item in $matchers is not a Hamcrest\\Matcher.\n     *\n     * @param array<mixed> $matchers expected to contain only matchers\n     * @throws \\InvalidArgumentException if any item is not a matcher\n     */\n    public static function checkAllAreMatchers(array $matchers): void\n    {\n        foreach ($matchers as $m) {\n            if (!($m instanceof Matcher)) {\n                throw new \\InvalidArgumentException(\n                    'Each argument or element must be a Hamcrest matcher'\n                );\n            }\n        }\n    }\n\n    /**\n     * Returns a copy of $items where each non-Matcher item is replaced by\n     * a Hamcrest\\Core\\IsEqual matcher for the item. If the first and only item\n     * is an array, it is used as the $items array to support the old style\n     * of passing an array as the sole argument to a matcher.\n     *\n     * @param array<mixed> $items contains items and matchers\n     * @return array<Matcher> all items are\n     */\n    public static function createMatcherArray(array $items)\n    {\n        //Extract single array item","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/hamcrest/hamcrest-php/blob/aa726aeff92ee86f82a89ca30dc0af9e01fb3539/hamcrest/Hamcrest/Util.php#L26-L62","documentation":"Hamcrest's Util::checkAllAreMatchers() validates that every element passed to aggregate matchers (allOf, anyOf, etc.) implements the Matcher interface. A non-matcher value (raw value, string, null) is rejected with InvalidArgumentException at construction time.","triggerScenarios":"Passing a plain value instead of a matcher, e.g. allOf('a', 'b'), anyOf([$anArray, 'x']), or including null in the matcher list.","commonSituations":"Forgetting equalTo()/identicalTo() wrappers around literal expected values; mixing raw values into matcher arrays; older code written for APIs that auto-wrapped values.","solutions":["Wrap raw values with equalTo(), identicalTo(), or sameValue()","Ensure every array element is a Matcher instance before constructing allOf/anyOf","Use Util::wrapValueWithIsEqual() (as assertThat does) for dynamic values"],"exampleFix":"// before\nassertThat($value, allOf('a', $b));\n// after\nassertThat($value, allOf(equalTo('a'), $b));","handlingStrategy":"type-guard","validationCode":"foreach ($candidates as $c) {\n    if (!$c instanceof \\Hamcrest\\Matcher) {\n        throw new PreconditionException('not a Matcher: '.gettype($c));\n    }\n}","typeGuard":"function allAreMatchers(array $ms): bool {\n    return array_reduce($ms, fn($ok, $m) => $ok && $m instanceof \\Hamcrest\\Matcher, true);\n}","tryCatchPattern":"try {\n    assertThat($v, allOf(...$matchers));\n} catch (\\InvalidArgumentException $e) {\n    if (strpos($e->getMessage(), 'Hamcrest matcher') === false) throw $e;\n    fail('One of the combined values is not a Matcher; wrap literals with equalTo()');\n}","preventionTips":["Always wrap expected literals in equalTo()/identicalTo()","Import matcher functions so a missing import doesn't resolve to a plain value","Run static analysis; Hamcrest matcher params can be typed against Matcher","Write a small test asserting each element of shared matcher lists implements Matcher"],"tags":["php","hamcrest","matcher","type-check"],"backgroundTag":"type-mismatch","analyzedSha":"aa726aeff92ee86f82a89ca30dc0af9e01fb3539","analyzedAt":"2026-09-15T04:07:57.177Z","contentChangedAt":"2026-09-15T04:07:57.177Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}