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

PHPCBF can break function/use closure brace placement #598

Closed
meijerm15 opened this issue May 22, 2015 · 1 comment
Closed

PHPCBF can break function/use closure brace placement #598

meijerm15 opened this issue May 22, 2015 · 1 comment

Comments

@meijerm15
Copy link

I've found an incorrect fix of PHPCBF.

It is about a 'function() use() {' which will be "fixed" into 'function() { use()', which will obviously break the code, and make it not work properly anymore.

I've listed here the errors and test case, I've used a custom standard, but I suspect the 'Squiz.Functions.MultiLineFunctionDeclaration.BraceOnNewLine' to cause this failed fix.

Thanks in advance

PHPCS

----------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 4 LINES
----------------------------------------------------------------------
 26 | ERROR | [x] Expected 1 space after USE keyword; found 0
    |       |     (Squiz.Functions.MultiLineFunctionDeclaration.SpaceAfterUse)
 33 | ERROR | [x] Expected 1 space after USE keyword; found 0
    |       |     (Squiz.Functions.MultiLineFunctionDeclaration.SpaceAfterUse)
 34 | ERROR | [x] Opening brace should be on the same line as the
    |       |     declaration
    |       |     (Squiz.Functions.MultiLineFunctionDeclaration.BraceOnNewLine)
 42 | ERROR | [x] Opening brace should be on the same line as the
    |       |     declaration
    |       |     (Squiz.Functions.MultiLineFunctionDeclaration.BraceOnNewLine)
----------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

PHPCBF

Processing Test.php [PHP => 178 tokens in 46 lines]... DONE in 8ms (4 fixable violations)
        => Fixing file: 0/4 violations remaining [made 4 passes]... DONE in 60ms
Patched 1 file
Time: 96ms; Memory: 5.25Mb

BEFORE

<?php

namespace Test\Test;

class Test
{
    public function testA()
    {
                    function ($param) use($result) {
                        return null;
                    }
    }

    public function testB()
    {
                    function ($param) use($result)
                    {
                        return null;
                    }
    }

    public function testC()
    {
                    function ($param) use ($result)
                    {
                        return null;
                    }
    }
}

AFTER

<?php

namespace Test\Test;

class Test
{
    public function testA()
    {
                    function ($param) use ($result) {
                        return null;
                    }
    }

    public function testB()
    {
                    function ($param) {
                        use ($result) // <-- WRONG

                        return null;
                    }
    }

    public function testC()
    {
                    function ($param) {
                        use ($result) // <-- WRONG

                        return null;
                    }
    }
}
@gsherwood gsherwood changed the title PHPCBF Fixing Squiz.Functions.MultiLineFunctionDeclaration.BraceOnNewLine wrong, in a 'function use' PHPCBF can break function/use closure brace placement May 25, 2015
@gsherwood
Copy link
Member

I don't think your sample code is valid, but I got the point :) Thanks for the detailed report.

The problem was in the generic function brace checking sniffs, and is now fixed.

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