1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Require @internal annotation on Psalm\Internal symbols

This commit is contained in:
Bruce Weirdan 2022-01-03 08:42:51 +02:00
parent ec023f4477
commit b54ab67c76
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?php
namespace Psalm\Example\Plugin;
use Psalm\DocComment;
use Psalm\FileManipulation;
use Psalm\Internal\Scanner\ParsedDocblock;
use Psalm\Issue\InternalClass;
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\AfterClassLikeAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeAnalysisEvent;
use function strpos;
class InternalChecker implements AfterClassLikeAnalysisInterface
{
/** @return null|false */
public static function afterStatementAnalysis(AfterClassLikeAnalysisEvent $event)
{
$storage = $event->getClasslikeStorage();
if (!$storage->internal
&& strpos($storage->name, 'Psalm\\Internal') === 0
&& $storage->location
) {
IssueBuffer::maybeAdd(
new InternalClass(
"Class $storage->name must be marked @internal",
$storage->location,
$storage->name
),
$event->getStatementsSource()->getSuppressedIssues(),
true
);
if (!$event->getCodebase()->alter_code) {
return null;
}
$stmt = $event->getStmt();
$docblock = $stmt->getDocComment();
if ($docblock) {
$docblock_start = $docblock->getStartFilePos();
$parsed_docblock = DocComment::parsePreservingLength($docblock);
} else {
$docblock_start = (int) $stmt->getAttribute('startFilePos');
$parsed_docblock = new ParsedDocblock('', []);
}
$docblock_end = (int) $stmt->getAttribute('startFilePos');
$parsed_docblock->tags['internal'] = [''];
$new_docblock_content = $parsed_docblock->render('');
$event->setFileReplacements([
new FileManipulation($docblock_start, $docblock_end, $new_docblock_content)
]);
}
return null;
}
}

View File

@ -46,6 +46,7 @@
<plugins>
<plugin filename="examples/plugins/FunctionCasingChecker.php"/>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
<plugin filename="examples/plugins/InternalChecker.php"/>
</plugins>
<issueHandlers>