bmitch / Codor

Custom PHPCS sniffs to find Code Smells

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Thinks it is returning null when filtering out nulls in Laravel collection

bmitch opened this issue · comments

return $foos->filter(function ($foo) {
    return $foo !== null;
});

incorrectly things its returning a null.

This problem is not specific to Laravel.
Here an example to reproduce it:

<?php
class Foo
{
    public function filter(array $items): array
    {
        return array_filter($items, function ($item) {
            return $item !== null;
        });
    }
}

And the error message:

---------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
---------------------------------------------------------------------------------------------------
 7 | ERROR | Return null value found.
   |       | (Codor.Files.ReturnNull.Codor\Sniffs\Files\ReturnNullSniff)
 7 | ERROR | Return null value found.
   |       | (Codor.Files.ReturnNull.Codor\Sniffs\Files\ReturnNullSniff)
---------------------------------------------------------------------------------------------------

According this message the problem comes from ReturnNullSniff.

Hi,
I take this issue, can you assign to me please @villfa ?
Thanks :)

Hello,

Question please when i run test to try to reproduce problem


 composer test tests/Sniffs/Files/ReturnNullSniffTest.php 
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 23 | ERROR | Method name
    |       | "ReturnNullSniffTest::it_detects_functions_that_return_null"
    |       | is not in camel caps format
    |       | (PSR1.Methods.CamelCapsMethodName.NotCamelCaps)
----------------------------------------------------------------------

How to test and reproduce problem in local please?

Thanks

To run a specific test file, use this command:

vendor/bin/simple-phpunit --debug tests/Sniffs/Files/ReturnNullSniffTest.php

To reproduce the problem you have to write a new test.
You could add a new method by duplicating the existing one and then modifying it as you need.
You can also use the code snippet from my previous comment by creating a new file in this folder: https://github.com/bmitch/Codor/tree/be85e18/tests/Sniffs/Files/Assets/ReturnNullSniff