Repository: briannesbitt/Carbon (php)

Issue:
Type error when calling canBeCreatedFromFormat with null date
<!--
    🛑 DON'T REMOVE ME.
    This issue template applies to all
      - bug reports,
      - feature proposals,
      - and documentation requests

    Having all those information will allow us to know exactly
    what you expect and answer you faster and precisely (answer
    that matches your Carbon version, PHP version and usage).
    
    Note: Comments between <!- - and - -> won't appear in the final
    issue (See [Preview] tab).
-->
Hello,

I encountered an issue with the following code:
<!--
    ⚠️ Your code below (not the code from inside Carbon library)
    Be sure it's dependency-free and enough to reproduce the issue
    when we run it on:
    https://play.phpsandbox.io/embed/nesbot/carbon
-->
```php
echo Carbon::canBeCreatedFromFormat(null, '!Y-n-j G:i:s.u');
```

Carbon version: 3.2.4
<!--
    ⚠️ Run the command `composer show nesbot/carbon` to get "versions"
-->

PHP version: 8.3.4

<!--
    Use `echo phpversion();`
    to get PHP version.

    Some issues can depends on your context, settings,
    macros, timezone, language. So to be sure the code
    you give is enough to reproduce your bug, try it
    first in:
    https://play.phpsandbox.io/embed/nesbot/carbon?input=%3C%3Fphp%0A%0Aecho%20Carbon%3A%3Aparse('2018-06-01')-%3EsubDay()-%3Emonth%3B

    You can use the [Options] button to change the version
    then when you get the bug with this editor, you can use
    the [Export] button, copy the link of the opened tab,
    then paste it in the issue. Then we can immediately get
    your issue.
-->


I expected to get:

```
false
```
<!--
    Always give your expectations. Each use has their owns.
    You may want daylight saving time to be taken into account,
    someone else want it to be ignored. You may want timezone
    to be used in comparisons, someone else may not, etc.
-->

But I actually get:

```
Carbon\Carbon::rawCreateFromFormat(): Argument #2 ($time) must be of type string, null given, called in /vendor/nesbot/carbon/src/Carbon/Traits/Comparison.php on line 845
```
<!--
    If you did not succeed to get the same result in
    https://play.phpsandbox.io/embed/nesbot/carbon?input=%3C%3Fphp%0A%0Aecho%20Carbon%3A%3Aparse('2018-06-01')%3B then precise the
    result you get.
-->

Thanks!


Pull request:
Handle null in canBeCreatedFromFormat
Fix #2996

Patch:
diff --git a/src/Carbon/CarbonInterface.php b/src/Carbon/CarbonInterface.php
index e79cd910..6934279a 100644
--- a/src/Carbon/CarbonInterface.php
+++ b/src/Carbon/CarbonInterface.php
@@ -1242,11 +1242,6 @@ interface CarbonInterface extends DateTimeInterface, JsonSerializable
      * Carbon::canBeCreatedFromFormat('11:12:45', 'h:i:s'); // true
      * Carbon::canBeCreatedFromFormat('13:12:45', 'h:i:s'); // false
      * ```
-     *
-     * @param string $date
-     * @param string $format
-     *
-     * @return bool
      */
     public static function canBeCreatedFromFormat(?string $date, string $format): bool;
 
diff --git a/src/Carbon/Traits/Comparison.php b/src/Carbon/Traits/Comparison.php
index 98831a3b..de26ef56 100644
--- a/src/Carbon/Traits/Comparison.php
+++ b/src/Carbon/Traits/Comparison.php
@@ -831,14 +831,13 @@ trait Comparison
      * Carbon::canBeCreatedFromFormat('11:12:45', 'h:i:s'); // true
      * Carbon::canBeCreatedFromFormat('13:12:45', 'h:i:s'); // false
      * ```
-     *
-     * @param string $date
-     * @param string $format
-     *
-     * @return bool
      */
     public static function canBeCreatedFromFormat(?string $date, string $format): bool
     {
+        if ($date === null) {
+            return false;
+        }
+
         try {
             // Try to create a DateTime object. Throws an InvalidArgumentException if the provided time string
             // doesn't match the format in any way.


Test patch:
diff --git a/tests/Carbon/IsTest.php b/tests/Carbon/IsTest.php
index cc57d7b7..85b31d75 100644
--- a/tests/Carbon/IsTest.php
+++ b/tests/Carbon/IsTest.php
@@ -984,6 +984,7 @@ class IsTest extends AbstractTestCase
     {
         $this->assertTrue(Carbon::canBeCreatedFromFormat('1975-05-01', 'Y-m-d'));
         $this->assertTrue(Carbon::canBeCreatedFromFormat('12/30/2019', 'm/d/Y'));
+        $this->assertFalse(Carbon::canBeCreatedFromFormat(null, 'Y-m-d'));
         $this->assertFalse(Carbon::canBeCreatedFromFormat('1975-05-01', 'd m Y'));
         $this->assertFalse(Carbon::canBeCreatedFromFormat('1975-5-1', 'Y-m-d'));
         $this->assertFalse(Carbon::canBeCreatedFromFormat('19-05-01', 'Y-m-d'));