-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check leading and trailing file whitespace and BOM
- Loading branch information
1 parent
37e65a4
commit 6905d66
Showing
9 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ parameters: | |
closureUsesThis: true | ||
randomIntParameters: true | ||
nullCoalesce: true | ||
fileWhitespace: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Whitespace; | ||
|
||
use Nette\Utils\Strings; | ||
use PhpParser\Node; | ||
use PhpParser\NodeTraverser; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\FileNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<FileNode> | ||
*/ | ||
class FileWhitespaceRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return FileNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$nodes = $node->getNodes(); | ||
if (count($nodes) === 0) { | ||
return []; | ||
} | ||
|
||
$firstNode = $nodes[0]; | ||
$messages = []; | ||
if ($firstNode instanceof Node\Stmt\InlineHTML && $firstNode->value === "\xef\xbb\xbf") { | ||
$messages[] = RuleErrorBuilder::message('File begins with UTF-8 BOM character. This may cause problems when running the code in the web browser.')->build(); | ||
} | ||
|
||
$nodeTraverser = new NodeTraverser(); | ||
$visitor = new class () extends \PhpParser\NodeVisitorAbstract { | ||
|
||
/** @var \PhpParser\Node[] */ | ||
private $lastNodes = []; | ||
|
||
/** | ||
* @param Node $node | ||
* @return int|Node|null | ||
*/ | ||
public function enterNode(Node $node) | ||
{ | ||
if ($node instanceof Node\Stmt\Declare_) { | ||
if ($node->stmts !== null && count($node->stmts) > 0) { | ||
$this->lastNodes[] = $node->stmts[count($node->stmts) - 1]; | ||
} | ||
return null; | ||
} | ||
if ($node instanceof Node\Stmt\Namespace_) { | ||
if (count($node->stmts) > 0) { | ||
$this->lastNodes[] = $node->stmts[count($node->stmts) - 1]; | ||
} | ||
return null; | ||
} | ||
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; | ||
} | ||
|
||
/** | ||
* @return Node[] | ||
*/ | ||
public function getLastNodes(): array | ||
{ | ||
return $this->lastNodes; | ||
} | ||
|
||
}; | ||
$nodeTraverser->addVisitor($visitor); | ||
$nodeTraverser->traverse($nodes); | ||
|
||
$lastNodes = $visitor->getLastNodes(); | ||
if (count($nodes) > 0) { | ||
$lastNodes[] = $nodes[count($nodes) - 1]; | ||
} | ||
foreach ($lastNodes as $lastNode) { | ||
if (!$lastNode instanceof Node\Stmt\InlineHTML || Strings::match($lastNode->value, '#(\s+)#') === null) { | ||
continue; | ||
} | ||
|
||
$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(); | ||
} | ||
|
||
return $messages; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Whitespace; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<FileWhitespaceRule> | ||
*/ | ||
class FileWhitespaceRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new FileWhitespaceRule(); | ||
} | ||
|
||
public function testBom(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/bom.php'], [ | ||
[ | ||
'File begins with UTF-8 BOM character. This may cause problems when running the code in the web browser.', | ||
1, | ||
], | ||
]); | ||
} | ||
|
||
public function testCorrectFile(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/correct.php'], []); | ||
} | ||
|
||
public function testTrailingWhitespaceWithoutNamespace(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/trailing.php'], [ | ||
[ | ||
'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.', | ||
6, | ||
], | ||
]); | ||
} | ||
|
||
public function testTrailingWhitespace(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/trailing-namespace.php'], [ | ||
[ | ||
'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.', | ||
8, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
echo 'test'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace Test; | ||
|
||
echo 'foo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Test; | ||
|
||
echo 'foo'; | ||
|
||
?> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
echo 'foo'; | ||
|
||
?> | ||
|