You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When PHPStan is configured with missingCheckedExceptionInThrows: true, and the Safe library is used along with Safe's PHPStan rules, a function that creates a new Safe\DateTime[Immutable] object with no parameters makes PHPStan think that the function must be documented as throwing an \Exception.
But, the DateTime[Immutable] class never throws an exception when constructed with no parameters. PHPStan understands this with the base \DateTime[Immutable] constructor and doesn't emit a warning in that case, but it doesn't understand the Safe\DateTime[Immutable] constructor, and emits an erroneous warning.
This is less of an issue with PHP 8.3, because in 8.3 DateTime emits a more specific \DateMalformedStringException and we can configure PHPStan to ignore that. But, previous versions of PHP will still be in use for many years on LTS systems, so this would be worth fixing now.
A workaround is to use a special inline PHPDoc every time we instantiate a new Safe\DateTime with no parameters. Not the worst thing, but it's cluttered and tedious.
Environment
PHP version: 8.1.28
PHPStan version: 1.10.67
Safe version: 2.5.0
PHPStan Safe Rule version: 1.2.0
Description of problem
When PHPStan is configured with
missingCheckedExceptionInThrows: true
, and the Safe library is used along with Safe's PHPStan rules, a function that creates a newSafe\DateTime[Immutable]
object with no parameters makes PHPStan think that the function must be documented as throwing an\Exception
.But, the
DateTime[Immutable]
class never throws an exception when constructed with no parameters. PHPStan understands this with the base\DateTime[Immutable]
constructor and doesn't emit a warning in that case, but it doesn't understand theSafe\DateTime[Immutable]
constructor, and emits an erroneous warning.This is less of an issue with PHP 8.3, because in 8.3
DateTime
emits a more specific\DateMalformedStringException
and we can configure PHPStan to ignore that. But, previous versions of PHP will still be in use for many years on LTS systems, so this would be worth fixing now.A workaround is to use a special inline PHPDoc every time we instantiate a new
Safe\DateTime
with no parameters. Not the worst thing, but it's cluttered and tedious.Example
PHPStan configuration
PHP
The text was updated successfully, but these errors were encountered: