Skip to content
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

Clarify exception message in Twig_Template::getAttribute #2009

Merged
merged 1 commit into from
Apr 1, 2016

Conversation

chalasr
Copy link
Contributor

@chalasr chalasr commented Mar 30, 2016

When a non existing property/method is called for an object, the exception message is:

Method "property()" for object "[class]" does not exist in [template]"

But in fact the Twig_Template::getAttribute() method check for (at least) 3 more methods (isProperty(), getProperty() and __call__()).

It could be more adapted and easier to be debugged if the message would be:

Neither the property "property" nor one of the methods "property()", "getproperty()"/"isproperty()" or "__call()" exist and have public access in class "[class]" in [template]

This message is mostly inspired from the one used in the PropertyAccess component.

BTW I think that it would be great to replace all the checks listed above by a PropertyAccessor::isReadable([property]).
I can work on a PR if it's not overkill.

@fabpot
Copy link
Contributor

fabpot commented Mar 31, 2016

Sounds like a good idea to me. The C extension must also be updated.

@chalasr chalasr force-pushed the 1.x branch 4 times, most recently from 16abb3a to a84cbd9 Compare March 31, 2016 10:04
@chalasr
Copy link
Contributor Author

chalasr commented Mar 31, 2016

@fabpot Thank's for your quick reply.

I'm trying to update the C extension, but I'm facing difficulties to use ucfirst.

Is there something specific to do in order to use this php function ? I tried to call php_ucfirst instead, unsuccessfully.
Or should I write a TWIG_UCFIRST in pure C ?

@stof
Copy link
Member

stof commented Mar 31, 2016

869 additions, 856 deletions in the C code looks wrong to me (and prevents reviewing btw, as github does not display the diff)

@@ -581,7 +581,7 @@ protected function getAttribute($object, $item, array $arguments = array(), $typ
return;
}

throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), -1, $this->getTemplateName());
throw new Twig_Error_Runtime(sprintf('Neither the property "%1$s" nor one of the methods "%1$s()", "get%2$s()"/"is%2$s()" or "__call()" exist and have public access in class "%3$s"', $item, ucfirst($item), get_class($object)), -1, $this->getTemplateName());
Copy link
Member

Choose a reason for hiding this comment

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

just use 'get'.$item as used when calling the method. PHP method names are case insensitive anyway

Copy link
Member

Choose a reason for hiding this comment

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

this will help you for the C extension btw, as you won't need to implement ucfirst anymore

@chalasr chalasr force-pushed the 1.x branch 3 times, most recently from fa2b5a9 to 9005969 Compare March 31, 2016 11:07
Add missing parenthesis

Adapt twig.c extension

Update the C extension

Use php_ucfirst instead of ucfirst

Try to implement a TWIG_UCFIRST in C extension

Last try ucfirst

Rollback twig.c extension

Remove useless ucfirst + update twig.c extension

Fix mis-escaped char

Repeat argument item for sprintf (first try doesnt work)

Remove unexpected breakline in code
@chalasr
Copy link
Contributor Author

chalasr commented Mar 31, 2016

@stof Use of ucfirst was more a matter of convention, I removed it.
The too large diff for the C extension was coming from my editor that unexpectedly replaced tabs by spaces. I fixed it.
Many thanks for your help.

@fabpot Now, it seems to be working well with and without the C extension.

@stof
Copy link
Member

stof commented Mar 31, 2016

👍

2 similar comments
@SpacePossum
Copy link
Contributor

👍

@xabbuh
Copy link
Contributor

xabbuh commented Mar 31, 2016

👍

@fabpot
Copy link
Contributor

fabpot commented Apr 1, 2016

Thank you @chalasr.

@fabpot fabpot merged commit d455b52 into twigphp:1.x Apr 1, 2016
fabpot added a commit that referenced this pull request Apr 1, 2016
…e (chalasr)

This PR was merged into the 1.x branch.

Discussion
----------

Clarify exception message in Twig_Template::getAttribute

When a non existing property/method is called for an object, the exception message is:

>  Method "property()" for object "[class]" does not exist in [template]"

But in fact the `Twig_Template::getAttribute()` method check for (at least) 3 more methods (`isProperty()`, `getProperty()` and `__call__()`).

It could be more adapted and easier to be debugged if the message would be:

> Neither the property "property" nor one of the methods "property]()", "getProperty()"/"isPropertyt()" or "__call()" exist and have public access in class "[class]" in [template]

This message is mostly inspired from the one used in the PropertyAccess component.

BTW I think that it would be great to replace all the checks listed above by a `PropertyAccessor::isReadable([property])`.
I can work on a PR if it's not overkill.

Commits
-------

d455b52 Clarify exception message in Twig_Template::getAttribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants