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
Comments
The cause of the error was found to be an insufficient number of assigned arrays. <?php declare(strict_types = 1);
class HelloWorld
{
/** @var array{int, int} */
public $test = [];
public function sayHello(): void
{
$this->test = [1];
}
} |
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);
}
}
|
@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
|
@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 |
@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
|
Fixed: phpstan/phpstan-src@3b5fd94 |
For future reference, actually running the test code I submitted will generate an undefined warning. $this->detectedCheat["playerName"][1]++;
|
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. |
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)
Code snippet that reproduces the problem
error
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
The text was updated successfully, but these errors were encountered: