Coding-standard: FullyQualifiedGlobal{Constants,Functions}Sniff: Configuration options needed

Created on 3 Jan 2018  路  19Comments  路  Source: slevomat/coding-standard

It should be possible to:

  • specify whether use is allowed for referencing functions/constants,
  • specify whether \ is allowed for referencing functions/constants,
  • specify how to apply the fix, either by prepending \ (currently forced now) or by adding use.

Original PR: #235

Most helpful comment

Thank you for testing. I will finish the sniff next week and do new release.

Alphabetical order should be fixed by AlphabeticallySortedUsesSniff

All 19 comments

I think it could be 4 new sniffs.

Moving from #235:
I was really looking forward for this feature. IMHO it could be solved by better naming of the sniff. The problem is that the original sniff was implemented for too narrow purpose and Majkl's update therefore doesn't completely fit in. What do you think?

As I said in #235 I prefer more sniffs:

  • FullyQualifiedConstantsSniff: all constants have to be FQN, the same as FullyQualifiedGlobalConstants but not only for global constants, the option exlude should be supported too.
  • FullyQualifiedFunctionsSniff: all functions have to be FQN, the same as FullyQualifiedGlobalFunctions but not only for global functions, the option exlude should be supported too.

FullyQualifiedGlobalConstants and FullyQualifiedGlobalFunctions could be then marked as deprecated probably and removed in later version.

To force use is little complicated because there's already ReferenceUsedNamesOnlySniff. Not sure how complicated would be to implemented functions and contants in this sniff.

It may by better to split ReferenceUsedNamesOnlySniff to 3 sniffs: classes, functions and constants.

It looks that some support in ReferenceUsedNamesOnlySniff was already there, just needed to fix: https://github.com/slevomat/coding-standard/commit/998b5e96ce36a55d7821d17f39d296a17c05b481

Tried to configure ReferenceUsedNamesOnlySniff somehow, but haven't found a way to have reports that global functions are used inside namespace without use function. That kind of defeats the idea. :/ Maybe it doesn't take into consideration the fact that non-imported functions/constants automatically fall back to global scope? (Though there could be an issue with functional code and same-namespace functions.)

First problem is here: https://github.com/slevomat/coding-standard/blob/998b5e96ce36a55d7821d17f39d296a17c05b481/SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php#L170

Not sure if it's fixable in this sniff. It's too complicated. I'll look at it hopefully in next few days.

@kukulich Looks promising! Trying:

<property name="allowFullyQualifiedGlobalClasses" type="boolean" value="true"/>
<property name="allowFullyQualifiedGlobalFunctions" type="boolean" value="false"/>
<property name="allowFullyQualifiedGlobalConstants" type="boolean" value="false"/>
<property name="allowFullyQualifiedNameForCollidingClasses" type="boolean" value="true"/>

It seems to collide with allowFullyQualifiedNameForCollidingClasses - when enabled, global non-imported functions are not reported, only those with \ in front are. When disabled, they're properly reported.
Also I'd suggest different message, current one is a bit confusing: Function array_keys() should not be referenced via a fully qualified name, but via a use statement.

Thanks!

@Majkl578 Thank you for the test. I think I should add new error code, new message and probably two new options: ignoreGlobalFunctions, ignoreGlobalConstants.

@Majkl578 Could you try new version please?

Tested on my project and on Doctrine ORM with:

<property name="allowFullyQualifiedGlobalClasses" type="boolean" value="true"/>
<property name="allowFullyQualifiedGlobalFunctions" type="boolean" value="false"/>
<property name="allowFullyQualifiedGlobalConstants" type="boolean" value="false"/>
<property name="allowFullyQualifiedNameForCollidingClasses" type="boolean" value="true"/>
<property name="allowFallbackGlobalFunctions" type="boolean" value="false"/>
<property name="allowFallbackGlobalConstants" type="boolean" value="false"/>

LGTM. 馃挴 One issue only: the uses are not added in alphabetical order.

Thank you for testing. I will finish the sniff next week and do new release.

Alphabetical order should be fixed by AlphabeticallySortedUsesSniff

Alphabetical order should be fixed by AlphabeticallySortedUsesSniff

True, it's minor issue, but phpcbf must be executed again, or is there an option to specify sniff order/dependency?

phpcbf runs fixer in 50 loops so only if there鈥檚 a lot of changes in one place it鈥檚 not fixed in the first run.

Released 4.3.0 with improved support for functions and constants in ReferenceUsedNamesOnlySniff.

Let's close this, I think it fulfills my expectations, thanks. 馃槉 @janedbal have you given it a try?

Works great! Thank you very much! Found only one small issue: #239

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings