---
title: "lint/complexity/prefer-null-coalesce"
pagination: false
---

{/* Generated by scripts/gen-rule-docs.py. Edit mapping.toml, not this file. */}

<RuleId id="lint/complexity/prefer-null-coalesce" />

In the [`code-quality`](/docs/rules/sets/code-quality) set. Safe fix. Gated by php-version.

## What it does

Use `??` instead of isset()/array_key_exists() ternaries, if/return chains and foreach lookups.

## Configuration

```toml title="pun.toml"
[lint.rules]
"complexity/prefer-null-coalesce" = "error"
```

To skip it in some files or lines, add an [override](/docs/configuration/pun-toml#lint) or an [ignore comment](/docs/configuration/ignore-comments).

## Options

| Name | Type | Allowed values | Default | Upstream name |
| --- | --- | --- | --- | --- |
| `checks` | `list` | `array-key-exists`, `consecutive-returns`, `foreach` | `["array-key-exists", "consecutive-returns", "foreach"]` | None (Pun's own) |

## Replaces

- [`ArrayKeyExistsTernaryThenValueToCoalescing`](https://getrector.com/rule-detail/array-key-exists-ternary-then-value-to-coalescing-rector) (rector)
- [`ConsecutiveNullCompareReturnsToNullCoalesceQueue`](https://getrector.com/rule-detail/consecutive-null-compare-returns-to-null-coalesce-queue-rector) (rector)
- [`SimplifyForeachToCoalescing`](https://getrector.com/rule-detail/simplify-foreach-to-coalescing-rector) (rector)

## Merged rules

These old IDs are now this rule. A config that still names one stops with an error. See [merged rules](/docs/migrate/merged-rules).

- `lint/complexity/prefer-coalesce-over-foreach`
- `lint/complexity/prefer-coalesce-chain`

[More lint/complexity rules](/docs/rules/lint/complexity) · [Edit this page](https://github.com/shipfastlabs/pun/edit/main/crates/pun-config/rules/mapping.toml)
