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

Incorrect warning Variable $var is undefined. #299

Closed
bnoordsij opened this issue Mar 31, 2023 · 6 comments · Fixed by #298 or #301
Closed

Incorrect warning Variable $var is undefined. #299

bnoordsij opened this issue Mar 31, 2023 · 6 comments · Fixed by #298 or #301

Comments

@bnoordsij
Copy link

v2.11.13 works, but since v2.11.14 the following code gives 2 warnings:

 15 | WARNING | Variable $item is undefined.
 21 | WARNING | Variable $value is undefined.
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Admin;

use App\Entities\Vacancy;

class VacancyController
{
    public function index(): array
    {
        $first = Vacancy::query()->where(static fn ($query) => $query)->first(); // OK

        $val = static fn ($item): string => $item; // fails

        return [
            'first' => $first,
            'val' => $val,
            'fn1' => static fn ($val): string => $val, // OK (because $val exists)
            'fn2' => static fn (Vacancy $value): Vacancy => $value, // fails
        ];
    }
}

only the first arrow function reaches the last return in getArrowFunctionOpenClose

@tomrajnoha
Copy link

If I may answer - see #297. If you're using this via composer, it'll help if you upgrade to 2.x-dev. Otherwise just checkout the current code from 2.x branch and it should work.

@sirbrillig
Copy link
Owner

This will hopefully also be fixed by #298

@sirbrillig
Copy link
Owner

sirbrillig commented Mar 31, 2023

This should be fixed now (it was due to the return types) but if you discover any more arrow functions behaving weird, please let me know!

@Levivb
Copy link
Contributor

Levivb commented Mar 31, 2023

$ composer info | grep phpcs-variable-analysis
sirbrillig/phpcs-variable-analysis             v2.11.15           A PHPCS sniff to detect problems with variables.
$ composer style
> vendor/bin/phpcs --standard=phpcs.xml
....................................W.................W..... 60 / 62 (97%)
..                                                           62 / 62 (100%)
FILE: File1.php
--------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
--------------------------------------------------------------
 33 | WARNING | Variable $allowedReferrer is undefined.
--------------------------------------------------------------

FILE: File2.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
 99 | WARNING | Variable $permissionName is undefined.
----------------------------------------------------------------------

Time: 2.26 secs; Memory: 28.98MB

Script vendor/bin/phpcs --standard=phpcs.xml handling the style event returned with error code 1
$ cat -n File1.php | grep -C 5 33
    28
    29	        $this->allowedReferrers = array_map(
    30	            static fn (string $allowedReferrer) => str_replace(
    31	                ['\*\*', '\*'],
    32	                ['[a-z\d.-]{0,63}', '[a-z\d-]{0,63}'],
    33	                preg_quote($allowedReferrer, '~'),
    34	            ),
    35	            $this->allowedReferrers,
    36	        );
$ cat -n File2.php | grep -C 2 99
    97	            ->filter(
    98	                static fn (string $permissionName) => Str::startsWith($permissionName, self::CONFIG_START)
    99	                    && $permissionName !== CustomPermission::ALL_CONFIG
   100	            )

I guess not 😅

@sirbrillig sirbrillig reopened this Mar 31, 2023
@sirbrillig
Copy link
Owner

Ugh, sorry, yeah, that's the same as #300. Arrow function scope is hard.

@Levivb
Copy link
Contributor

Levivb commented Mar 31, 2023

No problem at all :) Such quick fixes, thank you so much for your time and effort!
Tag v2.11.16 shows no more false positives :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants