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

New sniff: add comma after last param in multi-line function calls #9

Open
jrfnl opened this issue Jul 16, 2018 · 0 comments
Open

New sniff: add comma after last param in multi-line function calls #9

jrfnl opened this issue Jul 16, 2018 · 0 comments

Comments

@jrfnl
Copy link
Member

jrfnl commented Jul 16, 2018

Sniff basics -
Fixable for PHP: 7.3+
Sniff type: Modernize
Fixer type: Safe

Short description

As of PHP 7.3, a trailing comma will be allowed after the last parameter in a function call and in calls to isset() and unset().
This is mostly useful to keep a clean code history when using multi-line calls with each parameter on a new line.

Related PHPCompatibility sniff(s):

PHP manual references:

Example code:

Detect the following code pattern(s):

unset(
    $somethingIDontNeedAnymore,
    $anotherOneToKill,
    $letsMakeThisEasy
);

echo $twig->render(
    'index.html',
    compact(
        'title',
        'body',
        'comments'
    )
);

$newArray = array_merge(
    $arrayOne,
    $arrayTwo,
    ['foo', 'bar']
);

var_dump(
    $whatIsInThere,
    $probablyABugInThisOne,
    $oneMoreToCheck
);

$text = sprintf(
    $en,
    'comma',
    'Jane'
);

And fix these to:

unset(
    $somethingIDontNeedAnymore,
    $anotherOneToKill,
    $letsMakeThisEasy,
);

echo $twig->render(
    'index.html',
    compact(
        'title',
        'body',
        'comments',
    ),
);

$newArray = array_merge(
    $arrayOne,
    $arrayTwo,
    ['foo', 'bar'],
);

var_dump(
    $whatIsInThere,
    $probablyABugInThisOne,
    $oneMoreToCheck,
);

$text = sprintf(
    $en,
    'comma',
    'Jane',
);

Notes for implementation of the sniff:

  • Only apply this change to multi-line calls where each parameter is on a new line (nr of parameters === the diff in line number of the function call T_STRING and the closing parenthesis).
  • Beware of trailing comments after the last parameter!
  • Trailing comma's are also allowed in calls to closures and methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant