|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace NijiDigital\PhpStanRules\Rules; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\New_; |
| 9 | +use PhpParser\Node\Name; |
| 10 | +use PhpParser\Node\Scalar\String_; |
| 11 | +use PHPStan\Analyser\Scope; |
| 12 | +use PHPStan\Rules\Rule; |
| 13 | +use PHPStan\Rules\RuleError; |
| 14 | +use PHPStan\Rules\RuleErrorBuilder; |
| 15 | +use Psr\Clock\ClockInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * @implements Rule<New_> |
| 19 | + */ |
| 20 | +class NoRelativeDatetime implements Rule |
| 21 | +{ |
| 22 | + public const TIP = 'Use dependency injection to get an instance of ' . ClockInterface::class; |
| 23 | + |
| 24 | + #[\Override] |
| 25 | + public function getNodeType(): string |
| 26 | + { |
| 27 | + return New_::class; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @return RuleError[] |
| 32 | + */ |
| 33 | + #[\Override] |
| 34 | + public function processNode(Node $node, Scope $scope): array |
| 35 | + { |
| 36 | + if (!$node instanceof New_) { |
| 37 | + return []; |
| 38 | + } |
| 39 | + |
| 40 | + if (!$node->class instanceof Name) { |
| 41 | + return []; |
| 42 | + } |
| 43 | + |
| 44 | + $className = $node->class->toString(); |
| 45 | + if (!in_array($className, [\DateTime::class, \DateTimeImmutable::class], true)) { |
| 46 | + return []; |
| 47 | + } |
| 48 | + |
| 49 | + if ([] === $node->getArgs()) { |
| 50 | + return [ |
| 51 | + RuleErrorBuilder::message( |
| 52 | + sprintf( |
| 53 | + 'Usage of %s constructor without any argument is forbidden. Use %s::now().', |
| 54 | + $className, |
| 55 | + ClockInterface::class |
| 56 | + ) |
| 57 | + ) |
| 58 | + ->identifier('noRelativeDatetime.emptyConstructorCall') |
| 59 | + ->tip(self::TIP) |
| 60 | + ->build(), |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + $firstArg = $node->getArgs()[0]->value; |
| 65 | + if (!$firstArg instanceof String_) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + $dateString = strtolower($firstArg->value); |
| 70 | + |
| 71 | + $relativeKeywords = [ |
| 72 | + 'now', 'today', 'tomorrow', 'yesterday', |
| 73 | + 'next', 'last', 'first', 'second', |
| 74 | + 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', |
| 75 | + 'january', 'february', 'march', 'april', 'may', 'june', 'july', |
| 76 | + 'august', 'september', 'october', 'november', 'december', |
| 77 | + 'week', 'month', 'year', 'day', |
| 78 | + 'hour', 'minute', 'second', |
| 79 | + ]; |
| 80 | + |
| 81 | + foreach ($relativeKeywords as $relativeKeyword) { |
| 82 | + if (str_contains($dateString, $relativeKeyword)) { |
| 83 | + return [ |
| 84 | + RuleErrorBuilder::message( |
| 85 | + sprintf( |
| 86 | + 'Usage of relative date format ("%1$s") in %2$s is forbidden. Use %3$s::now()->modify("%1$s").', |
| 87 | + $dateString, |
| 88 | + $className, |
| 89 | + ClockInterface::class |
| 90 | + ) |
| 91 | + ) |
| 92 | + ->identifier('noRelativeDatetime.relativeDate') |
| 93 | + ->tip(self::TIP) |
| 94 | + ->build(), |
| 95 | + ]; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return []; |
| 100 | + } |
| 101 | +} |
0 commit comments