-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
Point to the right problem on ArgumentCountError exception #884
base: main
Are you sure you want to change the base?
Changes from 2 commits
4ee6171
9f11a43
3021fe8
6c2bc2b
4302957
cb03eb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -91,14 +91,23 @@ protected function createData( | |||||||
} | ||||||||
} | ||||||||
|
||||||||
try { | ||||||||
return new $dataClass->name(...$parameters); | ||||||||
} catch (ArgumentCountError $error) { | ||||||||
if ($this->isAnyParameterMissing($dataClass, array_keys($parameters))) { | ||||||||
throw CannotCreateData::constructorMissingParameters( | ||||||||
$dataClass, | ||||||||
$parameters, | ||||||||
$error | ||||||||
); | ||||||||
} | ||||||||
|
||||||||
return new $dataClass->name(...$parameters); | ||||||||
} | ||||||||
|
||||||||
protected function isAnyParameterMissing(DataClass $dataClass, array $parameters): bool | ||||||||
{ | ||||||||
return $dataClass | ||||||||
->constructorMethod | ||||||||
->parameters | ||||||||
->pluck('name') | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It would keep only parameters without default values aka required ones, though there is one obscure area, is if you have a default value before a required param (which normally emits a warning) Which is why filtering on is optional would provide the perfect solution, as it would work with variadic parameters, but it would need to be added to the DataParameter class and factory.. |
||||||||
->diff($parameters) | ||||||||
->isNotEmpty(); | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Tests\Fakes; | ||
|
||
use ArgumentCountError; | ||
use Spatie\LaravelData\Data; | ||
|
||
class DataWithArgumentCountErrorException extends Data | ||
{ | ||
public function __construct( | ||
public string $string, | ||
public string $optional = 'default', | ||
) { | ||
throw new ArgumentCountError('This function expects exactly 2 arguments, 1 given.'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.