Skip to content
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

How can I make each element of the array appear on a separate line? #922

Open
YepYuYu opened this issue Apr 19, 2023 · 4 comments
Open

How can I make each element of the array appear on a separate line? #922

YepYuYu opened this issue Apr 19, 2023 · 4 comments

Comments

@YepYuYu
Copy link

YepYuYu commented Apr 19, 2023

$array = new Array_();

$array->items = [
    new ArrayItem(new String_('test'), new String_('test0')),
    new ArrayItem(new String_('test'), new String_('test1')),
    new ArrayItem(new String_('test'), new String_('test2')),
    new ArrayItem(new String_('test'), new String_('test3')),
    new ArrayItem(new String_('test'), new String_('test4')),
    new ArrayItem(new String_('test'), new String_('test5')),
    new ArrayItem(new String_('test'), new String_('test6')),
];

$ast = new Expression(new Assign(new Variable('testArray'), $array));
$prettyPrinter = new Standard(['shortArraySyntax' => true]);
echo $prettyPrinter->prettyPrint([$ast]);

The generated code is as follows:

$testArray = ['test0' => 'test', 'test1' => 'test', 'test2' => 'test', 'test3' => 'test', 'test4' => 'test', 'test5' => 'test', 'test6' => 'test'];

The code that I would like to obtain is as follows.

$testArray = [
    'test0' => 'test', 
    'test1' => 'test', 
    'test2' => 'test', 
    'test3' => 'test', 
    'test4' => 'test', 
    'test5' => 'test', 
    'test6' => 'test'
];
@YepYuYu YepYuYu changed the title How can each element of the array be made independent on its own line? How can I make each element of the array appear on a separate line? Apr 19, 2023
@WyriHaximus
Copy link
Contributor

Not sure if this is possible, or even part of the scope for this package. But I've been running phpcs over the generated code for any code style and aesthetics of the code.

@YepYuYu
Copy link
Author

YepYuYu commented May 4, 2023

Not sure if this is possible, or even part of the scope for this package. But I've been running phpcs over the generated code for any code style and aesthetics of the code.

However, phpcs is unable to fulfill many formatting requirements, such as the format requested in this issue, parameter line breaks, and so on.

@nikic
Copy link
Owner

nikic commented May 4, 2023

There is no option for this, but it should be easy to implement your own. Extend the standard pretty printer and override this method:

protected function pExpr_Array(Expr\Array_ $node) {
Rather than pMaybeMultiline call pCommaSeparatedMultiline (possibly behind a check for an attribute, if you don't want to do this for all arrays).

@Rachad-Alabi-ADEKAMBI
Copy link

Rachad-Alabi-ADEKAMBI commented Aug 13, 2023

why not do something like this :
echo <br>.$prettyPrinter->prettyPrint([$ast]); ?

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

No branches or pull requests

4 participants