Skip to content

IF statement with no braces and double catch turned into syntax error by auto-fixer #3345

Closed
@PierreAntoineGuillaume

Description

@PierreAntoineGuillaume

When using cbf to clean up following sample code, it turns it

<?php

function testParse()
{
    if (true)
        try {
        } catch (\LogicException $e) {
        } catch (\Exception $e) {
        }
}

into

<?php

function testParse()
{
    if (true) {
        try {
        } catch (\LogicException $e) {
        }
    } catch (\Exception $e) {
    }
}

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcbf test.php ...
  3. now has a syntax error
$ vendor/bin/phpcbf test.php 

PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
test.php                                              5      0
----------------------------------------------------------------------
A TOTAL OF 5 ERRORS WERE FIXED IN 1 FILE
----------------------------------------------------------------------

Time: 48ms; Memory: 10MB

Expected behavior
Either a fatal error and no code modification, or this code :

<?php

function testParse()
{
    if (true) {
        try {
        } catch (\LogicException $e) {
        } catch (\Exception $e) {
        }
    }
}

Versions (please complete the following information):

  • OS: Ubuntu 20 (standard apt repo)
  • PHP: 7.4
  • PHPCS: 3.6.0
  • Standard: PSR12

Here's a full version : https://github.com/GPierre-Antoine/phpcbf-bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions