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

"Property does not accept" error when incrementing Array shapes #6333

Closed
DaisukeDaisuke opened this issue Jan 10, 2022 · 8 comments
Closed
Labels

Comments

@DaisukeDaisuke
Copy link

DaisukeDaisuke commented Jan 10, 2022

I am using google translate and deepl translate.
I apologize for the very long text of machine translation.

Bug report

When I increment the Array shapes of a class property that has more than one element, I get the following phpstan output:
Except for multidimensional arrays, it doesn't seem to occur if the initial values of the array shape are correctly assigned to the array.
Also, it does not occur when using array<>.

(from https://phpstan.org/r/1ccdd2ae-6def-45ba-8c01-c91c8f54ce8b)

Property HelloWorld::$test12 (array<int, array{value1: int, value2: int}>) does not accept array{array{value1: int}}.

Code snippet that reproduces the problem

class HelloWorld
{
    /**
     * @var array<string, array{stdClass, int}>
     */
    public array $detectedCheat = [];
    //If this function is actually executed, the php warning is obtained.   
    public function test(): void
    {
        $this->detectedCheat["playerName"][1]++;//error
    }
}

error

Property HelloWorld::$detectedCheat (array<string, array{stdClass, int}>) does not accept array{playerName: array{1: int}}.

https://phpstan.org/r/4f76a1f4-1e60-4d0e-a765-7f4ce90f2bcb

others codes(129 lines)

Property HelloWorld::$test11 (array{int, int}) does not accept default value of type array{}.
is not subject to this issuse.
https://phpstan.org/r/1ccdd2ae-6def-45ba-8c01-c91c8f54ce8b

Expected output

No error should occur.
Or, display undefined errors as needed.

Did PHPStan help you today? Did it make you happy in any way?

Today phpstan reported where I forgot to refactor the program, saving me time to launch the program.

phpstan version

1.4.x-dev@b5e71db
phpstan/phpstan-src@b5e71db

@DaisukeDaisuke
Copy link
Author

DaisukeDaisuke commented Jan 10, 2022

The cause of the error was found to be an insufficient number of assigned arrays.
The report I reported is equivalent to the code below.

<?php declare(strict_types = 1);

class HelloWorld
{
	/** @var array{int, int} */
	public $test = [];
	
	public function sayHello(): void
	{
		$this->test = [1];
	}
}

https://phpstan.org/r/57bd175d-b3a6-413f-899e-31111fbcc496

@DaisukeDaisuke
Copy link
Author

DaisukeDaisuke commented Jan 10, 2022

in my opinion, the following code should not report an error.

class HelloWorld
{
	/** @var array{int, int} */
	public array $test;
	
	/** @var array{int, int} */
	public array $test1 = [0, 0];
	
	public function sayHello(): void
	{
		$this->test = [0, 0];
		++$this->test[0]; //error

		//Dumped type: array{1, 0}
		dumpType($this->test);

		++$this->test1[0]; //error

		//Dumped type: array{int, int}
		dumpType($this->test1);
	}
}
Property HelloWorld::$test (array{int, int}) does not accept array{1}.
Dumped type: array{1, 0}
Property HelloWorld::$test1 (array{int, int}) does not accept array{int}.
Dumped type: array{int, int}

https://phpstan.org/r/6deed657-eee4-434a-8741-d54a909ac039

@phpstan-bot
Copy link
Contributor

@DaisukeDaisuke After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
  64: Property HelloWorld::$test11 (array{int, int}) does not accept default value of type array{}.
  72: Dumped type: array{stdClass, int}
- 73: Property HelloWorld::$detectedCheats (array<string, array{stdClass, int}>) does not accept array{playerName: array{1: int}}.
  75: Dumped type: array{int, int}
- 76: Property HelloWorld::$test1 (array<string, array{int, int}>) does not accept array{aaaaaa: array{1: int}}.
- 77: Property HelloWorld::$test1 (array<string, array{int, int}>) does not accept array{aaaaaa: array{int}}.
  79: Dumped type: array{int, int}
- 80: Property HelloWorld::$test2 (array<string, array{int, int}>) does not accept array{test: array{1: int}}.
- 81: Property HelloWorld::$test2 (array<string, array{int, int}>) does not accept array{test: array{int}}.
  84: Dumped type: array{int, int}
- 85: Property HelloWorld::$test3 (array<int, array{int, int}>) does not accept array{array{int}}.
- 86: Property HelloWorld::$test3 (array<int, array{int, int}>) does not accept array{array{1: int}}.
- 87: Property HelloWorld::$test3 (array<int, array{int, int}>) does not accept array{1: array{int}}.
  89: Dumped type: array{int, int, int}
- 90: Property HelloWorld::$test4 (array<string, array{int, int, int}>) does not accept array{aaaaaa: array{2: int}}.
  97: Dumped type: array<int, int>
 102: Dumped type: array{bool, int, string}
-103: Property HelloWorld::$test10 (array{bool, int, string}) does not accept array{1: int}.
 105: Dumped type: array{int, int}
-106: Property HelloWorld::$test11 (array{int, int}) does not accept array{1: int}.
-107: Property HelloWorld::$test11 (array{int, int}) does not accept array{int}.
 109: Dumped type: array{value1: int, value2: int}
-110: Property HelloWorld::$test12 (array<int, array{value1: int, value2: int}>) does not accept array{array{value1: int}}.
 116: Dumped type: array{int, int}
 124: Dumped type: array{int}
Full report
Line Error
64 Property HelloWorld::$test11 (array{int, int}) does not accept default value of type array{}.
72 Dumped type: array{stdClass, int}
75 Dumped type: array{int, int}
79 Dumped type: array{int, int}
84 Dumped type: array{int, int}
89 Dumped type: array{int, int, int}
97 Dumped type: array<int, int>
102 Dumped type: array{bool, int, string}
105 Dumped type: array{int, int}
109 Dumped type: array{value1: int, value2: int}
116 Dumped type: array{int, int}
124 Dumped type: array{int}

@phpstan-bot
Copy link
Contributor

@DaisukeDaisuke After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-12: Property HelloWorld::$detectedCheat (array<string, array{stdClass, int}>) does not accept array{playerName: array{1: int}}.
+No errors

@phpstan-bot
Copy link
Contributor

@DaisukeDaisuke After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-16: Property HelloWorld::$test (array{int, int}) does not accept array{1}.
 17: Dumped type: array{1, 0}
-19: Property HelloWorld::$test1 (array{int, int}) does not accept array{int}.
 20: Dumped type: array{int, int}
Full report
Line Error
17 Dumped type: array{1, 0}
20 Dumped type: array{int, int}

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@3b5fd94

@DaisukeDaisuke
Copy link
Author

For future reference, actually running the test code I submitted will generate an undefined warning.

$this->detectedCheat["playerName"][1]++;
Warning: Undefined array key "playerName" in /in/2FDrl on line 12
Warning: Undefined array key 1 in /in/2FDrl on line 12

https://3v4l.org/2FDrl

@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 Feb 14, 2022
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