Skip to content

[WIP] Use callable typehints #14330

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 3 commits into from
Closed

Conversation

mpajunen
Copy link
Contributor

Q A
Bug fix? no
New feature? no
BC breaks? yes
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

Initially discussed in #14293, now includes those changes as well.

The callable typehint is used where applicable. Two types of changes are included:

  • Replace \Closure typehints
  • Replace is_callable() checks

The PHP bug in the handling of callables of the 'Foo::bar' type (https://bugs.php.net/bug.php?id=68475) causes some issues. To support every possible callable, call_user_func($func, ...) needs to be used instead of using $func(...) directly. Besides the more verbose syntax, call_user_func is also slower. There are a few options on how to deal with this:

  1. Use call_user_func.
  2. Don't support callables of the static string type. Possibly add an extra check and throw an exception when such a callback is passed.
  3. Don't change typehints where this is an issue, but continue to use \Closure.

Even option 2 doesn't sound entirely unworkable to me, static callback strings are very unwieldy -- especially with namespaces. That the error is thrown at call time is less than ideal of course.

In these cases using call_user_func doesn't look like a too bad option either. But in other places noticeable performance improvements could be possible if existing code could be converted to use direct calls. So the decision may have implications beyond just this PR.

In addition to handling the static strings, I still want to make sure that none of the \Closure typehints that were replaced were intentionally strict. The one typehint in OptionsResolver mentioned by @stof in #14293 was already skipped.

@stof
Copy link
Member

stof commented Apr 13, 2015

We should support all callable types (which is already the case in most places in Symfony). Restricting the callables being allowed would be both a BC break and a WTF for users

$this->controller = $controller;
}

private function varToString($var)
Copy link
Member

Choose a reason for hiding this comment

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

this unused method should actually be removed in older branches

Copy link
Member

Choose a reason for hiding this comment

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

ah no sorry, it was used in the exception message

Also removes tests checking the exceptions thrown from
the removed is_callable checks.
@mpajunen mpajunen force-pushed the typehint-callable branch 2 times, most recently from dd38335 to 2425766 Compare April 14, 2015 19:22
@mpajunen
Copy link
Contributor Author

  • Related changes to support all callable types are now included.
  • The Console component changes include renaming functions and attributes. If these changes are too extensive, I can remove or rework them.
  • Changes from [DomCrawler] Callable type hint #14293 are also now included in this PR.

*/
public function setNormalizationClosures(array $closures)
public function setNormalizationCallbacks(array $callbacks)
Copy link
Member

Choose a reason for hiding this comment

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

Renaming the method is a BC break

Copy link
Contributor

Choose a reason for hiding this comment

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

@stof this is for 3.0 and all these changes in this pr are bc breaks.

Copy link
Contributor

Choose a reason for hiding this comment

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

but in this case here (which is just about renaming without changed typehint) it is probably better to deprecate the method in 2.8 first and provide the new method. so there is an upgrade path. and then remove the old one in 3.0

Copy link
Member

Choose a reason for hiding this comment

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

@Tobion the typehint changes are BC breaks only for people extending the classes, because of the E_STRICT (and for most places, they probably don't need to extend it). The method renaming is the only BC break affecting the usage of the class.

Copy link
Member

Choose a reason for hiding this comment

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

To make it perfectly clear, we don't want to break BC without providing an upgrade path. In this case, we should introduce the new method in 2.8, deprecate the old one in 2.8, and remove the deprecated on in 3.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I can open a separate PR with the naming changes and BC layer for 2.8.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All Config component changes now in #14364.

@mpajunen mpajunen force-pushed the typehint-callable branch from 2425766 to 2b47634 Compare April 15, 2015 18:13
@fabpot
Copy link
Member

fabpot commented Oct 5, 2015

This involves a lot of signature changes and so many BC breaks for which we cannot really warn the user beforehand, not sure this is worth it.

@nicolas-grekas
Copy link
Member

👎 from me: this should be changed on a case by case basis, when someone has an actual use case imo. Doing a global change is more a theoretical standpoint than a practical one.

@nicolas-grekas
Copy link
Member

See #16125 for the callable type hint

fabpot added a commit that referenced this pull request Oct 6, 2015
…nicolas-grekas)

This PR was merged into the 3.0-dev branch.

Discussion
----------

Replace is_callable checks with type hints

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |  #14330
| License       | MIT
| Doc PR        | -

Also removes tests checking the exceptions thrown from
the removed is_callable checks.

Commits
-------

7685cdd Add more callable type hints
4e0c6e1 Replace is_callable checks with type hints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants