safe icon indicating copy to clipboard operation
safe copied to clipboard

Wrap objects

Open moufmouf opened this issue 7 years ago • 9 comments

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.

moufmouf avatar Sep 06 '18 14:09 moufmouf

Like SplFileInfo->getRealPath()

TomasVotruba avatar Sep 14 '18 01:09 TomasVotruba

or DateTime::createFromFormat

fezfez avatar Sep 14 '18 11:09 fezfez

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.

Jamesking56 avatar Sep 25 '18 12:09 Jamesking56

I am willing to work on this, is this okay?

t0nic avatar Oct 12 '18 09:10 t0nic

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.

moufmouf avatar Oct 12 '18 14:10 moufmouf

Ok I will be working on it ASAP/now

t0nic avatar Oct 13 '18 18:10 t0nic

See #138 : it solves the issue for DateTime and DateTimeImmutable classes.

moufmouf avatar Sep 23 '19 15:09 moufmouf

Another candidate for this are the DOM* classes. Which may return false sometimes

BackEndTea avatar Dec 19 '19 15:12 BackEndTea

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

faizanakram99 avatar Sep 28 '20 10:09 faizanakram99