plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

Forcing short hand arrays in PHP

Open vincentmcgreevy opened this issue 3 years ago • 5 comments

It appears the PHP array short hand is being forced for version 5.4 and above?

const useShortForm = (node.kind === "array" && isMinVersion(options.phpVersion, "5.4")) || (node.kind === "list" && node.shortForm

Can this be updated to have the option to override this?

vincentmcgreevy avatar Dec 30 '22 15:12 vincentmcgreevy

Hi @vincentmcgreevy! That's correct, there is no option to override this. Prettier is an opinionated code formatter, and we try to keep the number of options to an absolute minimum. Can you explain why the shorthand array syntax is not working out for you?

czosel avatar Dec 30 '22 19:12 czosel

I am actually having the same issue. We have the old syntax in 1000s of places and them all being updated makes PRs very hard to distinguish between real changes and these

Dalton-James avatar Feb 03 '23 13:02 Dalton-James

@Dalton-James I'd recommend to format every file in a single commit when introducing prettier in an existing codebase, such that commits afterwards never just change formatting at places unrelated to the actual change. There even is a way to rewrite history as if the project would have always been formatted with Prettier, but I never tried that myself.

czosel avatar Feb 04 '23 08:02 czosel

Formatting all the code at once is not an option, we have a lot of old PHP scripts and prettier was breaking scripts so we lost any confidence in doing this. From what I remember one of the issues was prettier broke the syntax if you declared a class property with a variable.

vincentmcgreevy avatar Jun 09 '23 10:06 vincentmcgreevy

@vincentmcgreevy I get that. I’ll mention the AST_COMPARE flag though, which ensures that the AST doesn’t change after formatting (except expected changes), which should give you additional safety. See https://github.com/prettier/plugin-php/blob/39c4d523044055c1c1b82ee2e0c0537380d7b8f8/CONTRIBUTING.md?plain=1#L20

czosel avatar Jun 09 '23 11:06 czosel