Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given
najibu opened this issue · 5 comments
Hello,
I encountered an issue with the following code:
Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given.
vendor/nesbot/carbon/src/Carbon/Traits/Units.php on line 356
$date = self::rawAddUnit($date, $unit, $value);
Carbon version: 3.4.0
PHP version: 8.2.19
Thanks!
Hello,
As per the template instructions we need your code, not the code inside the library:
⚠️ 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
Also this error typically happen when passing a string
value to an add method, for instance:
$value = '4'; // Coming from an input that can be a string
Carbon::now()->addDays($value); // Not allowed
Instead you need to cast explictly (either to float or to int depending what make the more sense in your case):
Carbon::now()->addDays((float) $value);
It's also recommended to sanitize value with some blocking errors, for instance if it comes from user or API input, you could receive basically anything that is actually not numeric and you would just get 0
when you cast it (float) 'foobar'
=> double(0)
in such case, it's often better to throw exception or show validation errors rather than continuing the process.
Thanks for your reply.
I'm not using anything methods like addDays()
Here is the log stack
local.ERROR: Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given, called in /var/www/service-video-library/vendor/nesbot/carbon/src/Carbon/Traits/Units.php on line 358 {"exception":"[object] (TypeError(code: 0): Carbon\\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given, called in /var/www/service-video-library/vendor/nesbot/carbon/src/Carbon/Traits/Units.php on line 358 at /var/www/service-video-library/vendor/nesbot/carbon/src/Carbon/Traits/Units.php:457) [stacktrace] #0 /var/www/service-video-library/vendor/nesbot/carbon/src/Carbon/Traits/Units.php(358): Carbon\\Carbon::rawAddUnit(Object(Carbon\\Carbon), 'minute', '23244') #1 /var/www/service-video-library/vendor/nesbot/carbon/src/Carbon/Traits/Date.php(2925): Carbon\\Carbon->addUnit('minute', '23244', NULL) #2 /var/www/service-video-library/vendor/nesbot/carbon/src/Carbon/Traits/Date.php(2616): Carbon\\Carbon->callModifierMethod('minute', Array) #3 /var/www/service-video-library/vendor/tymon/jwt-auth/src/Claims/Factory.php(136): Carbon\\Carbon->__call('addMinutes', Array) #4 /var/www/service-video-library/vendor/tymon/jwt-auth/src/Claims/Factory.php(106): Tymon\\JWTAuth\\Claims\\Factory->exp() #5 /var/www/service-video-library/vendor/tymon/jwt-auth/src/Factory.php(143): Tymon\\JWTAuth\\Claims\\Factory->make('exp') #6 /var/www/service-video-library/vendor/tymon/jwt-auth/src/Factory.php(169): Tymon\\JWTAuth\\Factory->buildClaims() #7 /var/www/service-video-library/vendor/tymon/jwt-auth/src/Factory.php(85): Tymon\\JWTAuth\\Factory->buildClaimsCollection() #8 /var/www/service-video-library/vendor/tymon/jwt-auth/src/Manager.php(103): Tymon\\JWTAuth\\Factory->make() #9 /var/www/service-video-library/vendor/tymon/jwt-auth/src/JWT.php(194): Tymon\\JWTAuth\\Manager->decode(Object(Tymon\\JWTAuth\\Token)) #10 /var/www/service-video-library/vendor/tymon/jwt-auth/src/JWTAuth.php(63): Tymon\\JWTAuth\\JWT->getPayload() #11 /var/www/service-video-library/vendor/tymon/jwt-auth/src/Http/Middleware/BaseMiddleware.php(68): Tymon\\JWTAuth\\JWTAuth->authenticate() #12 /var/www/service-video-library/vendor/tymon/jwt-auth/src/Http/Middleware/Authenticate.php(30): Tymon\\JWTAuth\\Http\\Middleware\\BaseMiddleware->authenticate(Object(Illuminate\\Http\\Request)) #13 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Tymon\\JWTAuth\\Http\\Middleware\\Authenticate->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #14 /var/www/service-video-library/vendor/laravel-doctrine/orm/src/Middleware/SubstituteBindings.php(54): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #15 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): LaravelDoctrine\\ORM\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #16 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #17 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Routing/Router.php(805): Illuminate\\Pipeline\\Pipeline->then(Object(Closure)) #18 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Routing/Router.php(784): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request)) #19 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Routing/Router.php(748): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route)) #20 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Routing/Router.php(737): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request)) #21 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(200): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request)) #22 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request)) #23 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #24 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #25 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #26 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #27 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #28 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #29 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(51): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #30 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #31 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #32 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #33 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(110): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #34 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #35 /var/www/service-video-library/app/Http/Middleware/Down.php(28): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #36 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Vice\\VL2\\Http\\Middleware\\Down->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #37 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #38 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(175): Illuminate\\Pipeline\\Pipeline->then(Object(Closure)) #39 /var/www/service-video-library/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(144): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request)) #40 /var/www/service-video-library/public/index.php(51): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request)) #41 {main} "}
So in this stack we can see the first file not being from Carbon library is:
/var/www/service-video-library/vendor/tymon/jwt-auth/src/Claims/Factory.php(136)
And as per data info in the stack it's doing: ->addMinutes('23244')
which is no longer supported in Carbon 3.
That will need to be fixed in tymon/jwt-auth
Checking the code tymon/jwt-auth
is also supposed to use int
for the ttl
that is used their. But it's an annotation (this means that it's expected but not actually enforced).
As a first sight, another third-party or your code is overriding the JWT TTL (using ->setTTL()
) and use a string
value for it instead of an int
.
Thank you for you help, the problem was with the jwt time to live I have updated that and all works good now.
'ttl' => (int) env('JWT_TTL', 240),