-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyAccess] Improve errors when trying to find a writable property #31194
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
Conversation
fa85b0b
to
8e78f51
Compare
src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php
Outdated
Show resolved
Hide resolved
8e78f51
to
0ccb0b1
Compare
0ccb0b1
to
e84654d
Compare
status: Needs Review |
e84654d
to
9b3f0d3
Compare
} | ||
|
||
if ($method->getNumberOfRequiredParameters() > $parameters || $method->getNumberOfParameters() < $parameters) { | ||
$errors[] = sprintf('The method "%s" in class "%s" requires %d arguments, but should accept only %d', $methodName, $reflClass->name, $method->getNumberOfRequiredParameters(), $parameters); |
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.
IIUC (but I might be completely wrong):
requires %d arguments
-> that's the number of passed values right? What about was passed %d values, but you. must pass exactly %s arguments
?
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.
This is when a method is defined with the incorrect number of parameters. E.G. the method is defined as function addFoo()
, but it needs to be defined as function addFoo($var)
. So this message tells the user that the method is currently defined to require x arguments, but it should be defined to only require y arguments.
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.
@pierredup Can you change the current message to make it more explicit?
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.
This is really nice! Pierre, thanks for improving these error messages.
9b3f0d3
to
f90a9fd
Compare
Thank you @pierredup. |
…writable property (pierredup) This PR was submitted for the master branch but it was merged into the 4.4 branch instead (closes #31194). Discussion ---------- [PropertyAccess] Improve errors when trying to find a writable property | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17907 | License | MIT | Doc PR | N/A When setting a property using an adder/remove, the error message is very generic if the methods don't fit the exact requirements (both the adder and remover need to be defined and accept at least one argument). This can be confusing when you already have the methods `addFoo()` and `removeFoo()` defined (but without any parameters in the signature), but the error message states that the method doesn't exist or don't have public access. So this PR tries to improve the error message if a property isn't writable by doing the following: * If only one of the add/remove methods is implemented, indicate that the other method is needed as well. * If any of the adder/remover, setter or magic methods (`__call` or `__set`) don't have the required number of parameters, make it clear that the methods need to define the correct number of parameter. * The any of the access methods were found, but don't have public access, make it clear that the method needs to be defined as public, ```php class Foo { public function addBar($value) { } public function removeBar() { } } ``` **Before:** ``` Neither the property "bar" nor one of the methods "addBar()/removeBar()", "setBar()", "bar()", "__set()" or "__call()" exist and have public access in class "Foo". ``` **After:** ``` The method "removeBar" requires at least "1" parameters, "0" found. ``` Commits ------- f90a9fd Improve errors when trying to find a writable property
When setting a property using an adder/remove, the error message is very generic if the methods don't fit the exact requirements (both the adder and remover need to be defined and accept at least one argument). This can be confusing when you already have the methods
addFoo()
andremoveFoo()
defined (but without any parameters in the signature), but the error message states that the method doesn't exist or don't have public access.So this PR tries to improve the error message if a property isn't writable by doing the following:
__call
or__set
) don't have the required number of parameters, make it clear that the methods need to define the correct number of parameter.Before:
After: