{"record":{"id":"fee067bf852c1d85","repo":"PHPOffice/PhpSpreadsheet","slug":"conditional-is-not-an-expression-cf-rule-condition","errorCode":null,"errorMessage":"Conditional is not an Expression CF Rule conditional","messagePattern":"Conditional is not an Expression CF Rule conditional","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Style/ConditionalFormatting/Wizard/Expression.php","lineNumber":46,"sourceCode":"\n    public function getConditional(): Conditional\n    {\n        /** @var string[] */\n        $expression = $this->adjustConditionsForCellReferences([$this->expression]);\n\n        $conditional = new Conditional();\n        $conditional->setConditionType(Conditional::CONDITION_EXPRESSION);\n        $conditional->setConditions($expression);\n        $conditional->setStyle($this->getStyle());\n        $conditional->setStopIfTrue($this->getStopIfTrue());\n\n        return $conditional;\n    }\n\n    public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface\n    {\n        if ($conditional->getConditionType() !== Conditional::CONDITION_EXPRESSION) {\n            throw new Exception('Conditional is not an Expression CF Rule conditional');\n        }\n\n        $wizard = new self($cellRange);\n        $wizard->style = $conditional->getStyle();\n        $wizard->stopIfTrue = $conditional->getStopIfTrue();\n        $wizard->expression = self::reverseAdjustCellRef((string) ($conditional->getConditions()[0]), $cellRange);\n\n        return $wizard;\n    }\n\n    /**\n     * @param string[] $arguments\n     */\n    public function __call(string $methodName, array $arguments): self\n    {\n        if ($methodName !== 'formula') {\n            throw new Exception('Invalid Operation for Expression CF Rule Wizard');\n        }","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Style/ConditionalFormatting/Wizard/Expression.php#L28-L64","documentation":"Wizard\\Expression::fromConditional() rebuilds an Expression wizard from an existing Conditional, and only CONDITION_EXPRESSION conditionals qualify - rules whose condition is a formula evaluated against each cell. Cell value, text, blanks and date rules use other condition types and are rejected up front.","triggerScenarios":"Calling Expression::fromConditional() on a Conditional built by a CellValue, TextValue, Blanks or DateValue wizard; passing rules whose condition type is cellIs or timePeriod.","commonSituations":"Rule editors defaulting to the expression wizard; round-tripping imported rule sets without checking each rule's type; copy-pasted conversion code.","solutions":["Check $conditional->getConditionType() === Conditional::CONDITION_EXPRESSION first","Use Wizard::fromConditional() to dispatch to the correct wizard automatically","Wrap batch loops with try/catch to skip non-expression rules","Log the actual condition type when a rule is skipped"],"exampleFix":"// before\n$wizard = Wizard\\Expression::fromConditional($conditional); // throws unless type is expression\n\n// after\nuse PhpOffice\\PhpSpreadsheet\\Style\\Conditional;\n\nif ($conditional->getConditionType() === Conditional::CONDITION_EXPRESSION) {\n    $wizard = Wizard\\Expression::fromConditional($conditional);\n} else {\n    $wizard = Wizard::fromConditional($conditional);\n}","handlingStrategy":"type-guard","validationCode":"use PhpOffice\\PhpSpreadsheet\\Style\\Conditional;\n\nif ($conditional->getConditionType() === Conditional::CONDITION_EXPRESSION) {\n    $wizard = Wizard\\Expression::fromConditional($conditional, $cellRange);\n} else {\n    $wizard = Wizard::fromConditional($conditional, $cellRange);\n}","typeGuard":"function isExpressionConditional(Conditional $conditional): bool\n{\n    return $conditional->getConditionType() === Conditional::CONDITION_EXPRESSION;\n}","tryCatchPattern":"use PhpOffice\\PhpSpreadsheet\\Exception as PhpSpreadsheetException;\n\ntry {\n    $wizard = Wizard\\Expression::fromConditional($conditional, $cellRange);\n} catch (PhpSpreadsheetException $e) {\n    $wizard = Wizard::fromConditional($conditional, $cellRange);\n}","preventionTips":["Check for CONDITION_EXPRESSION before calling the Expression factory","Note that Wizard::EXPRESSION and Wizard::FORMULA are the same condition type","Use the generic Wizard::fromConditional() for imported rule sets","Ensure the first condition is the formula string you expect before converting"],"tags":["php","phpspreadsheet","conditional-formatting","wizard","expression","type-mismatch"],"backgroundTag":"conditional-rule-type-mismatch","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}