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

Make it easier to scope global functions #682

Closed
theofidry opened this issue Sep 26, 2022 · 0 comments · Fixed by #706
Closed

Make it easier to scope global functions #682

theofidry opened this issue Sep 26, 2022 · 0 comments · Fixed by #706

Comments

@theofidry
Copy link
Member

Unfortunately this is not a problem that will go away any time soon as long as big frameworks such as Laravel & Symfony do not try to change, and worse continue even for new code, the practice of:

  • introducing new functions in the global scope
  • not using fully-qualified function names or import statement for functions, i.e. relying on the PHP function autoload fallback mechanism

An idea to tackle this problem would be to expose yet another setting, e.g. user-global-functions = list<string|regex> which would have the following effects:

  • it considers this function as registered and declared within the scope code. This means that it will be namespaced. For example:

Original code:

<?php // vendor/symfony/deprecation-contracts/functions.php

if (!function_exists('trigger_deprecation')) {
   function trigger_deprecation() { ...}
}

(currently) Scoped code:

<?php // vendor/symfony/deprecation-contracts/functions.php

namespace Scoped;

if (!function_exists('trigger_deprecation')) { // <- this declaration is now incorrect, the new setting would help fix that
   function trigger_deprecation() { ...}
}
  • the function name would not be registered as internal and would kick in if the call is not FQ, e.g. using trigger_deprecation() in the code (without the leading \ or use statement), would be resolved to Scoped\trigger_deprecation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant