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

phpstan-ignore-line not ignoring line #6175

Closed
jacekkarczmarczyk opened this issue Dec 9, 2021 · 11 comments
Closed

phpstan-ignore-line not ignoring line #6175

jacekkarczmarczyk opened this issue Dec 9, 2021 · 11 comments
Labels

Comments

@jacekkarczmarczyk
Copy link

Bug report

phpstan-ignore-line does not work in certain circumstances

Code snippet that reproduces the problem

https://phpstan.org/r/51956c1f-c771-478e-b8b7-3ce82321edc6

trait SomeTrait {
	private function sayHello(): ?string // @phpstan-ignore-line
	{
		return $this->value;
	}
}

class HelloWorld2
{
	use SomeTrait;
	private string $value = '';
	public function sayIt(): void
	{
		echo $this->sayHello();
	}
}

Code above raises Method HelloWorld2::sayHello() never returns null so it can be removed from the return type. error

Expected output

no error

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

yup!

@ondrejmirtes
Copy link
Member

Hi, a similar thing was recently fixed in 1.1.1:

But I'm aware that traits still suffer from this problem as the ignoring still works the old way in them. It will took a bit more effort to fix them too.

@ondrejmirtes
Copy link
Member

Right now you can reformat the ignoring a bit and it will work: https://phpstan.org/r/3dcb41d8-d561-4df1-956e-846a60755dbd

Also here's proof it works in classes as it's supposed to: https://phpstan.org/r/5abe62eb-3ccc-4263-b4c8-683620db20e3

@jacekkarczmarczyk
Copy link
Author

Thanks for workaround! I'm aware that some related bugs were fixed, I've also checked it with class when i was trying to create minimal reproduction, it worked fine hence the repro with traits

@mvorisek
Copy link
Contributor

mvorisek commented Jan 23, 2022

@phpstan-bot
Copy link
Contributor

@mvorisek After the latest push in 1.10.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.0 – 8.1 (1 error)
+PHP 8.0 – 8.1 (3 errors)
 ==========
 
+19: Tip: Method A::a() always throws an exception, it should have return type "never".
+24: Tip: Method A::b() always throws an exception, it should have return type "never".
 11: Parameter #2 ...$arrays of function array_merge expects array, null given.
 
-PHP 7.1 – 7.4 (1 error)
+PHP 7.1 – 7.4 (3 errors)
 ==========
 
+19: Tip: Method A::a() always throws an exception, it should have return type "never".
+24: Tip: Method A::b() always throws an exception, it should have return type "never".
 11: Parameter #2 ...$args of function array_merge expects array, null given.
Full report

PHP 8.0 – 8.1 (3 errors)

Line Error
19 Tip: Method A::a() always throws an exception, it should have return type "never".
24 Tip: Method A::b() always throws an exception, it should have return type "never".
11 Parameter #2 ...$arrays of function array_merge expects array, null given.

PHP 7.1 – 7.4 (3 errors)

Line Error
19 Tip: Method A::a() always throws an exception, it should have return type "never".
24 Tip: Method A::b() always throws an exception, it should have return type "never".
11 Parameter #2 ...$args of function array_merge expects array, null given.

@phpstan-bot
Copy link
Contributor

@mvorisek After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.0 – 8.1 (1 error)
+PHP 8.0 – 8.1 (3 errors)
 ==========
 
+19: Tip: Method A::a() always throws an exception, it should have return type "never".
+24: Tip: Method A::b() always throws an exception, it should have return type "never".
 11: Parameter #2 ...$arrays of function array_merge expects array, null given.
 
-PHP 7.1 – 7.4 (1 error)
+PHP 7.1 – 7.4 (4 errors)
 ==========
 
+19: Constructor of class A has a return type.
+19: Tip: Method A::a() always throws an exception, it should have return type "never".
+24: Tip: Method A::b() always throws an exception, it should have return type "never".
 11: Parameter #2 ...$args of function array_merge expects array, null given.
Full report

PHP 8.0 – 8.1 (3 errors)

Line Error
19 Tip: Method A::a() always throws an exception, it should have return type "never".
24 Tip: Method A::b() always throws an exception, it should have return type "never".
11 Parameter #2 ...$arrays of function array_merge expects array, null given.

PHP 7.1 – 7.4 (4 errors)

Line Error
19 Constructor of class A has a return type.
19 Tip: Method A::a() always throws an exception, it should have return type "never".
24 Tip: Method A::b() always throws an exception, it should have return type "never".
11 Parameter #2 ...$args of function array_merge expects array, null given.

@phpstan-bot
Copy link
Contributor

@jacekkarczmarczyk After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-4: Method HelloWorld2::sayHello() never returns null so it can be removed from the return type.
+No errors

@phpstan-bot
Copy link
Contributor

@mvorisek After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.0 – 8.1 (1 error)
+PHP 8.0 – 8.1 (2 errors)
 ==========
 
-11: Parameter #2 ...$arrays of function array_merge expects array, null given.
+19: Tip: Method A::a() always throws an exception, it should have return type "never".
+24: Tip: Method A::b() always throws an exception, it should have return type "never".
 
-PHP 7.1 – 7.4 (1 error)
+PHP 7.1 – 7.4 (3 errors)
 ==========
 
-11: Parameter #2 ...$args of function array_merge expects array, null given.
+19: Constructor of class A has a return type.
+19: Tip: Method A::a() always throws an exception, it should have return type "never".
+24: Tip: Method A::b() always throws an exception, it should have return type "never".
Full report

PHP 8.0 – 8.1 (2 errors)

Line Error
19 Tip: Method A::a() always throws an exception, it should have return type "never".
24 Tip: Method A::b() always throws an exception, it should have return type "never".

PHP 7.1 – 7.4 (3 errors)

Line Error
19 Constructor of class A has a return type.
19 Tip: Method A::a() always throws an exception, it should have return type "never".
24 Tip: Method A::b() always throws an exception, it should have return type "never".

@phpstan-bot
Copy link
Contributor

@mvorisek After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-14: Method Model::foo() should return B but returns A.
 19: Dumped type: B
Full report
Line Error
19 Dumped type: B

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@01294d8

ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue Jul 24, 2023
@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 Aug 25, 2023
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

4 participants