-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docblock] Remove commment duplicating class name (#55)
- Loading branch information
1 parent
a7fff18
commit 5de5266
Showing
5 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Symplify\CodingStandard\Fixer\Naming; | ||
|
||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
use SplFileInfo; | ||
|
||
final class ClassNameResolver | ||
{ | ||
/** | ||
* @var array<string, string> | ||
*/ | ||
private array $classNameByFilePath = []; | ||
|
||
/** | ||
* @param Tokens<Token> $tokens | ||
*/ | ||
public function resolveClassName(SplFileInfo $splFileInfo, Tokens $tokens): ?string | ||
{ | ||
$filePath = $splFileInfo->getRealPath(); | ||
|
||
if (isset($this->classNameByFilePath[$filePath])) { | ||
return $this->classNameByFilePath[$filePath]; | ||
} | ||
|
||
$classLikeName = $this->resolveFromTokens($tokens); | ||
if (! is_string($classLikeName)) { | ||
return null; | ||
} | ||
|
||
$this->classNameByFilePath[$filePath] = $classLikeName; | ||
|
||
return $classLikeName; | ||
} | ||
|
||
/** | ||
* @param Tokens<Token> $tokens | ||
*/ | ||
private function resolveFromTokens(Tokens $tokens): ?string | ||
{ | ||
foreach ($tokens as $position => $token) { | ||
if (! $token->isGivenKind([T_CLASS, T_TRAIT, T_INTERFACE])) { | ||
continue; | ||
} | ||
|
||
$nextNextMeaningfulTokenIndex = $tokens->getNextMeaningfulToken($position + 1); | ||
$nextNextMeaningfulToken = $tokens[$nextNextMeaningfulTokenIndex]; | ||
|
||
// skip anonymous classes | ||
if (! $nextNextMeaningfulToken->isGivenKind(T_STRING)) { | ||
continue; | ||
} | ||
|
||
return $nextNextMeaningfulToken->getContent(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/name_class_itself.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture; | ||
|
||
/** | ||
* RemoveBareClassName | ||
*/ | ||
class RemoveBareClassName | ||
{ | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture; | ||
|
||
|
||
class RemoveBareClassName | ||
{ | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/trait_name_class_itself.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture; | ||
|
||
/** | ||
* useful comment here | ||
* | ||
* ThisIsOnlyTrait | ||
*/ | ||
trait ThisIsOnlyTrait | ||
{ | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessDefaultCommentFixer\Fixture; | ||
|
||
/** | ||
* useful comment here | ||
* | ||
*/ | ||
trait ThisIsOnlyTrait | ||
{ | ||
} | ||
|
||
?> |