{"record":{"id":"c24da760146d89b6","repo":"PHPOffice/PHPWord","slug":"method-function-is-not-defined","errorCode":null,"errorMessage":"Method $function is not defined.","messagePattern":"Method \\$function is not defined\\.","errorType":"exception","errorClass":"BadMethodCallException","httpStatus":null,"severity":"error","filePath":"src/PhpWord/PhpWord.php","lineNumber":149,"sourceCode":"            return $this->collections[$key];\n        }\n\n        // Run add collection item method\n        if (in_array($function, $addCollection)) {\n            $key = ucfirst(str_replace('add', '', $function) . 's');\n\n            $collectionObject = $this->collections[$key];\n\n            return $collectionObject->addItem($args[0] ?? null);\n        }\n\n        // Run add style method\n        if (in_array($function, $addStyle)) {\n            return forward_static_call_array(['PhpOffice\\\\PhpWord\\\\Style', $function], $args);\n        }\n\n        // Exception\n        throw new BadMethodCallException(\"Method $function is not defined.\");\n    }\n\n    /**\n     * Get document properties object.\n     *\n     * @return Metadata\\DocInfo\n     */\n    public function getDocInfo()\n    {\n        return $this->metadata['DocInfo'];\n    }\n\n    /**\n     * Get compatibility.\n     *\n     * @return Metadata\\Compatibility\n     *\n     * @since 0.12.0","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/PhpWord.php#L131-L167","documentation":"PhpWord's magic __call forwards unknown method names to static Style methods, but only for whitelisted add*Style names. Any other undefined method name hits the BadMethodCallException, meaning the method does not exist on PhpWord or the Style facade.","triggerScenarios":"Calling an undefined method on the PhpWord instance, e.g. $phpWord->addWrongStyle() or a typo like $phpWord->addSectionStyle() instead of the supported add*Style methods (addFontStyle, addParagraphStyle, etc.).","commonSituations":"Typo in a style method name; copying code from an older/newer PhpWord version where the method was renamed or removed; expecting instance style methods that were never part of the API.","solutions":["Check the method name against PhpWord's supported add*Style methods (addFontStyle, addParagraphStyle, addTitleStyle, addTableStyle, addNumberingStyle, addHeadingStyle, addCellStyle, addLinkStyle)","Use getStyle()/styles API directly (Style::setFont, etc.) if the shortcut does not exist","Check the PhpWord version documentation for renamed methods"],"exampleFix":"// before\n$phpWord->addParagraphStile('myStyle', ['spacing' => 100]); // typo\n// after\n$phpWord->addParagraphStyle('myStyle', ['spacing' => 100]);","handlingStrategy":"type-guard","validationCode":"if (!method_exists($phpWord, $methodName) && !in_array($methodName, ['addFontStyle','addParagraphStyle','addTitleStyle','addTableStyle','addNumberingStyle','addHeadingStyle','addCellStyle','addLinkStyle'], true)) { throw new \\BadMethodCallException($methodName); }","typeGuard":"function isPhpWordStyleMethod(string $name): bool { return (bool) preg_match('/^(add|set)[A-Z]/', $name) && method_exists(\\PhpOffice\\PhpWord\\PhpWord::class, $name); }","tryCatchPattern":"try { $phpWord->$method(...$args); } catch (\\BadMethodCallException $e) { // fall back to documented style API }","preventionTips":["Consult the PhpWord API docs for exact method names","Use an IDE with autocomplete instead of string-based calls","Pin the PhpWord version and check changelogs on upgrade"],"tags":["phpword","bad-method-call","api-misuse"],"backgroundTag":"method-not-implemented","analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}