Php-cs-fixer: PHP 7.4 property type hints cause error

Created on 6 Feb 2020  路  10Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

Error message when type hinting class properties:

  [PhpCsFixer\Linter\LintingException]
        PHP Parse error: syntax error, unexpected 'Entity' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) on line 7.

The PHP version you are using ($ php -v):

=> PHP 7.4.2

PHP CS Fixer version you are using ($ php-cs-fixer -V):

=> PHP CS Fixer 2.15.3 Europe Round by Fabien Potencier and Dariusz Ruminski (705490b)

The command you use to run PHP CS Fixer:

=> php-cs-fixer fix -vvv

The configuration file you are using, if any:

<?php

if (!file_exists(__DIR__.'/src')) {
    exit(0);
}

$finder = PhpCsFixer\Finder::create()
    ->in([
        __DIR__.'/src',
        ]
        )
;

return PhpCsFixer\Config::create()
    ->setRules(array(
        'simplified_null_return' => true,
        '@Symfony' => true,
        '@DoctrineAnnotation' => true,
        '@Symfony:risky' => true,
        'array_syntax' => ['syntax' => 'short'],
        'binary_operator_spaces' => ['align_double_arrow' => true, 'align_equals' => true],
        'protected_to_private' => false,
        'semicolon_after_instruction' => false,
        'blank_line_after_opening_tag'=>false,
        'header_comment' => [
            'separate'=>'bottom'
        ]
    ))
    ->setRiskyAllowed(true)
    ->setFinder($finder)
;

If applicable, please provide minimum samples of PHP code (as plain text, not screenshots):

  • before running PHP CS Fixer (no changes):
<?php

use My\Test\Entity;

class myTestClass extends Command
{
    private Entity $test;

    public function __construct(Entity $name)
    {
        $this->test = $name;

    }
}
kinbug

Most helpful comment

I think what is going on is on my home mac here /usr/local/bin/php-cs-fixer was running and not /Users/phil/.composer/vendor/bin/php-cs-fixer

Maybe I had an old install - anyways - everything seems sane again. and 2.17.0-DEV does seem to work perfectly with PHP 7.4.2

Thanks

All 10 comments

Hi,

I'm testing this on PHP 7.4.2 , however 2.15.3 doesn't run on PHP7.4;

PHP needs to be a minimum version of PHP 5.6.0 and maximum version of PHP 7.3.*.

can you double check the setup as I'm not sure how to bug hunt this any further :(

Sorry to waste your time.

I literally don't know how I caused this, but today trying to replicate it, I get sensible results.

On my macbook I have a version that has an unknown hash (in composer.json it has dev-master) e850937711919f694843717dfaea41f7f5d8442a and that reports as PHP CS Fixer 2.17.0-DEV and is working with PHP 7.4.2 - but no idea why my macs, which are all set up the same, are now giving me different issues haha

no worries man : )
thanks for checking and good to hear it is fixed, maybe it was a bug on the 2.17.0-DEV that we've fixed along the way, anyway, let us know if you find anything else!

I think what is going on is on my home mac here /usr/local/bin/php-cs-fixer was running and not /Users/phil/.composer/vendor/bin/php-cs-fixer

Maybe I had an old install - anyways - everything seems sane again. and 2.17.0-DEV does seem to work perfectly with PHP 7.4.2

Thanks

@SpacePossum is there any expectation to release 2.17.0? I had the same issue with PHP 7.4, would be amazing if this fix were able as soon as possible :)

@guilhermehubner 2.17 is a feature release that can be bit out.
However we release bug fixing releases more often. Therefore I would like to fix any bugs on the 2.15 and 2.16 branches, can you give an example of the bug? that would help me a lot :)

@SpacePossum I have the same problem reported in this issue. I don't know if using the DEV version is a good ideia. I really would appreciate a lot if there is an official release fixing this issue.

@guilhermehubner can you share an example of code that triggers this bug and provide the version of the tool and PHP you are using? I cannot reproduce the issue and as such cannot debug it

sadly noone gave you some code

running php-cs-fixer 2.16.3 with
php ./vendor/friendsofphp/php-cs-fixer/php-cs-fixer --config=.php_cs.dist fix ./brokenClass.php -vvv

<?php
class brokenClass
{
    private ?bool $breaks;

    /**
     * @return bool|null
     */
    public function getBreaks(): ?bool
    {
        return $this->breaks;
    }
}

error message:
[PhpCsFixer\Linter\LintingException]
PHP Parse error: syntax error, unexpected '?', expecting function (T_FUNCTION) or const (T_CONST) on line 6.

@akielbasa what rules are you using? The ones from the first comment are causing no error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

teohhanhui picture teohhanhui  路  3Comments

vitek-rostislav picture vitek-rostislav  路  3Comments

grachevko picture grachevko  路  3Comments

amitbisht511 picture amitbisht511  路  3Comments

BackEndTea picture BackEndTea  路  3Comments