PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Cannot get shared component for a pseudo-border.

Error message

Cannot get shared component for a pseudo-border.

What it means

In supervisor mode a Border object forwards reads to the matching border of the shared style component held by its parent. Border::getSharedComponent() maps parentPropertyName ('bottom', 'diagonal', 'left', 'right', 'top') to that shared side; pseudo-borders ('allBorders', 'outline', 'inside') have no single underlying side, so the match() falls through to default and throws 'Cannot get shared component for a pseudo-border.' It fires when property reads are attempted through a pseudo-border attached to a supervisor style.

Source

Thrown at src/PhpSpreadsheet/Style/Border.php:80

    /**
     * Get the shared style component for the currently active cell in currently active sheet.
     * Only used for style supervisor.
     */
    public function getSharedComponent(): self
    {
        /** @var Style $parent */
        $parent = $this->parent;

        /** @var Borders $sharedComponent */
        $sharedComponent = $parent->getSharedComponent();

        return match ($this->parentPropertyName) {
            'bottom' => $sharedComponent->getBottom(),
            'diagonal' => $sharedComponent->getDiagonal(),
            'left' => $sharedComponent->getLeft(),
            'right' => $sharedComponent->getRight(),
            'top' => $sharedComponent->getTop(),
            default => throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.'),
        };
    }

    /**
     * Build style array from subcomponents.
     *
     * @param mixed[] $array
     *
     * @return mixed[]
     */
    public function getStyleArray(array $array): array
    {
        /** @var Style $parent */
        $parent = $this->parent;

        return $parent->getStyleArray([$this->parentPropertyName => $array]);
    }

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Read concrete sides instead of pseudo-borders: getBorders()->getTop()->getBorderStyle(), getBottom(), getLeft(), getRight().
  2. Apply whole-outline/all formatting through applyFromArray (e.g. ['borders' => ['allBorders' => ['borderStyle' => Border::BORDER_THIN]]]) rather than reading pseudo-borders back.
  3. Keep a record of what you applied in your own variables instead of interrogating pseudo-borders.

Example fix

// before
$borderStyle = $sheet->getStyle('A1')->getBorders()->getAllBorders()->getBorderStyle();
// throws: Cannot get shared component for a pseudo-border.

// after
$borders = $sheet->getStyle('A1')->getBorders();
$borderStyle = $borders->getTop()->getBorderStyle(); // concrete side
// to SET all borders at once, use applyFromArray:
$sheet->getStyle('A1')->applyFromArray([
    'borders' => ['allBorders' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN]],
]);
Defensive patterns

Strategy: validation

Validate before calling

// Read concrete sides only; never route property reads through a pseudo-border
$borders = $sheet->getStyle('A1')->getBorders();
$style = $borders->getTop()->getBorderStyle();
if ($style === Border::BORDER_NONE) {
    $style = $borders->getBottom()->getBorderStyle();
}

Type guard

function isConcreteBorder(\PhpOffice\PhpSpreadsheet\Style\Border $b): bool
{
    return in_array($b->getParentPropertyName(), ['top', 'bottom', 'left', 'right', 'diagonal'], true);
}

Try / catch

try {
    $value = $border->getSharedComponent()->getBorderStyle();
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
    // pseudo-border: fall back to a concrete side
    $value = $borders->getTop()->getBorderStyle();
}

Prevention

When it happens

Trigger: Chaining $sheet->getStyle('A1')->getBorders()->getAllBorders()->getBorderStyle() (or any getter that routes through getSharedComponent) — the pseudo-border has no concrete side to read; similarly via getOutline()/getInside(); calling getSharedComponent() directly on a pseudo-border; reading a property on a pseudo-border in supervisor mode.

Common situations: Code that reads back formatting it previously applied via 'allBorders' arrays; generic style-inspection/reflection tooling that walks every Borders sub-object; conditional code that queries the border style of a range after setting outline borders.

Related errors


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