Skip to content

Commit

Permalink
Playground - NoPhpCodeRule
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 14, 2023
1 parent 66fdf01 commit 0745fbd
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions issue-bot/playground.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rules:
- PHPStan\Rules\Playground\FunctionNeverRule
- PHPStan\Rules\Playground\MethodNeverRule
- PHPStan\Rules\Playground\NoPhpCodeRule
41 changes: 41 additions & 0 deletions src/Rules/Playground/NoPhpCodeRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Playground;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\FileNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function count;

/**
* @implements Rule<FileNode>
*/
class NoPhpCodeRule implements Rule
{

public function getNodeType(): string
{
return FileNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (count($node->getNodes()) !== 1) {
return [];
}

$html = $node->getNodes()[0];
if (!$html instanceof Node\Stmt\InlineHTML) {
return [];
}

return [
RuleErrorBuilder::message('The example does not contain any PHP code. Did you forget the opening <?php tag?')
->identifier('phpstanPlayground.noPhp')
->build(),
];
}

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Playground/NoPhpCodeRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Playground;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<NoPhpCodeRule>
*/
class NoPhpCodeRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new NoPhpCodeRule();
}

public function testEmptyFile(): void
{
$this->analyse([__DIR__ . '/data/empty.php'], []);
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/no-php-code.php'], [
[
'The example does not contain any PHP code. Did you forget the opening <?php tag?',
1,
],
]);
}

}
Empty file.
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Playground/data/no-php-code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Foo
{
private int $foo;
}

0 comments on commit 0745fbd

Please sign in to comment.