Back to Php Cs Fixer

Rule ``no_unneeded_braces``

doc/rules/control_structure/no_unneeded_braces.rst

3.95.11.6 KB
Original Source

=========================== Rule no_unneeded_braces

Removes unneeded braces that are superfluous and aren't part of a control structure's body.

Warning

This rule is CONFIGURABLE


You can configure this rule using the following option: ``namespaces``.

Configuration
-------------

``namespaces``
~~~~~~~~~~~~~~

Remove unneeded braces from bracketed namespaces.

Allowed types: ``bool``

Default value: ``false``

Examples
--------

Example #1
~~~~~~~~~~

*Default* configuration.

.. code-block:: diff

   --- Original
   +++ New
   -<?php {
   +<?php 
        echo 1;
   -}

   +
    switch ($b) {
   -    case 1: {
   +    case 1: 
            break;
   -    }
   +    
    }

Example #2
~~~~~~~~~~

With configuration: ``['namespaces' => true]``.

.. code-block:: diff

   --- Original
   +++ New
    <?php
   -namespace Foo {
   +namespace Foo;
        function Bar(){}
   -}
   +

Rule sets
---------

The rule is part of the following rule sets:

- `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ with config:

  ``['namespaces' => true]``

- `@Symfony <./../../ruleSets/Symfony.rst>`_ with config:

  ``['namespaces' => true]``

References
----------

- Fixer class: `PhpCsFixer\\Fixer\\ControlStructure\\NoUnneededBracesFixer <./../../../src/Fixer/ControlStructure/NoUnneededBracesFixer.php>`_
- Test class: `PhpCsFixer\\Tests\\Fixer\\ControlStructure\\NoUnneededBracesFixerTest <./../../../tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php>`_

The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.