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

Undefined Index notice on numeric strings #1843

Closed
GameCharmer opened this issue Jan 25, 2019 · 7 comments
Closed

Undefined Index notice on numeric strings #1843

GameCharmer opened this issue Jan 25, 2019 · 7 comments
Labels
Milestone

Comments

@GameCharmer
Copy link

Current version of PHPStan: 0.11.1

I'm receiving the following notice multiple times during operation. It looks like the reflection is casting a numeric string to an integer then failing to test against string array keys.

PHP Notice:  Undefined index: 2819260324446 in /repo/vendor/phpstan/phpstan/src/Reflection/ClassConstantReflection.php on line 43

Code snippet that reproduces the problem

https://phpstan.org/r/53d70e4d-dce3-4d55-bad5-6a5d707f77b1

The analyzer is working as intended, but due to the notices being thrown is breaking CI. The use case is that a 3rd party API is switching from integers to strings for their IDs.

I believe this issue is related. If so, and this is unsolvable, is there any way to disable this type of check all together?

Expected output

Analysis is correct, PHP Notice for an Undefined index is being thrown though.

@GameCharmer
Copy link
Author

GameCharmer commented Jan 25, 2019

As a extremely hacky alternative,

	/**
	 * @return mixed
	 */
	public function getValue()
	{
        $handler = set_error_handler(function(){ throw new \ErrorException(); }, E_NOTICE);
        try {
            $val = $this->reflection->getValue();
        } catch (\ErrorException $e) {
            $val = (string) $this->reflection->getValue();
        }
        set_error_handler($handler);

        return $val;
	}

src/Reflection/ClassconstantReflection.php ~38

I'm on 7.2.10 and thought they had this issue fixed...

@GameCharmer
Copy link
Author

Nevermind, I didn't notice it since it showed success. Ran with --debug and it's far more broken than expected. :(

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jan 25, 2019 via email

@GameCharmer
Copy link
Author

@ondrejmirtes It seems to be a bug in PHP's reflection class. Even though you can set numeric string keys in an array, the reflection is still returning them as integers, which then throws undefined index notices during analysis.

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jan 25, 2019 via email

@GameCharmer GameCharmer reopened this Jan 25, 2019
@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 3, 2019
@GameCharmer
Copy link
Author

Hey, I just noticed that this is still open from over 2 years ago. Running the tests now via the /r link above shows everything working, so it's probably safe to assume this was fixed. Closing out unless you want it back open.

@github-actions
Copy link

This thread 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 Apr 29, 2021
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

2 participants