Skip to content

Conversation

@canvural
Copy link
Contributor

$exprType = $scope->getType($expr->expr);
$toStringMethod = $scope->getMethodReflection($exprType, '__toString');
if ($toStringMethod !== null) {
if ($toStringMethod->getThrowType() !== null) {
Copy link
Contributor

@staabm staabm Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rfc is PHP 7.4+, meaning this logic should be gated via PHPVersion class.

note though, that NodeScopeResolver is undergoing a major refactoring atm, so this might conflict easily

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't conflict, I'm not doing any major changes in NodeScopeResolver that I'm not pushing.

But it's possible that the same change also needs to happen in PHPStan\Analyser\Generator namespace. In this case it doesn't because there's no StringCastHandler ported yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the minimum version PHPStan supports was 7.4 Is it 7.1?

Copy link
Contributor

@staabm staabm Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are 2 things.

the php version PHPStan can analyze against, configured in the .neon config:

schema(int(), min(70100), max(80599)),

and the PHP runtime version you use to run PHPStan itself:
https://github.com/phpstan/phpstan/blob/5afb019da9f70ae5a2b4247a1830b5103423a325/composer.json#L7

@@ -0,0 +1,20 @@
<?php

namespace Bug13806;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this lack of test,

It should add a test for

function doFoo(Stringable $myVariable): void

to check the implicit throw tag

and a test for

class MyStringVoid {
	/** @throws void */
	public function __toString() {
		throw new \InvalidArgumentException();
	}
}

to check in this case the catch is reported as dead catch

@canvural canvural marked this pull request as ready for review November 14, 2025 16:39
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@VincentLanglet
Copy link
Contributor

Does it also solve phpstan/phpstan#5952 ?
You could add a test for this issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHPStan assumes string conversions cannot throw exceptions

5 participants