Skip to content

Throw less misleading exception when property access not found #19141

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

Closed
wants to merge 1 commit into from
Closed

Conversation

bramtweedegolf
Copy link
Contributor

@bramtweedegolf bramtweedegolf commented Jun 22, 2016

Prevent throwing a NoSuchPropertyException with a somewhat misleading message "Neither the property "X" nor one of the methods "addX()"/"removeX()", "setX()", "x()", "__set()" or "__call()" exist and have public access in class when the access cannot be determined, for instance if the doctrine schema is not up to date.

Q A
Branch? 3.1 for fixes
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
License MIT

@bramtweedegolf
Copy link
Contributor Author

bramtweedegolf commented Jun 22, 2016

Sorry, status should not be bug but enhancement.

@stof
Copy link
Member

stof commented Jun 22, 2016

this should be covered by a test

@fabpot
Copy link
Member

fabpot commented Jun 23, 2016

I think that this qualifies as a bug fix. Can you add a test for that case?

@@ -651,6 +651,8 @@ private function writeProperty($zval, $property, $value)
// fatal error.

$object->$property = $value;
} elseif (self::ACCESS_TYPE_NOT_FOUND === $access[self::ACCESS_TYPE]) {
throw new NoSuchPropertyException(sprintf('Could not determine access type for property "%s".', $property));
} elseif (self::ACCESS_TYPE_MAGIC === $access[self::ACCESS_TYPE]) {
Copy link
Contributor

@phansys phansys Jun 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An elseif expression after an uncaught exception looks weird. IMHO, it should be more semantic to replace it with a new if.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas: Coding Standards > Structure

Do not use else, elseif, break after if and case conditions which return or throw something;

Copy link
Member

@nicolas-grekas nicolas-grekas Aug 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what you have in mind but if you mean just turning this elseif into an if, then the code won't mean the same.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean a better form could be:

    // ...
} elseif (self::ACCESS_TYPE_NOT_FOUND === $access[self::ACCESS_TYPE]) {
    throw new NoSuchPropertyException(sprintf('Could not determine access type for property "%s".', $property));
}

if (self::ACCESS_TYPE_MAGIC === $access[self::ACCESS_TYPE]) {
    $object->{$access[self::ACCESS_NAME]}($value);
} else {
    // ...
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this code doesn't do the same...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would't be changed, I mean only else/elseif blocks directly adjacent after return or throw statments (in this case, if (self::ACCESS_TYPE_MAGIC === $access[self::ACCESS_TYPE]))

Copy link
Member

@nicolas-grekas nicolas-grekas Aug 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry but this is not the same... all conditions have to be checked before comming to that line, which means all previous conditions before have an impact on the conditions below. If you break the chain, you break the code...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditions after an if that evaluates to true aren't checked (else / elseif) and the script flow stops when an uncaught exception is found, so which is the sense on having an elseif after a broken execution? IMO, this must be an if. I'm sharing a gist with the whole method just in order to clarify my thoughts: https://gist.github.com/phansys/42deea38ddcbc1d14b82654d92e8d7e0#file-propertyaccessor-php-L25

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With your code, if !$access[self::ACCESS_HAS_PROPERTY] && property_exists($object, $property) is true, an exception will be thrown anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good, finally I get your point. Sorry for the misunderstanding and thank you for the time you took to explain this.

@nicolas-grekas
Copy link
Member

ping @bramtweedegolf

@nicolas-grekas
Copy link
Member

Status: needs work

@@ -651,6 +651,8 @@ private function writeProperty($zval, $property, $value)
// fatal error.

$object->$property = $value;
} elseif (self::ACCESS_TYPE_NOT_FOUND === $access[self::ACCESS_TYPE]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bramtweedegolf can you please move this elseif one step down, to put all throwing cases at the end?

@fabpot
Copy link
Member

fabpot commented Sep 14, 2016

@bramtweedegolf Can you take the latest comments into account and add some unit tests to finish this PR? Thanks.

@nicolas-grekas nicolas-grekas added this to the 3.1 milestone Dec 6, 2016
@nicolas-grekas
Copy link
Member

Thank you @bramtweedegolf.

nicolas-grekas added a commit that referenced this pull request Dec 8, 2016
…ound (bramtweedegolf)

This PR was submitted for the master branch but it was merged into the 3.1 branch instead (closes #19141).

Discussion
----------

Throw less misleading exception when property access not found

Prevent throwing a NoSuchPropertyException with a somewhat misleading message "Neither the property "X" nor one of the methods "addX()"/"removeX()", "setX()", "x()", "__set()" or "__call()" exist and have public access in class when the access cannot be determined, for instance if the doctrine schema is not up to date.

| Q | A |
| --- | --- |
| Branch? | 3.1 for fixes |
| Bug fix? | no |
| New feature? | no |
| BC breaks? | no |
| Deprecations? | no |
| Tests pass? | yes |
| License | MIT |

Commits
-------

ec28da4 Throw less misleading exception when property access not found
This was referenced Dec 13, 2016
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.

6 participants