PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Invalid Operation for Duplicates CF Rule Wizard

Error message

Invalid Operation for Duplicates CF Rule Wizard

What it means

The Duplicates wizard accepts exactly two magic methods from its OPERATORS map: duplicates() (highlight duplicate values) and unique() (highlight unique values). __call rejects every other method name with this exception, so there is no third mode to invoke.

Source

Thrown at src/PhpSpreadsheet/Style/ConditionalFormatting/Wizard/Duplicates.php:67

        ) {
            throw new Exception('Conditional is not a Duplicates CF Rule conditional');
        }

        $wizard = new self($cellRange);
        $wizard->style = $conditional->getStyle();
        $wizard->stopIfTrue = $conditional->getStopIfTrue();
        $wizard->inverse = $conditional->getConditionType() === Conditional::CONDITION_UNIQUE;

        return $wizard;
    }

    /**
     * @param mixed[] $arguments
     */
    public function __call(string $methodName, array $arguments): self
    {
        if (!array_key_exists($methodName, self::OPERATORS)) {
            throw new Exception('Invalid Operation for Duplicates CF Rule Wizard');
        }

        $this->inverse(self::OPERATORS[$methodName]);

        return $this;
    }
}

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Use duplicates() or unique() only
  2. Validate dynamic method names against ['duplicates', 'unique'] before invoking
  3. Catch PhpSpreadsheetException around dynamic calls
  4. Remember the inverse mode is chosen at construction too: (new Wizard($range))->newRule(Wizard::UNIQUE)

Example fix

// before
$duplicatesWizard = (new Wizard('A1:E10'))->newRule(Wizard::DUPLICATES);
$duplicatesWizard->distinct();   // throws

// after
$duplicatesWizard->duplicates(); // or ->unique()
Defensive patterns

Strategy: validation

Validate before calling

$allowed = ['duplicates', 'unique'];
$method = $config['mode'];
if (!in_array($method, $allowed, true)) {
    throw new InvalidArgumentException('Unknown Duplicates mode: ' . $method);
}
$wizard->$method();

Try / catch

use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;

try {
    $wizard->$method();
} catch (PhpSpreadsheetException $e) {
    throw new InvalidArgumentException('Invalid Duplicates mode: ' . $method, 0, $e);
}

Prevention

When it happens

Trigger: ->distinct(); ->repeated(); ->uniqueValues(); any method name other than duplicates() or unique().

Common situations: SQL naming habits (DISTINCT) carried into the fluent API; guessing symmetric method names; dynamic invocation from configuration or user input.

Related errors


AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17). Data as JSON: /api/errors/0b38739c2cc83ea5. Report an issue: GitHub.