|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Whitespace; |
| 4 | + |
| 5 | +use Nette\Utils\Strings; |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\NodeTraverser; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Node\FileNode; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\Rules\RuleErrorBuilder; |
| 12 | + |
| 13 | +/** |
| 14 | + * @implements Rule<FileNode> |
| 15 | + */ |
| 16 | +class FileWhitespaceRule implements Rule |
| 17 | +{ |
| 18 | + |
| 19 | + public function getNodeType(): string |
| 20 | + { |
| 21 | + return FileNode::class; |
| 22 | + } |
| 23 | + |
| 24 | + public function processNode(Node $node, Scope $scope): array |
| 25 | + { |
| 26 | + $nodes = $node->getNodes(); |
| 27 | + if (count($nodes) === 0) { |
| 28 | + return []; |
| 29 | + } |
| 30 | + |
| 31 | + $firstNode = $nodes[0]; |
| 32 | + $messages = []; |
| 33 | + if ($firstNode instanceof Node\Stmt\InlineHTML && $firstNode->value === "\xef\xbb\xbf") { |
| 34 | + $messages[] = RuleErrorBuilder::message('File begins with UTF-8 BOM character. This may cause problems when running the code in the web browser.')->build(); |
| 35 | + } |
| 36 | + |
| 37 | + $nodeTraverser = new NodeTraverser(); |
| 38 | + $visitor = new class () extends \PhpParser\NodeVisitorAbstract { |
| 39 | + |
| 40 | + /** @var \PhpParser\Node[] */ |
| 41 | + private $lastNodes = []; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param Node $node |
| 45 | + * @return int|Node|null |
| 46 | + */ |
| 47 | + public function enterNode(Node $node) |
| 48 | + { |
| 49 | + if ($node instanceof Node\Stmt\Declare_) { |
| 50 | + if ($node->stmts !== null && count($node->stmts) > 0) { |
| 51 | + $this->lastNodes[] = $node->stmts[count($node->stmts) - 1]; |
| 52 | + } |
| 53 | + return null; |
| 54 | + } |
| 55 | + if ($node instanceof Node\Stmt\Namespace_) { |
| 56 | + if (count($node->stmts) > 0) { |
| 57 | + $this->lastNodes[] = $node->stmts[count($node->stmts) - 1]; |
| 58 | + } |
| 59 | + return null; |
| 60 | + } |
| 61 | + return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return Node[] |
| 66 | + */ |
| 67 | + public function getLastNodes(): array |
| 68 | + { |
| 69 | + return $this->lastNodes; |
| 70 | + } |
| 71 | + |
| 72 | + }; |
| 73 | + $nodeTraverser->addVisitor($visitor); |
| 74 | + $nodeTraverser->traverse($nodes); |
| 75 | + |
| 76 | + $lastNodes = $visitor->getLastNodes(); |
| 77 | + if (count($nodes) > 0) { |
| 78 | + $lastNodes[] = $nodes[count($nodes) - 1]; |
| 79 | + } |
| 80 | + foreach ($lastNodes as $lastNode) { |
| 81 | + if (!$lastNode instanceof Node\Stmt\InlineHTML || Strings::match($lastNode->value, '#(\s+)#') === null) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + $messages[] = RuleErrorBuilder::message('File ends with a trailing whitespace. This may cause problems when running the code in the web browser. Remove the closing ?> mark or remove the whitespace.')->line($lastNode->getStartLine())->build(); |
| 86 | + } |
| 87 | + |
| 88 | + return $messages; |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments