Fork me on GitHub
a Sensio Labs Product

I ate three cookies πŸͺ (v3.40.2) edition

Rule native_constant_invocationΒΆ

Add leading \ before constant invocation of internal constant to speed up resolving. Constant name match is case-sensitive, except for null, false and true.

WarningΒΆ

Using this rule is riskyΒΆ

Risky when any of the constants are namespaced or overridden.

ConfigurationΒΆ

excludeΒΆ

List of constants to ignore.

Allowed types: array

Default value: ['null', 'false', 'true']

fix_built_inΒΆ

Whether to fix constants returned by get_defined_constants. User constants are not accounted in this list and must be specified in the include one.

Allowed types: bool

Default value: true

includeΒΆ

List of additional constants to fix.

Allowed types: array

Default value: []

scopeΒΆ

Only fix constant invocations that are made within a namespace or fix all.

Allowed values: 'all' and 'namespaced'

Default value: 'all'

strictΒΆ

Whether leading \ of constant invocation not meant to have it should be removed.

Allowed types: bool

Default value: true

ExamplesΒΆ

Example #1ΒΆ

Default configuration.

--- Original
+++ New
-<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
+<?php var_dump(\PHP_VERSION, \M_PI, MY_CUSTOM_PI);

Example #2ΒΆ

With configuration: ['scope' => 'namespaced'].

--- Original
+++ New
 <?php
 namespace space1 {
-    echo PHP_VERSION;
+    echo \PHP_VERSION;
 }
 namespace {
     echo M_PI;
 }

Example #3ΒΆ

With configuration: ['include' => ['MY_CUSTOM_PI']].

--- Original
+++ New
-<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
+<?php var_dump(\PHP_VERSION, \M_PI, \MY_CUSTOM_PI);

Example #4ΒΆ

With configuration: ['fix_built_in' => false, 'include' => ['MY_CUSTOM_PI']].

--- Original
+++ New
-<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
+<?php var_dump(PHP_VERSION, M_PI, \MY_CUSTOM_PI);

Example #5ΒΆ

With configuration: ['exclude' => ['M_PI']].

--- Original
+++ New
-<?php var_dump(PHP_VERSION, M_PI, MY_CUSTOM_PI);
+<?php var_dump(\PHP_VERSION, M_PI, MY_CUSTOM_PI);

Rule setsΒΆ

The rule is part of the following rule sets:

  • @PhpCsFixer:risky with config:

    ['fix_built_in' => false, 'include' => ['DIRECTORY_SEPARATOR', 'PHP_INT_SIZE', 'PHP_SAPI', 'PHP_VERSION_ID'], 'scope' => 'namespaced', 'strict' => true]

  • @Symfony:risky with config:

    ['strict' => false]

Source classΒΆ

PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer