Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allman styling control structures #51

Open
pimjansen opened this issue Apr 16, 2020 · 1 comment
Open

Allman styling control structures #51

pimjansen opened this issue Apr 16, 2020 · 1 comment

Comments

@pimjansen
Copy link

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:
squizlabs/PHP_CodeSniffer#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
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants