Skip to content

Commit

Permalink
False positive on PHP 8.0 attribute style comment (#644)
Browse files Browse the repository at this point in the history
* False positive on PHP 8.0 attribute style comment

Inserting a space character after "#" single-line comment start is causing
disruption for comments that represent a PHP forward compatible PHP 8.0
attribute comment.

Code not tokenized as PHP 8.0+ have this as a single line comment
(T_COMMENT) and as the notation has been designed forward compatible, a
comment fixer introducing the space is much likely a false positive:

    #[\ReturnTypeWillChange]

    # [\ReturnTypeWillChange]

Change is to *not* insert the space when "[" <U005B> LEFT SQUARE BRACKET
follows "#" <U0023> NUMBER SIGN for the CommentSurroundedBySpacesFixer[1].

[1]: \PhpCsFixerCustomFixers\Fixer\CommentSurroundedBySpacesFixer

* Update README.md

Co-authored-by: Kuba Werłos <werlos@gmail.com>
  • Loading branch information
ktomk and kubawerlos committed Nov 8, 2021
1 parent a2cfe31 commit 541984e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -3,7 +3,7 @@
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
![Tests](https://img.shields.io/badge/tests-2985-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-2987-brightgreen.svg)
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)

[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/CommentSurroundedBySpacesFixer.php
Expand Up @@ -61,7 +61,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
/** @var string $newContent */
$newContent = Preg::replace(
[
'/^(\/\/|#|\/\*+)((?!(?:\/|\*|\h)).+)$/',
'/^(\/\/|#(?!\[)|\/\*+)((?!(?:\/|\*|\h)).+)$/',
'/^(.+(?<!(?:\/|\*|\h)))(\*+\/)$/',
],
'$1 $2',
Expand Down
9 changes: 9 additions & 0 deletions tests/Fixer/CommentSurroundedBySpacesFixerTest.php
Expand Up @@ -69,6 +69,15 @@ public static function provideFixCases(): iterable
'<?php $a; #foo',
];

yield [
'<?php $a; #',
];

yield [
'<?php #[\ReturnTypeWillChange]
function doFoo() {}',
];

yield [
'<?php $a; /* foo */',
'<?php $a; /*foo */',
Expand Down

0 comments on commit 541984e

Please sign in to comment.