PHPCSExtra icon indicating copy to clipboard operation
PHPCSExtra copied to clipboard

Allman styling control structures

Open pimjansen opened this issue 4 years ago • 1 comments

Seems that Allman styling for control structures are still missing in PHPCS. Below an example of a valid structure: https://en.wikipedia.org/wiki/Indentation_style#Allman_style

I also noticed some work already has been done over here: https://github.com/squizlabs/PHP_CodeSniffer/pull/1847

<?php

declare(strict_types=1);

namespace My\Namespace;

use Foo\Bar;

final class MyClass implements Foo
{
    // Example from PSR12 / https://www.php-fig.org/psr/psr-12/
    use C
    {
        B::bigTalk insteadof C;
        C::mediumTalk as FooBar;
    }

    public function __construct()
    {

    }

    public function aVeryLongMethodName(
        TypeHint $hint,
        &$arg2,
        array $arg3 = []
    ): void
    {
        // method body
    }

    public function anonymouseClasses()
    {
        $instance = new class {};

        $instance = new class extends \Foo implements \HandleableInterface
        {
            // Class content
        };

        $instance = new class extends \Foo implements
            \ArrayAccess,
            \Countable,
            \Serializable
        {
            // Class content
        };
    }
    public function closure()
    {
        // No idea if below is the way due the open method "(" but the indenting is allman way though
        $app->get('/hello/{name}', function ($name) use ($app)
        {
            return 'Hello ' . $app->escape($name);
        });

        $foo->bar(
            $arg1,
            function ($arg2) use ($var1)
            {
                // body
            },
            $arg3
        );

        $closureWithArgs = function ($arg1, $arg2)
        {
            // body
        };

        $closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2)
        {
            // body
        };

        $closureWithArgsVarsAndReturn = function ($arg1, $arg2) use ($var1, $var2): bool
        {
            // body
        };
    }

    public function structures()
    {
        foreach ($a as $b)
        {
            // do something
        }

        for ($i = 0; $i < 10; $i++)
        {
            // do something
        }

        for (
            $i = 0;
            $i < 10;
            $i++
        )
        {
            // for body
        }

        while (true)
        {

        }

        while (
            $expr1
            && $expr2
        )
        {
            // structure body
        }

        do
        {
            // structure body;
        }
        while ($expr);

        switch ($var)
        {
            case 'foo':
                // do something
                break;
            default:
                // do something
                break;
        }

        if ($foo === true)
        {
            // do
        }
        elseif ($bar === true)
        {
            // elseif
        }
        else
        {
            // else
        }

        try
        {
            // try body
        }
        catch (FirstThrowableType $e)
        {
            // catch body
        }
        catch (OtherThrowableType | AnotherThrowableType $e)
        {
            // catch body
        }
        finally
        {
            // finally body
        }
    }
}

pimjansen avatar Apr 16 '20 13:04 pimjansen

Related:

  • https://github.com/squizlabs/PHP_CodeSniffer/issues/526
  • https://github.com/squizlabs/PHP_CodeSniffer/issues/2025
  • https://github.com/squizlabs/PHP_CodeSniffer/issues/2948

jrfnl avatar May 01 '20 11:05 jrfnl