PHPCompatibility / PHPModernizer

External PHPCS standard with auto-fixers to modernize legacy codebases
GNU Lesser General Public License v3.0
10 stars 0 forks source link

Sniff to detect function calls not using the PHP7 opcode cache #17

Open jrfnl opened 6 years ago

jrfnl commented 6 years ago

From the PHPCS gitter - originally posted by @photodude:

Is there a rule to enforce the special compiled PHP functions? I've looked but didn't find anything. background... PHP 7+ has some functions are replaced by opcodes, producing much faster code. these functions need to be referenced in the root namespace at compile time: Either there is no namespace, or they are prefixed by a \ Here is the exact list of functions that have this behavior: https://github.com/php/php-src/blob/f2db305fa4e9bd7d04d567822687ec714aedcdb5/Zend/zend_compile.c#L3872

The reason why I think this would be a good addition to PHPCompatibility is that a sniff for this would help people take advantage of a (lesser known) PHP7 feature which would make their code more compatible with PHP7 (while not making it incompatible with lower PHP versions).

A sniff for this could check if the functions are used within a namespace and not prefixed with a \ nor referenced in a use statement and throw a warning if testVersion would have a minimum PHP version of PHP 7.0.

At the same time, I realize this is more of a "best practice" kind of sniff, which is outside of the scope of PHPCompatibility.

Opinions ?

MarkMaldaba commented 6 years ago

At the same time, I realize this is more of a "best practice" kind of sniff, which is outside of the scope of PHPCompatibility.

You probably know my opinion, as this kind of thing has come up before. 😃

My view is that if it's not a compatibility issue, it doesn't belong in the library.

I think there is probably a good case for a separate PHPOptimisation standard, which could include this kind of sniff and could also advise on other potential performance optimisations, but it seems orthogonal to the aim of PHPCompatibility.

photodude commented 6 years ago

a good case for a separate PHPOptimisation standard, which could include this kind of sniff and could also advise on other potential performance optimisations

Sounds like an interesting project which could be very useful.

This was originally a question @mbabker asked me about PHPCS, it's based on the following issue https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/3048

jrfnl commented 6 years ago

FYI: A sniff for this - including auto-fixer - will be included in the new PHPModernizer standard in the foreseeable future.

jrfnl commented 5 years ago

I'm going to try out the new "transfer issue" functionality and move this issue to the PHPModernizer repo.