Skip to content

Commit ff41218

Browse files
authored
[DeadCode] Skip set/get inlined comments to keep safe (#33)
1 parent aceaedf commit ff41218

File tree

8 files changed

+28
-170
lines changed

8 files changed

+28
-170
lines changed

.github/workflows/code_analysis.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ jobs:
3636
name: 'Tests'
3737
run: vendor/bin/phpunit
3838

39-
-
40-
name: 'PHP Linter'
41-
run: vendor/bin/parallel-lint src tests
42-
4339
-
4440
name: 'Check Commented Code'
4541
run: vendor/bin/easy-ci check-commented-code src tests --ansi

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
"require": {
66
"php": ">=8.1",
77
"nette/utils": "^3.2",
8-
"friendsofphp/php-cs-fixer": "^3.18",
8+
"friendsofphp/php-cs-fixer": "^3.40",
99
"symplify/rule-doc-generator-contracts": "^11.1"
1010
},
1111
"require-dev": {
12-
"symplify/easy-coding-standard": "^12.0.1",
12+
"symplify/easy-coding-standard": "^12.0.8",
1313
"squizlabs/php_codesniffer": "^3.7.2",
1414
"phpunit/phpunit": "^10.2",
1515
"symplify/rule-doc-generator": "^12.0",
16-
"php-parallel-lint/php-parallel-lint": "^1.3",
1716
"phpstan/extension-installer": "^1.3",
1817
"phpstan/phpstan": "^1.10.26",
1918
"rector/rector": "^0.17.7",
2019
"symplify/easy-ci": "^11.3",
2120
"symplify/phpstan-extensions": "^11.2",
22-
"tomasvotruba/unused-public": "^0.2",
21+
"tomasvotruba/unused-public": "^0.3",
2322
"tomasvotruba/type-coverage": "^0.2",
24-
"tomasvotruba/class-leak": "^0.1"
23+
"tomasvotruba/class-leak": "^0.2",
24+
"tracy/tracy": "^2.10"
2525
},
2626
"autoload": {
2727
"psr-4": {

src/DocBlock/UselessDocBlockCleaner.php

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ final class UselessDocBlockCleaner
1717
self::TODO_IMPLEMENT_METHOD_COMMENT_BY_PHPSTORM_REGEX,
1818
self::COMMENT_CLASS_REGEX,
1919
self::COMMENT_CONSTRUCTOR_CLASS_REGEX,
20-
self::COMMENT_METHOD_CLASS_REGEX,
2120
];
2221

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

35-
/**
36-
* @see https://regex101.com/r/QeAiRV/1
37-
* @var string
38-
*/
39-
private const SPACE_STAR_SLASH_REGEX = '#[\s\*\/]#';
40-
41-
/**
42-
* @see https://regex101.com/r/S1wAAh/2
43-
* @var string
44-
*/
45-
private const COMMENT_METHOD_CLASS_REGEX = '#^\s{0,}(\/\*{2}\s+?)?(\*|\/\/)\s+([Gg]et|[Ss]et)\s+[^\s]*\.?(\s+\*\/)?$#';
46-
47-
/**
48-
* @see https://regex101.com/r/eBux3I/1
49-
* @var string
50-
*/
51-
private const COMMENT_ANY_METHOD_CLASS_REGEX = '#^\s{0,}(\/\*{2}\s+?)?(\*|\/\/)\s+(?<obvious_method_comment>([Gg]et|[Ss]et)\s+(.*))(\s+\*\/)?$#';
52-
5334
/**
5435
* @see https://regex101.com/r/RzTdFH/4
5536
* @var string
@@ -65,63 +46,14 @@ final class UselessDocBlockCleaner
6546
/**
6647
* @param Token[] $tokens
6748
*/
68-
public function clearDocTokenContent(array $tokens, int $position, string $docContent): string
49+
public function clearDocTokenContent(array $tokens, int $position, Token $currentToken): string
6950
{
51+
$docContent = $currentToken->getContent();
52+
7053
foreach (self::CLEANING_REGEXES as $cleaningRegex) {
7154
$docContent = Strings::replace($docContent, $cleaningRegex, '');
7255
}
7356

74-
return $this->cleanClassMethodCommentMimicMethodName($tokens, $position, $docContent);
75-
}
76-
77-
/**
78-
* @param Token[] $reversedTokens
79-
*/
80-
private function cleanClassMethodCommentMimicMethodName(
81-
array $reversedTokens,
82-
int $index,
83-
string $docContent
84-
): string {
85-
$matchMethodClass = Strings::match($docContent, self::COMMENT_METHOD_CLASS_REGEX);
86-
if ($matchMethodClass) {
87-
return $docContent;
88-
}
89-
90-
if (! $this->isNextFunction($reversedTokens, $index)) {
91-
return $docContent;
92-
}
93-
94-
$matchAnyMethodClass = Strings::match($docContent, self::COMMENT_ANY_METHOD_CLASS_REGEX);
95-
if (! $matchAnyMethodClass) {
96-
return $docContent;
97-
}
98-
99-
$obviousMethodComment = $matchAnyMethodClass['obvious_method_comment'];
100-
$obviousMethodComment = $this->removeSpaces($obviousMethodComment);
101-
102-
$methodNameContent = $reversedTokens[$index + 6]->getContent();
103-
104-
if (strtolower($obviousMethodComment) !== strtolower($methodNameContent)) {
105-
return $docContent;
106-
}
107-
108-
return Strings::replace($docContent, self::COMMENT_ANY_METHOD_CLASS_REGEX, '');
109-
}
110-
111-
/**
112-
* @param Token[] $reversedTokens
113-
*/
114-
private function isNextFunction(array $reversedTokens, int $index): bool
115-
{
116-
if (! isset($reversedTokens[$index + 4])) {
117-
return false;
118-
}
119-
120-
return $reversedTokens[$index + 4]->getContent() === 'function';
121-
}
122-
123-
private function removeSpaces(string $content): string
124-
{
125-
return Strings::replace($content, self::SPACE_STAR_SLASH_REGEX, '');
57+
return $docContent;
12658
}
12759
}

src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
6060
$cleanedDocContent = $this->uselessDocBlockCleaner->clearDocTokenContent(
6161
$reversedTokens,
6262
$index,
63-
$token->getContent()
63+
$token
6464
);
65+
6566
if ($cleanedDocContent !== '') {
6667
continue;
6768
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessClassCommentFixer\Fixture;
4+
5+
class SkipInlineSet
6+
{
7+
public function getTranslator()
8+
{
9+
// set value
10+
$value = 1000;
11+
}
12+
}

tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_method_comment.php.inc

Lines changed: 0 additions & 45 deletions
This file was deleted.

tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_extra_space_class_comment.php.inc

Lines changed: 0 additions & 43 deletions
This file was deleted.

tests/bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@
2828
// initialize custom T_* token constants used by PHP_CodeSniffer parser
2929
new Tokens();
3030
}
31+
32+
33+
// prefer local coding-standard over old, vendor one
34+
exec('rm -rf vendor/symplify/easy-coding-standard/vendor/symplify/coding-standard/src');
35+
exec('ln -s $PWD/src vendor/symplify/easy-coding-standard/vendor/symplify/coding-standard/');

0 commit comments

Comments
 (0)