Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoUselessCommentFixer - Introduction #4078

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ $config = PhpCsFixer\Config::create()
'no_unneeded_final_method' => true,
'no_unreachable_default_argument_value' => true,
'no_unset_on_property' => true,
'no_useless_comment' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_class_elements' => true,
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,10 @@ Choose from the list of available rules:

Unused ``use`` statements must be removed.

* **no_useless_comment**

There must be no comment like "Class Foo".

* **no_useless_else**

There should not be useless ``else`` cases.
Expand Down
4 changes: 0 additions & 4 deletions src/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ public function getPhpExecutable();
public function getRiskyAllowed();

/**
* Get rules.
*
* Keys of array are names of fixers/sets, values are true/false.
*
* @return array
Expand Down Expand Up @@ -174,8 +172,6 @@ public function setPhpExecutable($phpExecutable);
public function setRiskyAllowed($isRiskyAllowed);

/**
* Set rules.
*
* Keys of array are names of fixers or sets.
* Value for set must be bool (turn it on or off).
* Value for fixer may be bool (turn it on or off) or array of configuration
Expand Down
115 changes: 115 additions & 0 deletions src/Fixer/Comment/NoUselessCommentFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Fixer\Comment;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

/**
* @author Kuba Werłos <werlos@gmail.com>
*/
final class NoUselessCommentFixer extends AbstractFixer
{
/**
* {@inheritdoc}
*/
public function getDefinition()
{
return new FixerDefinition(
'There must be no comment like "Class Foo".',
[
new CodeSample('<?php
/**
* Class Foo
* Class to do something
*/
class Foo {}
'),
new CodeSample('<?php
class Foo {
/**
* Get bar
*/
function getBar() {
return "bar";
}
}
'),
]
);
}

/**
* {@inheritdoc}
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]);
}

/**
* {@inheritdoc}
*/
public function getPriority()
{
// must be run before NoEmptyCommentFixer, NoEmptyPhpdocFixer, PhpdocTrimConsecutiveBlankLineSeparationFixer and PhpdocTrimFixer
return 6;
}

/**
* {@inheritdoc}
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
continue;
}

$nextIndex = $tokens->getTokenNotOfKindSibling(
$index,
1,
[[T_WHITESPACE], [T_COMMENT], [T_DOC_COMMENT], [T_ABSTRACT], [T_FINAL], [T_PUBLIC], [T_PROTECTED], [T_PRIVATE], [T_STATIC]]
);
if (null === $nextIndex) {
continue;
}

if ($tokens[$nextIndex]->isGivenKind([T_CLASS, T_INTERFACE, T_TRAIT])) {
$newContent = Preg::replace(
'/((^|\R)\h*(#|\/*\**))\h*\b(Class|Interface|Trait)\h+[A-Za-z0-9\\\\_]+\.?(\h*\R\h*\*?|\h*$)/i',
'$1',
$token->getContent()
);
} elseif ($tokens[$nextIndex]->isGivenKind(T_FUNCTION)) {
$newContent = Preg::replace(
'/((^|\R)\h*(#|\/*\**))\h*\b((Gets?|SetS?)\h+[A-Za-z0-9\\\\_]+|[A-Za-z0-9\\\\_]+\h+constructor)\.?(\h*\R\h*\*?|\h*$)/i',
'$1',
$token->getContent()
);
} else {
continue;
}

if ($newContent === $token->getContent()) {
continue;
}

$tokens[$index] = new Token([$token->getId(), $newContent]);
}
}
}
2 changes: 0 additions & 2 deletions src/Fixer/ConfigurableFixerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
interface ConfigurableFixerInterface extends FixerInterface
{
/**
* Set configuration.
*
* New configuration must override current one, not patch it.
* Using `null` makes fixer to use default configuration (or reset configuration from previously configured back
* to default one).
Expand Down
4 changes: 4 additions & 0 deletions tests/AutoReview/FixerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public function provideFixersPriorityCases()
[$fixers['no_unused_imports'], $fixers['blank_line_after_namespace']],
[$fixers['no_unused_imports'], $fixers['no_extra_blank_lines']],
[$fixers['no_unused_imports'], $fixers['no_leading_import_slash']],
[$fixers['no_useless_comment'], $fixers['no_empty_comment']],
[$fixers['no_useless_comment'], $fixers['no_empty_phpdoc']],
[$fixers['no_useless_comment'], $fixers['phpdoc_trim']],
[$fixers['no_useless_comment'], $fixers['phpdoc_trim_consecutive_blank_line_separation']],
[$fixers['no_useless_else'], $fixers['braces']],
[$fixers['no_useless_else'], $fixers['combine_consecutive_unsets']],
[$fixers['no_useless_else'], $fixers['no_extra_blank_lines']],
Expand Down
Loading