Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PHP 8.3 typed class constants #2396

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,17 @@ await prettier.format(YOUR_CODE, {

Prettier for PHP supports the following options. We recommend that all users set the `phpVersion` option.

| Name | Default | Description |
| ------------------ | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `phpVersion` | `"7.0"` | Allows specifying the PHP version you're using. If you're using PHP 7.1 or later, setting this option will make use of modern language features in the printed output. If you're using PHP lower than 7.0, you'll have to set this option or Prettier will generate incompatible code. |
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)) |
| `tabWidth` | `4` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)), The default is `4` based on the `PSR-2` coding standard. |
| `useTabs` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tabs)) |
| `singleQuote` | `false` | If set to `"true"`, strings that use double quotes but do not rely on the features they add, will be reformatted. Example: `"foo" -> 'foo'`, `"foo $bar" -> "foo $bar"`. |
| `trailingCommaPHP` | `true` | If set to `true`, trailing commas will be added wherever possible. <br> If set to `false`, no trailing commas are printed. |
| `braceStyle` | `"per-cs"` | If set to `"per-cs"`, prettier will move open brace for code blocks (classes, functions and methods) onto new line. <br> If set to `"1tbs"`, prettier will move open brace for code blocks (classes, functions and methods) onto same line. |
| `requirePragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)) |
| `insertPragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#insert-pragma)) |
| Name | Default | Description |
| ------------------ | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `phpVersion` | `"8.3"` | Specifies the PHP version used. Setting this option will make use of modern language features in the printed output, but using a value higher than your PHP will cause Prettier to generate incompatible code. |
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)) |
| `tabWidth` | `4` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)), The default is `4` based on the `PSR-2` coding standard. |
| `useTabs` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tabs)) |
| `singleQuote` | `false` | If set to `"true"`, strings that use double quotes but do not rely on the features they add, will be reformatted. Example: `"foo" -> 'foo'`, `"foo $bar" -> "foo $bar"`. |
| `trailingCommaPHP` | `true` | If set to `true`, trailing commas will be added wherever possible. <br> If set to `false`, no trailing commas are printed. |
| `braceStyle` | `"per-cs"` | If set to `"per-cs"`, prettier will move open brace for code blocks (classes, functions and methods) onto new line. <br> If set to `"1tbs"`, prettier will move open brace for code blocks (classes, functions and methods) onto same line. |
| `requirePragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)) |
| `insertPragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#insert-pragma)) |

## Ignoring code

Expand Down
16 changes: 3 additions & 13 deletions src/options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@ export default {
since: "0.13.0",
category: CATEGORY_PHP,
type: "choice",
default: "7.0",
default: "8.3",
description: "Minimum target PHP version.",
choices: [
{ value: "5.0" },
{ value: "5.1" },
{ value: "5.2" },
{ value: "5.3" },
{ value: "5.4" },
{ value: "5.5" },
{ value: "5.6" },
{ value: "7.0" },
{ value: "7.1" },
{ value: "7.2" },
{ value: "7.3" },
{ value: "7.4" },
{ value: "8.0" },
{ value: "8.1" },
{ value: "8.2" },
{ value: "8.3" },
{ value: "8.4" },
],
},
trailingCommaPHP: {
Expand Down
1 change: 1 addition & 0 deletions src/parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function parse(text, opts) {
const parser = new engine({
parser: {
extractDoc: true,
version: opts.phpVersion,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't be needed once glayzzle/php-parser#1150 has landed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should use the php version a user sets (https://github.com/prettier/plugin-php#configuration) instead of using the php-parser default though? If I remember correctly, the user setting has no effect on the parser without this line.

The php-parser default now being 8.3 makes it unnecessary for most people, but if someone set it to 8.0 it would use unsupported syntax. Then again, only 8.2+ are currently supported by the php team, so maybe it's time to drop all the 5.x and 7.x versions and the default changed from "7.0" to "8.3"?

Copy link
Collaborator

@czosel czosel Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the idea is that we always try to parse with the latest PHP version, and only use the prettier version setting to control our printing (i.e. don't print features that are unsupported in the configured version). Since glayzzle/php-parser#1150 is now landed, I don't really see a reason to change this. I'd be very open to update the default for our version setting to something more reasonable though! 👍

},
ast: {
withPositions: true,
Expand Down
1 change: 1 addition & 0 deletions src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,7 @@ function printNode(path, options, print) {
node.final ? "final " : "",
node.visibility ? [node.visibility, " "] : "",
"const",
node.type ? [node.nullable ? " ?" : " ", print("type")] : "",
firstVariable ? [" ", firstVariable] : "",
indent(printed.slice(1).map((p) => [",", hardline, p])),
]);
Expand Down
Loading