global_namespace_import.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ================================
  2. Rule ``global_namespace_import``
  3. ================================
  4. Imports or fully qualifies global classes/functions/constants.
  5. Configuration
  6. -------------
  7. ``import_classes``
  8. ~~~~~~~~~~~~~~~~~~
  9. Whether to import, not import or ignore global classes.
  10. Allowed types: ``null`` and ``bool``
  11. Default value: ``true``
  12. ``import_constants``
  13. ~~~~~~~~~~~~~~~~~~~~
  14. Whether to import, not import or ignore global constants.
  15. Allowed types: ``null`` and ``bool``
  16. Default value: ``null``
  17. ``import_functions``
  18. ~~~~~~~~~~~~~~~~~~~~
  19. Whether to import, not import or ignore global functions.
  20. Allowed types: ``null`` and ``bool``
  21. Default value: ``null``
  22. Examples
  23. --------
  24. Example #1
  25. ~~~~~~~~~~
  26. *Default* configuration.
  27. .. code-block:: diff
  28. --- Original
  29. +++ New
  30. <?php
  31. namespace Foo;
  32. +use DateTimeImmutable;
  33. -$d = new \DateTimeImmutable();
  34. +$d = new DateTimeImmutable();
  35. Example #2
  36. ~~~~~~~~~~
  37. With configuration: ``['import_classes' => true, 'import_constants' => true, 'import_functions' => true]``.
  38. .. code-block:: diff
  39. --- Original
  40. +++ New
  41. <?php
  42. namespace Foo;
  43. +use DateTimeImmutable;
  44. +use function count;
  45. +use const M_PI;
  46. -if (\count($x)) {
  47. - /** @var \DateTimeImmutable $d */
  48. - $d = new \DateTimeImmutable();
  49. - $p = \M_PI;
  50. +if (count($x)) {
  51. + /** @var DateTimeImmutable $d */
  52. + $d = new DateTimeImmutable();
  53. + $p = M_PI;
  54. }
  55. Example #3
  56. ~~~~~~~~~~
  57. With configuration: ``['import_classes' => false, 'import_constants' => false, 'import_functions' => false]``.
  58. .. code-block:: diff
  59. --- Original
  60. +++ New
  61. <?php
  62. namespace Foo;
  63. use DateTimeImmutable;
  64. use function count;
  65. use const M_PI;
  66. -if (count($x)) {
  67. - /** @var DateTimeImmutable $d */
  68. - $d = new DateTimeImmutable();
  69. - $p = M_PI;
  70. +if (\count($x)) {
  71. + /** @var \DateTimeImmutable $d */
  72. + $d = new \DateTimeImmutable();
  73. + $p = \M_PI;
  74. }
  75. Rule sets
  76. ---------
  77. The rule is part of the following rule sets:
  78. - `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ with config:
  79. ``['import_classes' => false, 'import_constants' => false, 'import_functions' => false]``
  80. - `@Symfony <./../../ruleSets/Symfony.rst>`_ with config:
  81. ``['import_classes' => false, 'import_constants' => false, 'import_functions' => false]``
  82. References
  83. ----------
  84. - Fixer class: `PhpCsFixer\\Fixer\\Import\\GlobalNamespaceImportFixer <./../../../src/Fixer/Import/GlobalNamespaceImportFixer.php>`_
  85. - Test class: `PhpCsFixer\\Tests\\Fixer\\Import\\GlobalNamespaceImportFixerTest <./../../../tests/Fixer/Import/GlobalNamespaceImportFixerTest.php>`_
  86. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.