-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
null
-friendly methods
#73
Comments
In the same vein, we have this final class DurationHelper
{
/**
* @return ($a is null ? ($b is null ? null : Duration) : Duration)
*/
public static function plus(?Duration $a, ?Duration $b): ?Duration
{
if (null === $a && null === $b) {
return null;
}
if (null === $a) {
return $b;
}
if (null === $b) {
return $a;
}
return $a->plus($b);
}
/**
* @return ($a is null ? ($b is null ? null : Duration) : Duration)
*/
public static function minus(?Duration $a, ?Duration $b): ?Duration
{
return self::plus($a, $b?->negated());
}
} It would be much nicer if that would be integrated into |
Still in the same vein, we use So I'm wondering if we could we add a Or at least a Related : this could have been fixed by https://wiki.php.net/rfc/nullable-casting using |
Hi, while I understand the use case, I don't think this is something that belongs to this library. A thin helper class is probably fine for this! Regarding |
I dislike having static helper classes everywhere for such simple things that could easily be integrated in libraries and improve its developer experience. Moreover, it means adding one more way of doing things, which means there'll be consistency issues in a big enough team/project. Why do you decided not to consider such simple changes ? Why don't they belong in the library in your opinion ? |
I like the fact that the library is warning me at runtime that the value I'm passing is not parseable. What I suggest is using the same pattern as Enum creation and implementing a I think it would be the sensible thing to do. |
That could be an approach indeed. I must admit I tend to forget that not everybody use and rely on a static analyser. I find them to be such powerful tools to have on top of PHP, it wouldn't cross my mind doing without them nowadays. |
I think I'm not ready to accept null values everywhere, @gnutix.
No doubt about this, everybody should use one. But not everybody does, plus the
I may revisit my opinion about this one. I recently had to use
@tigitz Do you have an opinion on this? Basically we would end up with something like: public function toString(): string
{
// ...
}
public function jsonSerialize(): string
{
return $this->toString();
}
public function __toString(): string
{
return $this->toString();
} That's quite a lot of redundancy, but each one does have its use cases. |
The number one places that would benefit from this are the static constructor methods (especially the About the 3 toString methods, I'd still rather have an |
@BenMorel To me it's fine to have all of them, including the suggested The added maintenance complexity is far outweighed by the additional expressiveness in the api IMO, allowing users to properly define what they really want from the Popular Days.js library also has those methods, sometimes returning the same output: |
@BenMorel I am working on it, but I really don't see the value of adding |
I pushed 2 commits on #85 (as I already fixed a bug in
Please move the conversation there. 🙏 |
Closing as we won't do anything about accepting |
In our application, we often times have to create Brick objects (
LocalDate
,Duration
,TimeZone
, etc) with code like this :It would be nice if methods like parse-rs would allow
null
in input, and directly return it if it is null indeed. It would go along with a PHPDoc stating@return ($input is null ? null : self)
, so that static analysers can tell which result it will be. And that way, no more bloated ternaries at call-site.WDYT ? I'd be willing to work on a pull request in the upcoming two weeks, if that's something you would accept in the library. Let me know!
The text was updated successfully, but these errors were encountered: