---
title: "lint/style/prefer-class-constant"
pagination: false
---

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

<RuleId id="lint/style/prefer-class-constant" />

In the [`modernize`](/docs/rules/sets/modernize) and [`coding-style`](/docs/rules/sets/coding-style) sets. Unsafe fix: it can change behaviour.

## What it does

Use `Foo::class` instead of class-name strings.

## Configuration

```toml title="pun.toml"
[lint.rules]
"style/prefer-class-constant" = "error"
```

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

## Options

| Name | Type | Allowed values | Default | Upstream name |
| --- | --- | --- | --- | --- |
| `namespaced-only` | `bool` | Any | `false` | None (Pun's own) |
| `checks` | `list` | `class-name-resolution`, `string-class-name` | `["class-name-resolution", "string-class-name"]` | None (Pun's own) |

## Replaces

- [`class_keyword`](https://cs.symfony.com/doc/rules/language_construct/class_keyword.html) (php-cs-fixer, risky)
- [`StringClassNameToClassConstant`](https://getrector.com/rule-detail/string-class-name-to-class-constant-rector) (rector)
- [`UseClassKeywordForClassNameResolution`](https://getrector.com/rule-detail/use-class-keyword-for-class-name-resolution-rector) (rector)
- [`no-literal-namespace-string`](https://mago.carthage.software/1.61.0/en/tools/linter/rules/#no-literal-namespace-string) (mago-linter)

## 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/style/prefer-class-keyword`
- `lint/style/prefer-class-name-constant`

Rector converts only names with a namespace, such as `'App\Models\User'`; PHP-CS-Fixer's class_keyword also converts global names such as `'Throwable'`. `namespaced-only = true` keeps to Rector's checks; `pun migrate` sets it when the rule comes from rector.php and not from class_keyword. `checks` keeps to the Rector rules rector.php runs: `withPhpSets()` runs only `StringClassNameToClassConstantRector` (`checks = ["string-class-name"]`), which leaves `'App\Foo::bar'` alone.

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