Skip to content

Commit d06ca63

Browse files
committed
refactored versions after rebasing
- automatically use latest version - phpVersion is always a number now - moved isMinVersion to simple number comparison
1 parent c73758d commit d06ca63

File tree

5 files changed

+41
-59
lines changed

5 files changed

+41
-59
lines changed

src/needs-parens.mjs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
getPrecedence,
3-
shouldFlatten,
4-
isBitwiseOperator,
5-
isMinVersion,
6-
} from "./util.mjs";
1+
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.mjs";
72

83
function needsParens(path, options) {
94
const { parent } = path;
@@ -135,7 +130,7 @@ function needsParens(path, options) {
135130
case "new": {
136131
const requiresParens =
137132
node.kind === "clone" ||
138-
(node.kind === "new" && !isMinVersion(options.phpVersion, "8.4"));
133+
(node.kind === "new" && options.phpVersion < 8.4);
139134
switch (parent.kind) {
140135
case "propertylookup":
141136
case "nullsafepropertylookup":

src/options.mjs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ import path from "path";
33

44
const CATEGORY_PHP = "PHP";
55

6-
const LATEST_PHP_VERSION = "8.3";
6+
// prettier-ignore
7+
const SUPPORTED_PHP_VERSIONS = [
8+
5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6,
9+
7.0, 7.1, 7.2, 7.3, 7.4,
10+
8.0, 8.1, 8.2, 8.3, 8.4,
11+
];
12+
13+
export const LATEST_SUPPORTED_PHP_VERSION = Math.max(...SUPPORTED_PHP_VERSIONS);
714

815
let getComposerError = "";
916

1017
/**
1118
* Detect the minimum PHP version from the composer.json file
12-
* @return {string|null} The PHP version to use in the composer.json file, null when not found
19+
* @return {number|null} The PHP version to use in the composer.json file, null when not found
1320
*/
1421
function getComposerPhpVersion() {
1522
// Try to find composer.json
@@ -39,12 +46,12 @@ function getComposerPhpVersion() {
3946
const composerJson = JSON.parse(fileContent);
4047

4148
if (composerJson.require && composerJson.require.php) {
42-
// Check for wildcard pattern like "7.*"
49+
// Check for a wildcard pattern like "7.*"
4350
const wildcardMatch = composerJson.require.php.match(
4451
/^(?:[^0-9]*)?([0-9]+)\.\*/
4552
);
4653
if (wildcardMatch) {
47-
return `${wildcardMatch[1]}.0`;
54+
return parseFloat(`${wildcardMatch[1]}.0`);
4855
}
4956

5057
// Extract version from composer semver format
@@ -53,7 +60,7 @@ function getComposerPhpVersion() {
5360
);
5461

5562
if (versionMatch) {
56-
return `${versionMatch[1]}.${versionMatch[2]}`;
63+
return parseFloat(`${versionMatch[1]}.${versionMatch[2]}`);
5764
} else {
5865
getComposerError = `Could not decode PHP version (${composerJson.require.php}})`;
5966
return null;
@@ -72,15 +79,16 @@ function getComposerPhpVersion() {
7279
export { getComposerPhpVersion };
7380

7481
/**
75-
* Resolve the PHP version based on the provided options.
82+
* Resolve the PHP version to a number based on the provided options.
7683
*
7784
*/
7885
export function resolvePhpVersion(options) {
7986
if (!options) {
8087
return;
8188
}
8289
if (options.phpVersion === "auto") {
83-
options.phpVersion = getComposerPhpVersion() ?? LATEST_PHP_VERSION;
90+
options.phpVersion =
91+
getComposerPhpVersion() ?? LATEST_SUPPORTED_PHP_VERSION;
8492
} else if (options.phpVersion === "composer") {
8593
const v = getComposerPhpVersion();
8694
if (v === null) {
@@ -90,6 +98,8 @@ export function resolvePhpVersion(options) {
9098
} else {
9199
options.phpVersion = v;
92100
}
101+
} else {
102+
options.phpVersion = parseFloat(options.phpVersion);
93103
}
94104
}
95105

@@ -101,30 +111,14 @@ export default {
101111
default: "auto",
102112
description: "Minimum target PHP version.",
103113
choices: [
104-
{ value: "5.0" },
105-
{ value: "5.1" },
106-
{ value: "5.2" },
107-
{ value: "5.3" },
108-
{ value: "5.4" },
109-
{ value: "5.5" },
110-
{ value: "5.6" },
111-
{ value: "7.0" },
112-
{ value: "7.1" },
113-
{ value: "7.2" },
114-
{ value: "7.3" },
115-
{ value: "7.4" },
116-
{ value: "8.0" },
117-
{ value: "8.1" },
118-
{ value: "8.2" },
119-
{ value: "8.3" },
120-
{ value: "8.4" },
114+
...SUPPORTED_PHP_VERSIONS.map((v) => ({ value: v.toFixed(1) })),
121115
{
122116
value: "composer",
123117
description: "Use the PHP version defined in composer.json",
124118
},
125119
{
126120
value: "auto",
127-
description: `Try composer.json, latest PHP Version (${LATEST_PHP_VERSION})`,
121+
description: `Try composer.json, else latest PHP Version (${LATEST_SUPPORTED_PHP_VERSION})`,
128122
},
129123
],
130124
},

src/parser.mjs

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

55
function parse(text, opts) {
@@ -13,15 +13,11 @@ function parse(text, opts) {
1313
// Todo https://github.com/glayzzle/php-parser/issues/170
1414
text = text.replace(/\?>\n<\?/g, "?>\n___PSEUDO_INLINE_PLACEHOLDER___<?");
1515

16-
const latestSupportedPhpVersion = Math.max(
17-
...options.phpVersion.choices.map((c) => parseFloat(c.value))
18-
);
19-
2016
// initialize a new parser instance
2117
const parser = new engine({
2218
parser: {
2319
extractDoc: true,
24-
version: `${latestSupportedPhpVersion}`,
20+
version: `${LATEST_SUPPORTED_PHP_VERSION}`,
2521
},
2622
ast: {
2723
withPositions: true,

src/printer.mjs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import {
3737
isReferenceLikeNode,
3838
normalizeMagicMethodName,
3939
isSimpleCallArgument,
40-
isMinVersion,
4140
} from "./util.mjs";
4241

4342
const {
@@ -66,12 +65,19 @@ const {
6665
isPreviousLineEmpty,
6766
} = prettierUtil;
6867

68+
/**
69+
* Determine if we should print a trailing comma based on the config & php version
70+
*
71+
* @param options {object} Prettier Options
72+
* @param requiredVersion {number}
73+
* @returns {boolean}
74+
*/
6975
function shouldPrintComma(options, requiredVersion) {
7076
if (!options.trailingCommaPHP) {
7177
return false;
7278
}
7379

74-
return isMinVersion(options.phpVersion, requiredVersion);
80+
return options.phpVersion >= requiredVersion;
7581
}
7682

7783
function shouldPrintHardlineForOpenBrace(options) {
@@ -612,9 +618,9 @@ function printArgumentsList(path, options, print, argumentsKey = "arguments") {
612618
const lastArg = getLast(args);
613619

614620
const maybeTrailingComma =
615-
(shouldPrintComma(options, "7.3") &&
621+
(shouldPrintComma(options, 7.3) &&
616622
["call", "new", "unset", "isset"].includes(node.kind)) ||
617-
(shouldPrintComma(options, "8.0") &&
623+
(shouldPrintComma(options, 8.0) &&
618624
["function", "closure", "method", "arrowfunc", "attribute"].includes(
619625
node.kind
620626
))
@@ -1186,7 +1192,7 @@ function printAttrs(path, options, print, { inline = false } = {}) {
11861192
allAttrs.push(
11871193
group([
11881194
indent(attrGroup),
1189-
ifBreak(shouldPrintComma(options, "8.0") ? "," : ""),
1195+
ifBreak(shouldPrintComma(options, 8.0) ? "," : ""),
11901196
softline,
11911197
"]",
11921198
inline ? ifBreak(softline, " ") : "",
@@ -1658,11 +1664,7 @@ function printNode(path, options, print) {
16581664
join([",", line], path.map(print, "items")),
16591665
]),
16601666
node.name
1661-
? [
1662-
ifBreak(shouldPrintComma(options, "7.2") ? "," : ""),
1663-
softline,
1664-
"}",
1665-
]
1667+
? [ifBreak(shouldPrintComma(options, 7.2) ? "," : ""), softline, "}"]
16661668
: "",
16671669
]);
16681670
case "useitem":
@@ -2356,9 +2358,8 @@ function printNode(path, options, print) {
23562358
case "list":
23572359
case "array": {
23582360
const useShortForm =
2359-
(node.kind === "array" && isMinVersion(options.phpVersion, "5.4")) ||
2360-
(node.kind === "list" &&
2361-
(node.shortForm || isMinVersion(options.phpVersion, "7.1")));
2361+
(node.kind === "array" && options.phpVersion >= 5.4) ||
2362+
(node.kind === "list" && (node.shortForm || options.phpVersion >= 7.1));
23622363
const open = useShortForm ? "[" : [node.kind, "("];
23632364
const close = useShortForm ? "]" : ")";
23642365

@@ -2408,7 +2409,7 @@ function printNode(path, options, print) {
24082409
indent([softline, printArrayItems(path, options, print)]),
24092410
needsForcedTrailingComma ? "," : "",
24102411
ifBreak(
2411-
!needsForcedTrailingComma && shouldPrintComma(options, "5.0")
2412+
!needsForcedTrailingComma && shouldPrintComma(options, 5.0)
24122413
? [
24132414
lastElem && shouldPrintHardlineBeforeTrailingComma(lastElem)
24142415
? hardline
@@ -2675,7 +2676,7 @@ function printNode(path, options, print) {
26752676
if (parent.kind === "encapsedpart") {
26762677
const parentParent = path.grandparent;
26772678
let closingTagIndentation = 0;
2678-
const flexible = isMinVersion(options.phpVersion, "7.3");
2679+
const flexible = options.phpVersion >= 7.3;
26792680
let linebreak = literalline;
26802681
if (parentParent.type === "heredoc") {
26812682
linebreak = flexible ? hardline : literalline;
@@ -2744,7 +2745,7 @@ function printNode(path, options, print) {
27442745
case "string":
27452746
case "shell":
27462747
case "heredoc": {
2747-
const flexible = isMinVersion(options.phpVersion, "7.3");
2748+
const flexible = options.phpVersion >= 7.3;
27482749
const linebreak = flexible ? hardline : literalline;
27492750
return [
27502751
getEncapsedQuotes(node),
@@ -2769,7 +2770,7 @@ function printNode(path, options, print) {
27692770
case "magic":
27702771
return node.value;
27712772
case "nowdoc": {
2772-
const flexible = isMinVersion(options.phpVersion, "7.3");
2773+
const flexible = options.phpVersion >= 7.3;
27732774
const linebreak = flexible ? hardline : literalline;
27742775
return [
27752776
"<<<'",

src/util.mjs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,6 @@ function memoize(fn) {
709709
};
710710
}
711711

712-
function isMinVersion(actualVersion, requiredVersion) {
713-
return parseFloat(actualVersion) >= parseFloat(requiredVersion);
714-
}
715712

716713
export {
717714
printNumber,
@@ -744,5 +741,4 @@ export {
744741
normalizeMagicMethodName,
745742
isSimpleCallArgument,
746743
memoize,
747-
isMinVersion,
748744
};

0 commit comments

Comments
 (0)