Skip to content

Commit

Permalink
[DeadCode] Skip get/set comments removal as yields false positives
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 29, 2023
1 parent aceaedf commit fe60789
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 170 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ jobs:
name: 'Tests'
run: vendor/bin/phpunit

-
name: 'PHP Linter'
run: vendor/bin/parallel-lint src tests

-
name: 'Check Commented Code'
run: vendor/bin/easy-ci check-commented-code src tests --ansi
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
"require": {
"php": ">=8.1",
"nette/utils": "^3.2",
"friendsofphp/php-cs-fixer": "^3.18",
"friendsofphp/php-cs-fixer": "^3.40",
"symplify/rule-doc-generator-contracts": "^11.1"
},
"require-dev": {
"symplify/easy-coding-standard": "^12.0.1",
"symplify/easy-coding-standard": "^12.0.8",
"squizlabs/php_codesniffer": "^3.7.2",
"phpunit/phpunit": "^10.2",
"symplify/rule-doc-generator": "^12.0",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10.26",
"rector/rector": "^0.17.7",
"symplify/easy-ci": "^11.3",
"symplify/phpstan-extensions": "^11.2",
"tomasvotruba/unused-public": "^0.2",
"tomasvotruba/unused-public": "^0.3",
"tomasvotruba/type-coverage": "^0.2",
"tomasvotruba/class-leak": "^0.1"
"tomasvotruba/class-leak": "^0.2",
"tracy/tracy": "^2.10"
},
"autoload": {
"psr-4": {
Expand Down
76 changes: 4 additions & 72 deletions src/DocBlock/UselessDocBlockCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ final class UselessDocBlockCleaner
self::TODO_IMPLEMENT_METHOD_COMMENT_BY_PHPSTORM_REGEX,
self::COMMENT_CLASS_REGEX,
self::COMMENT_CONSTRUCTOR_CLASS_REGEX,
self::COMMENT_METHOD_CLASS_REGEX,
];

/**
Expand All @@ -32,24 +31,6 @@ final class UselessDocBlockCleaner
*/
private const TODO_COMMENT_BY_PHPSTORM_REGEX = '#\/\/ TODO: Change the autogenerated stub$#';

/**
* @see https://regex101.com/r/QeAiRV/1
* @var string
*/
private const SPACE_STAR_SLASH_REGEX = '#[\s\*\/]#';

/**
* @see https://regex101.com/r/S1wAAh/2
* @var string
*/
private const COMMENT_METHOD_CLASS_REGEX = '#^\s{0,}(\/\*{2}\s+?)?(\*|\/\/)\s+([Gg]et|[Ss]et)\s+[^\s]*\.?(\s+\*\/)?$#';

/**
* @see https://regex101.com/r/eBux3I/1
* @var string
*/
private const COMMENT_ANY_METHOD_CLASS_REGEX = '#^\s{0,}(\/\*{2}\s+?)?(\*|\/\/)\s+(?<obvious_method_comment>([Gg]et|[Ss]et)\s+(.*))(\s+\*\/)?$#';

/**
* @see https://regex101.com/r/RzTdFH/4
* @var string
Expand All @@ -65,63 +46,14 @@ final class UselessDocBlockCleaner
/**
* @param Token[] $tokens
*/
public function clearDocTokenContent(array $tokens, int $position, string $docContent): string
public function clearDocTokenContent(array $tokens, int $position, Token $currentToken): string
{
$docContent = $currentToken->getContent();

foreach (self::CLEANING_REGEXES as $cleaningRegex) {
$docContent = Strings::replace($docContent, $cleaningRegex, '');
}

return $this->cleanClassMethodCommentMimicMethodName($tokens, $position, $docContent);
}

/**
* @param Token[] $reversedTokens
*/
private function cleanClassMethodCommentMimicMethodName(
array $reversedTokens,
int $index,
string $docContent
): string {
$matchMethodClass = Strings::match($docContent, self::COMMENT_METHOD_CLASS_REGEX);
if ($matchMethodClass) {
return $docContent;
}

if (! $this->isNextFunction($reversedTokens, $index)) {
return $docContent;
}

$matchAnyMethodClass = Strings::match($docContent, self::COMMENT_ANY_METHOD_CLASS_REGEX);
if (! $matchAnyMethodClass) {
return $docContent;
}

$obviousMethodComment = $matchAnyMethodClass['obvious_method_comment'];
$obviousMethodComment = $this->removeSpaces($obviousMethodComment);

$methodNameContent = $reversedTokens[$index + 6]->getContent();

if (strtolower($obviousMethodComment) !== strtolower($methodNameContent)) {
return $docContent;
}

return Strings::replace($docContent, self::COMMENT_ANY_METHOD_CLASS_REGEX, '');
}

/**
* @param Token[] $reversedTokens
*/
private function isNextFunction(array $reversedTokens, int $index): bool
{
if (! isset($reversedTokens[$index + 4])) {
return false;
}

return $reversedTokens[$index + 4]->getContent() === 'function';
}

private function removeSpaces(string $content): string
{
return Strings::replace($content, self::SPACE_STAR_SLASH_REGEX, '');
return $docContent;
}
}
3 changes: 2 additions & 1 deletion src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
$cleanedDocContent = $this->uselessDocBlockCleaner->clearDocTokenContent(
$reversedTokens,
$index,
$token->getContent()
$token
);

if ($cleanedDocContent !== '') {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessClassCommentFixer\Fixture;

class SkipInlineSet
{
public function getTranslator()
{
// set value
$value = 1000;
}
}

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@
// initialize custom T_* token constants used by PHP_CodeSniffer parser
new Tokens();
}


// prefer local coding-standard over old, vendor one
exec('rm -rf vendor/symplify/easy-coding-standard/vendor/symplify/coding-standard/src');
exec('ln -s $PWD/src vendor/symplify/easy-coding-standard/vendor/symplify/coding-standard/');

0 comments on commit fe60789

Please sign in to comment.