Skip to content

Generic InlineControlStructureSniff can create parse error when case/if/elseif/else have mixed brace and braceless definitions #879

Closed
@madflow

Description

@madflow

Hi,

bin/phpcbf --version
PHP_CodeSniffer version 2.5.1 (stable) by Squiz (http://www.squiz.net)

I tried to clean the following function:

bin/phpcbf --standard=PSR2 test.php

<?php
// test.php

function sth($data, $type)
{
    switch ($type) {
    case Format::NONE:
        return true;
    case Format::DATE:
    case Format::TIME:
    case Format::DATETIME:
    case Format::DATETIMEU:
        if (empty($data)) { return true; 
        }
        elseif (!is_object($data)) {
            $datetime = date_create($data);
            return ($datetime!==false);
        }
        elseif ($data instanceof \DateTime) 
        return true;
    else {
        throw new FormatException('Unknown datetime object: ' . get_class($data));
    }
            break;
    default:
        throw new FormatException('Unknown format: ' . print_r($type, true));
    }
}

This results in:

Processing test.php [PHP => 199 tokens in 26 lines]... DONE in 8ms (12 fixable violations)
=> Fixing file: 0/12 violations remaining [made 6 passes]... DONE in 51ms
Patched 1 file
Time: 134ms; Memory: 4.75Mb

➜ /tmp php -l test.php
PHP Parse error: syntax error, unexpected 'else' (T_ELSE) in test.php on line 20
Errors parsing test.php

I did the same with

PHP_CodeSniffer version 2.3.4 (stable) by Squiz (http://www.squiz.net)

=> this does not create a parse error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions