Skip to content

Commit 469d8fe

Browse files
committed
set default phpVersion to auto and added composer setting
1 parent 987861d commit 469d8fe

File tree

4 files changed

+99
-24
lines changed

4 files changed

+99
-24
lines changed

README.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -176,27 +176,32 @@ await prettier.format(YOUR_CODE, {
176176

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

179-
| Name | Default | Description |
180-
| ------------------ | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
181-
| `phpVersion` | `"8.3"` \* | Allows specifying the PHP version you're using. (See Notes Below) |
182-
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)) |
183-
| `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. |
184-
| `useTabs` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tabs)) |
185-
| `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"`. |
186-
| `trailingCommaPHP` | `true` | If set to `true`, trailing commas will be added wherever possible. <br> If set to `false`, no trailing commas are printed. |
187-
| `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. |
188-
| `requirePragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)) |
189-
| `insertPragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#insert-pragma)) |
190-
191-
### `phpVersion` Configuration Notes:
192-
193-
This setting now defaults to PHP 8.3, however, if the code you are formatting has a `composer.json` with in the current
194-
directory or any parent directory, the plugin will use minimum supported php version from `{"require":{"php":"..."}}`
195-
to set the phpVersion. If no such value is found, it will default to PHP 8.3.
196-
197-
If the phpVersion is not set correctly for your environment, this plugin will product code that could be incompatible
198-
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
199-
that uses features not available in PHP 7.4.
179+
| Name | Default | Description |
180+
| ------------------ | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
181+
| `phpVersion` | `"auto"` \* | Allows specifying the PHP version you're using. (See Notes Below) |
182+
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)) |
183+
| `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. |
184+
| `useTabs` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tabs)) |
185+
| `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"`. |
186+
| `trailingCommaPHP` | `true` | If set to `true`, trailing commas will be added wherever possible. <br> If set to `false`, no trailing commas are printed. |
187+
| `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. |
188+
| `requirePragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)) |
189+
| `insertPragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#insert-pragma)) |
190+
191+
### \* `phpVersion` Configuration Notes :
192+
193+
The default setting `auto`, attempts to auto-detect your supported php versions from a `composer.json` with in the
194+
current directory or any parent directory, the plugin will use minimum supported php version from `{"require":{"php":"..."}}`
195+
to set the phpVersion. If not found, or not able to be parsed, it will default to PHP 8.3.
196+
197+
You set the `phpVersion` to `composer` and this will only use the `composer.json` file to determine the php
198+
version, prettier will crash if the PHP cannot be determined.
199+
200+
You can also set the `phpVersion` to a specific version, such as `7.4`, `8.0`, `8.1`, `8.2`, or `8.3`.
201+
202+
**Please Note:** If the phpVersion is not set correctly for your environment, this plugin will product code that could
203+
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
204+
produce code that uses features not available in PHP 7.4.
200205

201206
## Ignoring code
202207

src/options.mjs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import path from "path";
33

44
const CATEGORY_PHP = "PHP";
55

6+
const LATEST_PHP_VERSION = "8.3";
7+
8+
let getComposerError = "";
9+
610
/**
711
* Detect the minimum PHP version from the composer.json file
812
* @return {string|null} The PHP version to use in the composer.json file, null when not found
@@ -50,24 +54,51 @@ function getComposerPhpVersion() {
5054

5155
if (versionMatch) {
5256
return `${versionMatch[1]}.${versionMatch[2]}`;
57+
} else {
58+
getComposerError = `Could not decode PHP version (${composerJson.require.php}})`;
59+
return null;
5360
}
5461
}
5562
} catch (e) {
56-
// Ignore JSON parsing errors
63+
getComposerError = `Error reading composer.json: ${e.message}`;
5764
}
65+
} else {
66+
getComposerError = "Could not find composer.json";
5867
}
5968

6069
return null;
6170
}
6271

6372
export { getComposerPhpVersion };
6473

74+
/**
75+
* Resolve the PHP version based on the provided options.
76+
*
77+
*/
78+
export function resolvePhpVersion(options) {
79+
if (!options) {
80+
return;
81+
}
82+
if (options.phpVersion === "auto") {
83+
options.phpVersion = getComposerPhpVersion() ?? LATEST_PHP_VERSION;
84+
} else if (options.phpVersion === "composer") {
85+
const v = getComposerPhpVersion();
86+
if (v === null) {
87+
throw new Error(
88+
`Could not determine PHP version from composer; ${getComposerError}`
89+
);
90+
} else {
91+
options.phpVersion = v;
92+
}
93+
}
94+
}
95+
6596
export default {
6697
phpVersion: {
6798
since: "0.13.0",
6899
category: CATEGORY_PHP,
69100
type: "choice",
70-
default: getComposerPhpVersion() ?? "8.3",
101+
default: "auto",
71102
description: "Minimum target PHP version.",
72103
choices: [
73104
{ value: "5.0" },
@@ -87,6 +118,14 @@ export default {
87118
{ value: "8.2" },
88119
{ value: "8.3" },
89120
{ value: "8.4" },
121+
{
122+
value: "composer",
123+
description: "Use the PHP version defined in composer.json",
124+
},
125+
{
126+
value: "auto",
127+
description: `Try composer.json, latest PHP Version (${LATEST_PHP_VERSION})`,
128+
},
90129
],
91130
},
92131
trailingCommaPHP: {

src/parser.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import engine from "php-parser";
2+
import { resolvePhpVersion } from "./options.mjs";
23

34
function parse(text, opts) {
45
const inMarkdown = opts && opts.parentParser === "markdown";
56

67
if (!text && inMarkdown) {
78
return "";
89
}
10+
resolvePhpVersion(opts);
911

1012
// Todo https://github.com/glayzzle/php-parser/issues/170
1113
text = text.replace(/\?>\n<\?/g, "?>\n___PSEUDO_INLINE_PLACEHOLDER___<?");

tests/composer-version/composer-version.spec.mjs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getComposerPhpVersion } from "../../src/options.mjs";
1+
import { getComposerPhpVersion, resolvePhpVersion } from "../../src/options.mjs";
22
import fs from "fs";
33
import path from "path";
44
import os from "os";
@@ -79,6 +79,27 @@ describe("getComposerPhpVer", () => {
7979
expect(getComposerPhpVersion()).toBe(expected);
8080
});
8181

82+
test("phpVersion=composer reads composer.json", () => {
83+
84+
const composerContent = JSON.stringify(
85+
{
86+
require: {
87+
php: ">=5.4",
88+
},
89+
},
90+
null,
91+
2
92+
);
93+
94+
process.chdir(tempDir);
95+
fs.writeFileSync(tempComposerPath, composerContent);
96+
97+
98+
const options = { phpVersion: "composer" };
99+
resolvePhpVersion(options);
100+
expect(options.phpVersion).toBe("5.4");
101+
})
102+
82103
test("returns null when composer.json has no PHP requirement", () => {
83104
const composerContent = JSON.stringify(
84105
{
@@ -98,6 +119,14 @@ describe("getComposerPhpVer", () => {
98119
expect(getComposerPhpVersion()).toBe(null);
99120
});
100121

122+
123+
test("returns error when no composer.json and phpVersion set to composer", () => {
124+
125+
process.chdir(tempDir);
126+
127+
expect(() =>resolvePhpVersion({phpVersion:"composer"})).toThrow();
128+
});
129+
101130
test("returns null when composer.json has invalid PHP requirement", () => {
102131
const composerContent = JSON.stringify(
103132
{

0 commit comments

Comments
 (0)