Comments (8)
I just tested it with v3.x-dev an there it works (again)! But at dev-master it's broken.
from carbon.
Hello,
I will further check if it's expected or if it's the PHPDoc that is wrong.
But in any case I strongly recommend not to do that.
Doing Carbon::parse('2018-06-01')->isSameDay(null)
. It's kinda like doing "orange" + 8
=> trying to do an operation with different things that has no particular reason to mean anything together. There is no reason why null
should be comparable to any kind of date.
You should pass to isSameDay()
only a DateTimeInterface
or a valid string
representing a date-time.
If you mean to say "is same day as now", just use:
Carbon::parse('2018-06-01')->isToday()
Instead, it will make your code less ambigous.
from carbon.
Hello, I confirm this is expected since PHPDoc types were turned into concrete type: c650829
All the isSame*
method will now only accept non-option DateTimeInterface|string $date
parameter.
I will align the PHPDoc.
from carbon.
Documentation fixed in e7c9d37
from carbon.
Hello, I confirm this is expected since PHPDoc types were turned into concrete type: c650829
All the
isSame*
method will now only accept non-optionDateTimeInterface|string $date
parameter.I will align the PHPDoc.
Don't want to offend you, but that's a breaking change since Carbon 2.x version and has nothing to do with the strict type change because in the commit you've shared, In the referenced commit the change was made to have al fallback for isNext (for passing null), but the same fallback wasn't added to isSame
c650829#diff-ede373ba22ef24bd43ba5f8bbc290b90d23d78caf63b8c75cb0971534df3da4bR2707
So changing the docs will indeed align the docs with the behaviour, but that will make a breaking change and in my opion nothing to do with "strict types"
from carbon.
Coming from weaker types (Some Carbon method were crafted with PHP 5 support), we have false
and null
getting accepted just by accident. I wouldn't even say "working" because Carbon::parse('2018-06-01')->isSameDay(null) === true
just doesn't make any sense to be fair.
3.x-dev should be now aligned on dev-master, it was a mistake that it was not updated yet.
This change should apply as early as 3.0.0 (stable) if I'm not mistaken so it's indeed a breaking change, but that's the purpose of bumping major version number, it's to actually do breaking change to fix nonsenses like this one.
from carbon.
To my defense it was used like this:
$formatPattern = $this->start_initial->isSameDay($this->end_initial) ? 'H:i' : 'd/m H:i';
Where my workaround/fix now is:
$formatPattern = $this->start_initial->isSameDay($this->end_initial ?? now()) ? 'H:i' : 'd/m H:i'
because the end_initial
is a Carbon but can also be null. So because it was supported in 2.x I assumed i could just use it off course. The workaround works too, but feels a bit more "ugly" 🤷
from carbon.
No, it's not ugly at all! That's way cleaner because in the first code you have a completely non obvious implicit knowledge that $this->end_initial === null
means "ending now". Other business cases could give to null
a completely different meaning (such as "never ends", "end not yet decided or calculated", "end not known")
For the record $this->end_initial ?? 'now'
would also work as the functions accepts string representing date-time.
With your new code, new developers and your future you no longer have to guess and remember this implicit rule as it's made explicit.
You can find a lot of articles telling you why returning null or giving a specific meaning to null values is typically considered like a bad practices for many other reasons. And comparing null
to another value is one of those code smells which does not just apply to dates but also to any other objects or scalar values.
from carbon.
Related Issues (20)
- Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given HOT 5
- Failed to parse time dd/mm/yyyy using parseFromLocale with Portuguese HOT 1
- Check month issue HOT 1
- PHP Version < 7.4 broken HOT 2
- Psalm reports MissingDependency on CarbonPeriod HOT 1
- Syntax Supported Only After PHP 7.4 Causes Errors in Versions Prior to 7.4 HOT 1
- Undocumented breaking change: isSameMonth() HOT 3
- CarbonPeriod loses timezone when accessing days
- Using "Depends on the following methods" @method in traits causes PHPStan to lose knowledge of type resulting in errors
- 2.7.4 is not support php7.3 HOT 1
- PHPSandbox is not working properly HOT 2
- `age` function does not calculate the age correctly for Carbon >= 3 HOT 1
- DateInterval CarbonInterval Carbon - Laravel 11 HOT 3
- Remove copy() & Make all arithmetic immutable. HOT 1
- Cant create negative interval between 00:00 and -01:00 HOT 2
- Czech (CS) uses genitive case when using iso format `MMMM YYYY`
- Misleading error message in `createFromFormat` HOT 8
- Type Error with setUnit() function in Date.php file HOT 5
- firstOfMonth gives the next month HOT 1
- "Inverted" CarbonPeriod with custom CarbonInterval HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from carbon.