Wrap objects
Just like we do with functions, it would be great to be able to overload objects that have methods returning false instead of throwing exceptions.
Like SplFileInfo->getRealPath()
or DateTime::createFromFormat
The simplest way I can think of doing this is to simply create extended classes which override the unsafe methods.
Like Safe\DateTime would override createFromFormat, check if it returns false and then throw an exception if it does.
I am willing to work on this, is this okay?
Definitely!
Most of the code of the functions is auto-generated from the PHPDoc. Alas, I think we won't be able to do the same for objects (it is way more complex).
Unless you can find a way to do it automatically that I did not think of, we will have to deal with each object manually.
Pick whatever object you want to wrap (DateTime and SplFileInfo seem to be 2 good candidates) and put the wrapped class in the "lib/" folder.
Ok I will be working on it ASAP/now
See #138 : it solves the issue for DateTime and DateTimeImmutable classes.
Another candidate for this are the DOM* classes. Which may return false sometimes
Not sure if this is the right place to ask this question, how to use Safe\DateTime with doctrine ?
With phpstan-safe-rule, it doesn't let use any DateTime field in doctrine.
I can probably create a custom doctrine type based on DateType but then I would need to replace all "date" annotations with say "safe_date", doesn't sound intuitive