-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRedundantRegexDefinitionRule.php
More file actions
57 lines (47 loc) · 1.63 KB
/
RedundantRegexDefinitionRule.php
File metadata and controls
57 lines (47 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace Rector\Behastan\Rule;
use Rector\Behastan\Contract\RuleInterface;
use Rector\Behastan\Enum\RuleIdentifier;
use Rector\Behastan\ValueObject\Pattern\RegexPattern;
use Rector\Behastan\ValueObject\PatternCollection;
use Rector\Behastan\ValueObject\RuleError;
use Symfony\Component\Finder\SplFileInfo;
final readonly class RedundantRegexDefinitionRule implements RuleInterface
{
/**
* @param SplFileInfo[] $contextFiles
* @param SplFileInfo[] $featureFiles
* @return RuleError[]
*/
public function process(
array $contextFiles,
array $featureFiles,
PatternCollection $patternCollection,
string $projectDirectory
): array {
$ruleErrors = [];
/** @var RegexPattern[] $regexPatterns */
$regexPatterns = $patternCollection->byType(RegexPattern::class);
foreach ($regexPatterns as $regexPattern) {
// is regex pattern more then just exact start + end?
if ($regexPattern->isRegexPatternNeccessary()) {
continue;
}
$errorMessage = sprintf(
'The regex pattern "%s" is redundant. Remove start + end and use plain string exact string instead.',
$regexPattern->pattern,
);
$ruleErrors[] = new RuleError(
$errorMessage,
[$regexPattern->filePath . ':' . $regexPattern->line],
$this->getIdentifier()
);
}
return $ruleErrors;
}
public function getIdentifier(): string
{
return RuleIdentifier::REDUNDANT_REGEX_DEFINITION;
}
}