{"record":{"id":"b0ceeebb01f89af3","repo":"PHPOffice/PhpSpreadsheet","slug":"style-is-not-a-conditional-style","errorCode":null,"errorMessage":"Style is not a conditional style","messagePattern":"Style is not a conditional style","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/Worksheet.php","lineNumber":1712,"sourceCode":"\n        return $this;\n    }\n\n    /**\n     * Duplicate conditional style to a range of cells.\n     *\n     * Please note that this will overwrite existing cell styles for cells in range!\n     *\n     * @param Conditional[] $styles Cell style to duplicate\n     * @param string $range Range of cells (i.e. \"A1:B10\"), or just one cell (i.e. \"A1\")\n     *\n     * @return $this\n     */\n    public function duplicateConditionalStyle(array $styles, string $range = ''): static\n    {\n        foreach ($styles as $cellStyle) {\n            if (!($cellStyle instanceof Conditional)) {\n                throw new Exception('Style is not a conditional style');\n            }\n        }\n\n        // Calculate range outer borders\n        [$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($range . ':' . $range);\n\n        // Make sure we can loop upwards on rows and columns\n        if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) {\n            $tmp = $rangeStart;\n            $rangeStart = $rangeEnd;\n            $rangeEnd = $tmp;\n        }\n\n        // Loop through cells and apply styles\n        for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {\n            for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {\n                $this->setConditionalStyles(Coordinate::stringFromColumnIndex($col) . $row, $styles);\n            }","sourceCodeStart":1694,"sourceCodeEnd":1730,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/Worksheet.php#L1694-L1730","documentation":"Worksheet::duplicateConditionalStyle(array $styles, string $range) requires every element of $styles to be an instance of PhpSpreadsheet\\Worksheet\\Conditional (a conditional formatting rule: condition type/operator plus its style). The loop validates each element and throws on the first non-Conditional entry — regular Style objects or plain arrays are not accepted.","triggerScenarios":"$sheet->duplicateConditionalStyle([$sheet->getStyle('A1')], 'B2:B10') — passing a Style from getStyle(); passing the output of getConditionalStyles() mixed with raw arrays; passing [['operator' => '>', 'value' => 5]] style config arrays.","commonSituations":"Confusing duplicateStyle() (regular styles) with duplicateConditionalStyle(); serializing conditional rules to arrays for storage and passing them back unhydrated; copy-pasting config-array examples that predate the object API.","solutions":["Build Conditional objects: (new Conditional())->setConditionType(Conditional::CONDITION_CELLIS)->setOperatorType(Conditional::OPERATOR_GREATERTHAN)->addCondition(10), style via $conditional->getStyle()->applyFromArray([...]), then pass [$conditional]","Filter/assert before the call: array_filter($styles, fn ($s) => $s instanceof Conditional) — or better, fail loudly if the filter removes anything","For plain-cell (non-conditional) formatting use duplicateStyle() instead"],"exampleFix":"// before\n$sheet->duplicateConditionalStyle([$sheet->getStyle('A1')], 'B2:B10'); // Style is not Conditional\n\n// after\n$conditional = new Conditional();\n$conditional->setConditionType(Conditional::CONDITION_CELLIS)\n    ->setOperatorType(Conditional::OPERATOR_GREATERTHAN)\n    ->addCondition(10);\n$conditional->getStyle()->applyFromArray(['font' => ['bold' => true]]);\n$sheet->duplicateConditionalStyle([$conditional], 'B2:B10');","handlingStrategy":"type-guard","validationCode":"use PhpOffice\\PhpSpreadsheet\\Worksheet\\Conditional;\n\n$invalid = array_filter($styles, fn ($s) => !$s instanceof Conditional);\nif ($invalid !== []) {\n    throw new InvalidArgumentException('styles must all be Conditional instances');\n}\n$sheet->duplicateConditionalStyle($styles, 'B2:B10');","typeGuard":"use PhpOffice\\PhpSpreadsheet\\Worksheet\\Conditional;\n\n/** @param mixed[] $styles */\nfunction allConditional(array $styles): bool\n{\n    return array_all($styles, fn ($s) => $s instanceof Conditional);\n    // PHP < 8.2: !in_array(false, array_map(fn ($s) => $s instanceof Conditional, $styles), true)\n}","tryCatchPattern":null,"preventionTips":["Hydrate stored rule arrays back into Conditional objects before use","Keep duplicateStyle() (plain) and duplicateConditionalStyle() (rules) clearly separated in wrappers","Add static analysis annotations Conditional[] wherever rules are passed around"],"tags":["phpspreadsheet","conditional-formatting","type-mismatch","validation"],"backgroundTag":"wrong-argument-type","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}