Php-cs-fixer: HeaderComment - tool generates invalid PHP

Created on 1 Nov 2018  Â·  2Comments  Â·  Source: FriendsOfPHP/PHP-CS-Fixer

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

PHP 7.2.11-2+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Oct 15 2018 11:40:04) ( NTS )

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

2.12 / 2.13

The command you use to run PHP CS Fixer:

$ php-cs-fixer fix tmp/test2.php --dry-run -vvv --diff

The configuration file you are using, if any:

<?php

$header = <<<'EOF'
/**
 * Foo
 */
EOF;

$finder = PhpCsFixer\Finder::create()
    ->in(__DIR__)
;

return PhpCsFixer\Config::create()
    ->setRules([
        'header_comment' => [
            'header' => $header,
            'location' => 'after_open',
            'separate' => 'bottom'
        ],
    ])
    ->setFinder($finder)
;

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

<?php

  • with unexpected changes applied when running PHP CS Fixer:
    [none]
$ php-cs-fixer fix tmp/test2.php --dry-run -vvv --diff
Loaded config default from "/home/possum/work/PHP-CS-Fixer/.php_cs.dist".
Using cache file ".php_cs.cache".
Paths from configuration file have been overridden by paths provided as command arguments.
E
Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error

Checked all files in 0.006 seconds, 10.000 MB memory used

Files that were not fixed due to errors reported during linting after fixing:
   1) /home/possum/work/PHP-CS-Fixer/tmp/test2.php


        [PhpCsFixer\Linter\LintingException]                                             
        PHP Parse error: syntax error, unexpected '*', expecting end of file on line 6.  


      Applied fixers: header_comment
      ---------- begin diff ----------
--- Original
+++ New
@@ @@
 <?php
+/*
+ * /**
+ *  * Foo
+ *  */
+ */
+

  • with the changes you expected instead:
<?php
/**
 * Foo
 */

--- Update ---

Clearly this is an user error for providing the header as PHPDoc already.
However we could detect this case during configuration of the rule and throw a nice exception message and not crash during fixing.

kinbug

Most helpful comment

I prefer to see the fixer throw an InvalidFixerConfigurationException when */ is in the configured header

All 2 comments

Should we throw exception if it contains */, as that is the sequence that breaks it. Or should it be escaped somehow?

I prefer to see the fixer throw an InvalidFixerConfigurationException when */ is in the configured header

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fisharebest picture fisharebest  Â·  3Comments

kcloze picture kcloze  Â·  3Comments

datenmeister picture datenmeister  Â·  3Comments

aidantwoods picture aidantwoods  Â·  3Comments

Bilge picture Bilge  Â·  3Comments