Skip to content

[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

Merged
merged 1 commit into from
Jul 15, 2019

Conversation

pierredup
Copy link
Contributor

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,
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.

@pierredup pierredup force-pushed the property-access-errors branch from 8e78f51 to 0ccb0b1 Compare April 22, 2019 12:42
@nicolas-grekas nicolas-grekas added this to the next milestone Apr 23, 2019
@pierredup
Copy link
Contributor Author

status: Needs Review

}

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);
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Member

@javiereguiluz javiereguiluz left a 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.

@fabpot fabpot changed the base branch from master to 4.4 July 15, 2019 06:46
@fabpot fabpot force-pushed the property-access-errors branch from 9b3f0d3 to f90a9fd Compare July 15, 2019 06:46
@fabpot
Copy link
Member

fabpot commented Jul 15, 2019

Thank you @pierredup.

@fabpot fabpot merged commit f90a9fd into symfony:4.4 Jul 15, 2019
fabpot added a commit that referenced this pull request Jul 15, 2019
…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
@pierredup pierredup deleted the property-access-errors branch July 15, 2019 07:25
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
This was referenced Nov 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants