forked from prettier/plugin-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.mjs
More file actions
151 lines (136 loc) · 4.11 KB
/
options.mjs
File metadata and controls
151 lines (136 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import fs from "fs";
import path from "path";
const CATEGORY_PHP = "PHP";
// prettier-ignore
const SUPPORTED_PHP_VERSIONS = [
5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6,
7.0, 7.1, 7.2, 7.3, 7.4,
8.0, 8.1, 8.2, 8.3, 8.4,
];
export const LATEST_SUPPORTED_PHP_VERSION = Math.max(...SUPPORTED_PHP_VERSIONS);
let getComposerError = "";
/**
* Detect the minimum PHP version from the composer.json file
* @return {number|null} The PHP version to use in the composer.json file, null when not found
*/
function getComposerPhpVersion() {
// Try to find composer.json
const currentDir = process.cwd();
let composerPath = null;
const directComposerPath = path.join(currentDir, "composer.json");
if (fs.existsSync(directComposerPath)) {
composerPath = directComposerPath;
}
if (!composerPath) {
let searchDir = path.dirname(currentDir);
while (searchDir !== path.parse(searchDir).root) {
const potentialComposerPath = path.join(searchDir, "composer.json");
if (fs.existsSync(potentialComposerPath)) {
composerPath = potentialComposerPath;
break;
}
searchDir = path.dirname(searchDir);
}
}
if (composerPath) {
try {
const fileContent = fs.readFileSync(composerPath, "utf8");
const composerJson = JSON.parse(fileContent);
if (composerJson.require && composerJson.require.php) {
// Check for a wildcard pattern like "7.*"
const wildcardMatch = composerJson.require.php.match(
/^(?:[^0-9]*)?([0-9]+)\.\*/
);
if (wildcardMatch) {
return parseFloat(`${wildcardMatch[1]}.0`);
}
// Extract version from composer semver format
const versionMatch = composerJson.require.php.match(
/^(?:[^0-9]*)?([0-9]+)\.([0-9]+)/
);
if (versionMatch) {
return parseFloat(`${versionMatch[1]}.${versionMatch[2]}`);
} else {
getComposerError = `Could not decode PHP version (${composerJson.require.php}})`;
return null;
}
}
} catch (e) {
getComposerError = `Error reading composer.json: ${e.message}`;
}
} else {
getComposerError = "Could not find composer.json";
}
return null;
}
export { getComposerPhpVersion };
/**
* Resolve the PHP version to a number based on the provided options.
*
*/
export function resolvePhpVersion(options) {
if (!options) {
return;
}
if (options.phpVersion === "auto") {
options.phpVersion =
getComposerPhpVersion() ?? LATEST_SUPPORTED_PHP_VERSION;
} else if (options.phpVersion === "composer") {
const v = getComposerPhpVersion();
if (v === null) {
throw new Error(
`Could not determine PHP version from composer; ${getComposerError}`
);
}
options.phpVersion = v;
} else {
options.phpVersion = parseFloat(options.phpVersion);
}
}
export default {
phpVersion: {
since: "0.13.0",
category: CATEGORY_PHP,
type: "choice",
default: "auto",
description: "Minimum target PHP version.",
choices: [
...SUPPORTED_PHP_VERSIONS.map((v) => ({ value: v.toFixed(1) })),
{
value: "composer",
description: "Use the PHP version defined in composer.json",
},
{
value: "auto",
description: `Try composer.json, else latest PHP Version (${LATEST_SUPPORTED_PHP_VERSION})`,
},
],
},
trailingCommaPHP: {
since: "0.0.0",
category: CATEGORY_PHP,
type: "boolean",
default: true,
description: "Print trailing commas wherever possible when multi-line.",
},
braceStyle: {
since: "0.10.0",
category: CATEGORY_PHP,
type: "choice",
default: "per-cs",
description:
"Print one space or newline for code blocks (classes and functions).",
choices: [
{ value: "psr-2", description: "(deprecated) Use per-cs" },
{ value: "per-cs", description: "Use the PER Coding Style brace style." },
{ value: "1tbs", description: "Use 1tbs brace style." },
],
},
singleQuote: {
since: "0.0.0",
category: CATEGORY_PHP,
type: "boolean",
default: false,
description: "Use single quotes instead of double quotes.",
},
};