Skip to content

feat!: bump default formatting version to composer version with fallaback to latest supported PHP #2434

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

Merged
merged 17 commits into from
Jul 16, 2025

Conversation

czosel
Copy link
Collaborator

@czosel czosel commented Jul 8, 2025

BREAKING CHANGE: If you didn't set the phpVersion option explicitly, the formatting will try and located the minimum supported version from your composer.json (require.php), else the latest supported PHP version (currently: 8.4) now (instead of 7.0).

As suggested in #2396 by @mreiden

@czosel czosel force-pushed the bump-default-8.3 branch 6 times, most recently from a81d0fe to 1f20ced Compare July 8, 2025 10:06
@cseufert
Copy link
Collaborator

cseufert commented Jul 9, 2025

Just an idea, should we have a guess at this from the composer.json file at the project root? or is that too complicated? Look for

...
  "require": {
    "php": ">=8.2",
  }
...

@czosel
Copy link
Collaborator Author

czosel commented Jul 9, 2025

@cseufert Sounds reasonable! I don't have much experience with composer - does one typically set the version constraint to be equal or above the PHP version that is actually in use? If yes, this would probably make sense. We could then implement a logic like this:

  • Use phpVersion from prettier config
  • If unset, use minimum PHP version from composer.json
  • If unset, use default version

If this logic makes sense, we might still want to merge this PR (just to keep the fallback to a realistic value).

@hackel
Copy link
Contributor

hackel commented Jul 9, 2025

config.platform.php in composer.json is for the specific PHP version being used in production. You'll probably want to look at this value as well.

@cseufert
Copy link
Collaborator

I have added basic detection for min php version from composer.json package. Should work for most cases, falls back to 8.3 as default if composer.json not found, or invalid.

Copy link
Collaborator Author

@czosel czosel left a comment

Choose a reason for hiding this comment

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

Just some minor things, nice work!!

@cseufert
Copy link
Collaborator

@czosel
I feel like this is ready to merge, however I did have to change the jest configuration, I think its for the best though.

@cseufert cseufert requested a review from Copilot July 11, 2025 00:39
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR bumps the default PHP formatting target to 8.3, introduces automatic version detection via composer.json, and updates snapshots/tests to explicitly include version and trailing commas.

  • Added getComposerPhpVer utility and set phpVersion default to getComposerPhpVer() ?? "8.3", plus added "8.4" to the choices.
  • Updated run_spec invocations and Jest snapshots across many test suites to include explicit phpVersion: "7.0" and trailing commas.
  • Expanded Jest’s testRegex to match all .spec.js/.spec.mjs files and added new tests for getComposerPhpVer.

Reviewed Changes

Copilot reviewed 46 out of 47 changed files in this pull request and generated 1 comment.

File Description
src/options.mjs Implements dynamic PHP version detection and bumps fallback to 8.3; adds 8.4 choice
jest.config.mjs Broadens test matching pattern to .*\.spec\.m?js$
tests/composer-version/composer-version.spec.mjs New tests covering getComposerPhpVer behavior
tests/trailing_commas/jsfmt.spec.mjs Adds explicit phpVersion to formatting specs
Comments suppressed due to low confidence (2)

src/options.mjs:70

  • Consider adding a unit test to verify that when no composer.json is found (or parsing fails), getComposerPhpVer() returns null and the default falls back to "8.3" as intended.
    default: getComposerPhpVer() ?? "8.3",

src/options.mjs:10

  • [nitpick] The function name getComposerPhpVer could be expanded to getComposerPhpVersion for clarity and consistency with the phpVersion option name.
function getComposerPhpVer() {

@czosel
Copy link
Collaborator Author

czosel commented Jul 11, 2025

Apart from the nitpick this looks good to me now 👍

@cseufert
Copy link
Collaborator

I have updated the docs, however thinking about it further, maybe auto should be the default, and prettier should return an error if the proejcts php version cannot be determined?

@czosel
Copy link
Collaborator Author

czosel commented Jul 14, 2025

I have updated the docs, however thinking about it further, maybe auto should be the default, and prettier should return an error if the proejcts php version cannot be determined?

Interesting idea, that would make it easier to debug when the composer lookup is not working (instead of the silent fallback to 8.3). The downside is that for very simple setups without composer.json we would force users to set the version, where before we'd just assume a default.

Another idea would be to have two "special" settings:

  • auto: composer.json or latest supported version (default, exactly as you currently implemented it)
  • composer: Read from composer.json or return an error if version could not be determined
  • (explicit version setting, same as now)

What do you think? Too much or just right? 🙃

czosel and others added 10 commits July 15, 2025 12:15
BREAKING CHANGE: If you didn't set the `phpVersion` option explicitly,
the formatting will assume PHP 8.3 now (instead of 7.0).
- added wildcard major versions
- updated test for null return
- added invalid json test case
- automatically use latest version
- phpVersion is always a number now
- moved isMinVersion to simple number comparison
@cseufert cseufert changed the title feat!: bump default formatting version to 8.3 feat!: bump default formatting version to composer version with fallaback to latest supported PHP Jul 15, 2025
@cseufert
Copy link
Collaborator

I have re-implemented the default version, check out the readme, but in essence auto is default, composer is commposer or fail, and existing versions still work. Fallback is now the latest version supported (8.4), I have re-based this on the main branch as well, and updated the test logic for some of the 8.4 tests.

Apologies, as this has turned into a rather large PR though now, as multiple things have changed at once.

plugin-php/README.md

Lines 191 to 205 in 1ea1177

### \* `phpVersion` Configuration Notes :
The default setting `auto`, attempts to auto-detect your supported php versions from a `composer.json` with in the
current directory or any parent directory, the plugin will use a minimum supported php version from
`{"require":{"php":"..."}}` to set the phpVersion. If not found or not able to be parsed, it will default to latest
supported PHP version, which is currently `8.4`.
You set the `phpVersion` to `composer` and this will only use the `composer.json` file to determine the php
version, prettier will crash if the PHP cannot be determined.
You can also set the `phpVersion` to a specific version, such as `7.4`, `8.0`, `8.1`, `8.2`, or `8.3`.
**Please Note:** If the phpVersion is not set correctly for your environment, this plugin will product code that could
be incompatible with your PHP runtime. For example, if you are using PHP 7.4, but the plugin is set to PHP 8.3, it will
produce code that uses features not available in PHP 7.4.

@czosel
Copy link
Collaborator Author

czosel commented Jul 15, 2025

@cseufert Looks great!! 💯 A few very small cleanups, then we're good to go 🚀

@czosel
Copy link
Collaborator Author

czosel commented Jul 16, 2025

All good now! Thanks @cseufert 💯

@czosel czosel merged commit 7b4d64b into prettier:main Jul 16, 2025
15 checks passed
@czosel
Copy link
Collaborator Author

czosel commented Jul 16, 2025

Released in v0.24.0 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants