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

Fully qualify all global function calls. #718

Merged
merged 2 commits into from
Jan 18, 2024

Conversation

mad-briller
Copy link
Contributor

@mad-briller mad-briller commented Jan 18, 2024

Modern php has several optimizations it can apply to global functions at compile-time, but only when they are fully-qualified.

For example it can translate several functions into specific opcodes which are faster than their function equivalent.

It can also do compile-time evaluation of constant operands for several functions.

Additionally, fully-qualifying global functions avoids a local namespace lookup and as such makes calls faster in general. The improvement will be pretty minor but every little helps, especially in situations where these functions may be called thousands of times in a single analysis.

@mglaman
Copy link
Owner

mglaman commented Jan 18, 2024

Did you manually do this? I wonder if we could use Rector to automate this and if we should add a rule to make sure functions are always imported.

I know they are in PHPStan, it'd be worth checking how that project enforces this

@mad-briller
Copy link
Contributor Author

Yeah i did do it manually

not sure about the rector aspect with regards to the automation, as that would require either pre-commit hooks or for github actions to add commits onto opened prs to apply changes.

It would be easy to add a phpcs sniff to enforce they're always fully-qualified though, phpstan itself uses https://github.com/slevomat/coding-standard and the sniff is SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName

Adopting the full slevomant/coding-standard ruleset would likely be quite a large piece of work as the slevomat standard is fairly opinionated, but i could require-dev it and add the single sniff if you'd like.

@mglaman
Copy link
Owner

mglaman commented Jan 18, 2024

Drupal core also uses parts of slevomat/coding-standard (https://git.drupalcode.org/project/coder/-/blob/8.3.x/composer.json?ref_type=heads#L21)

It's used by Drupal core https://git.drupalcode.org/project/drupal/-/blob/11.x/core/phpcs.xml.dist?ref_type=heads#L255

I think it'd be great to add it as a dev dependency and include that one rule to start

@mad-briller
Copy link
Contributor Author

mad-briller commented Jan 18, 2024

Turns out slevomat/coding-standards was already installed to the repository 😅, but looking into it the rule does more than just enforce the use of use function:

https://github.com/slevomat/coding-standard/blob/master/doc/namespaces.md#slevomatcodingstandardnamespacesreferenceusednamesonly-

it also prevents the use of inline namespaces and a few others, which increases the size of the changeset. I've resolved all the violations it came up with in a separate commit to make it easier for you to review.

Copy link
Owner

@mglaman mglaman left a comment

Choose a reason for hiding this comment

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

Thanks for this, and the extra fixes. There wasn't really a pattern for using FQCN or not. This is a nice addition to standardize and clean things up.

@mglaman mglaman merged commit c393f60 into mglaman:main Jan 18, 2024
18 checks passed
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.

2 participants