magento / magento/magento-coding-standard

Enforce import of global functions or using FQN calls

Open
#462 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Progress: ready for grooming proposal
Dominant language
PHP
Stars
375
Forks
165
PR merge metrics
No merged PRs in 30d

Description

### Rule

### Reason

I used code example from here
https://tideways.com/profiler/blog/compiler-optimized-php-functions
https://veewee.github.io/blog/optimizing-php-performance-by-fq-function-calls/

If we import or use FQN call for the global function php produces more optimized code.
Also visible via profilers:
SPX:
![Selection_578](https://github.com/magento/magento-coding-standard/assets/3192770/0b24704b-a769-4c01-8814-af97a4447c58)

Same effect with blackfire:
1. Without import
![Selection_575](https://github.com/magento/magento-coding-standard/assets/3192770/fac2b53b-08b3-43a9-8945-0d72cf35fda7)

2. with import
![Selection_576](https://github.com/magento/magento-coding-standard/assets/3192770/2f496a08-2b4d-4f66-a935-60ebfb8ce098)

Although the optimization might not be that significant for whole codebase it also helps to specify dependency via use statement.
It also might improve performance of some loops when checks like `is_array`, `ltrim`, `preg_match` etc. are used as PHP would no more execute extra opcode `INIT_NS_FCALL_BY_NAME`

Also by import or FQN core function for `setup:di:compile` code we might reduce magento compilation time by a few seconds.

![Selection_577](https://github.com/magento/magento-coding-standard/assets/3192770/923fd7df-ddc5-458d-a06a-ac081d935b15)

### Implementation
Can be taken from here: https://github.com/slevomat/coding-standard/blob/master/doc/namespaces.md#slevomatcodingstandardnamespacesreferenceusednamesonly-
```






```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository files or tests are named. Start by locating the coding-standard rule configuration and checking the referenced Slevomat Namespaces rule; done means global functions are required to be imported or called by fully qualified name, with the repository's checks passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.