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

PHP 7.4 closures with array_filter breaks sniffs #839

Closed
raalderink opened this issue Jan 16, 2020 · 7 comments
Closed

PHP 7.4 closures with array_filter breaks sniffs #839

raalderink opened this issue Jan 16, 2020 · 7 comments
Labels
Milestone

Comments

@raalderink
Copy link

raalderink commented Jan 16, 2020

There is an issue with the NullableTypeForNullDefaultValueSniff fixer, used on a closure with an array_filter. It is unable to properly transform this closure into an arrow function.

File to use when reproducing:

<?php
array_map(
    static function (array $value): array {
        return array_filter($value);
    },
    []
);

When running this fixer on the above code, it will iterate 3 times over it. The first two times through the T_CLOSURE, where the second time would be the culprit. The second iteration results in the following code, which breaks the third iteration, throwing Undefined index: parenthesis_opener

<?php
array_map(
    static fn (array $value):  => array_filter[$value],
    []
);

This clearly breaks the array_filter function, because somehow it converts it to an array. The following would be correct:

array_map(
    fn(array $value): array => array_filter($value),
    []
);

In addition to that, the StaticClosureSniff will fail with Undefined index: scope_opener on the above code, which would be correct.

@kukulich
Copy link
Contributor

@raalderink Do you really mean NullableTypeForNullDefaultValueSniff ?

@raalderink
Copy link
Author

@kukulich Yes, the fixer throws this on that code:

Stack trace:
#0 /.../vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/TypeHints/NullableTypeForNullDefaultValueSniff.php(40): PHP_CodeSniffer\Runner->handleErrors(8, 'Undefined index...', '/...', 40, Array)
#1 /.../vendor/squizlabs/php_codesniffer/src/Files/File.php(496): SlevomatCodingStandard\Sniffs\TypeHints\NullableTypeForNullDefaultValueSniff->process(Object(PHP_CodeSniffer\Files\LocalFile), 7)
#2 /.../vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php(91): PHP_CodeSniffer\Files\File->process()
#3 / in /.../vendor/squizlabs/php_codesniffer/src/Runner.php on line 606

Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined index: parenthesis_opener in /.../vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/TypeHints/NullableTypeForNullDefaultValueSniff.php on line 40 in /.../vendor/squizlabs/php_codesniffer/src/Runner.php on line 606

PHP_CodeSniffer\Exceptions\RuntimeException: Undefined index: parenthesis_opener in /.../vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/TypeHints/NullableTypeForNullDefaultValueSniff.php on line 40 in /.../vendor/squizlabs/php_codesniffer/src/Runner.php on line 606

Call Stack:
    0.0017     401280   1. {main}() /.../vendor/squizlabs/php_codesniffer/bin/phpcbf:0
    0.0487    1731048   2. PHP_CodeSniffer\Runner->runPHPCBF() /.../vendor/squizlabs/php_codesniffer/bin/phpcbf:18
    0.3532    7546232   3. PHP_CodeSniffer\Runner->run() /.../vendor/squizlabs/php_codesniffer/src/Runner.php:200
    0.3632    8086296   4. PHP_CodeSniffer\Runner->processFile() /.../vendor/squizlabs/php_codesniffer/src/Runner.php:434
    0.3920    9037944   5. PHP_CodeSniffer\Reporter->cacheFileReport() /.../vendor/squizlabs/php_codesniffer/src/Runner.php:656
    0.3921    9056624   6. PHP_CodeSniffer\Reports\Cbf->generateFileReport() /.../vendor/squizlabs/php_codesniffer/src/Reporter.php:285
    0.3921    9056624   7. PHP_CodeSniffer\Fixer->fixFile() /.../vendor/squizlabs/php_codesniffer/src/Reports/Cbf.php:49
    0.3991    9155432   8. PHP_CodeSniffer\Files\LocalFile->process() /.../vendor/squizlabs/php_codesniffer/src/Fixer.php:174
    0.3991    9155432   9. PHP_CodeSniffer\Files\LocalFile->process() /.../vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php:91
    0.4003    9148152  10. SlevomatCodingStandard\Sniffs\TypeHints\NullableTypeForNullDefaultValueSniff->process() /.../vendor/squizlabs/php_codesniffer/src/Files/File.php:496
    0.4006    9151216  11. PHP_CodeSniffer\Runner->handleErrors() /.../vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/TypeHints/NullableTypeForNullDefaultValueSniff.php:40

@leongersen
Copy link

leongersen commented Jan 16, 2020

This issue isn't limited to array_filter, it also occurs with, for example, array_merge. Here is another reproducer:

array_reduce(
    [],
    static function (array $values): array {
        return array_merge($values, []);
    },
    []
);

Edit: And another:

static function (\InvalidArgumentException $exception): string {
    return $exception->getMessage();
}

@kukulich kukulich added the Bug label Jan 16, 2020
@kukulich kukulich added this to the 6.1.0 milestone Jan 16, 2020
@kukulich
Copy link
Contributor

Thank you for your report.

It looks it's a bug in PHPCS itself but I've found a workaround for this sniff: d69ad4a

@leongersen
Copy link

Is there an open issue for phpcs for this issue?

@kukulich
Copy link
Contributor

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants