1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 17:27:28 +01:00
psalm/examples/plugins/InternalChecker.php

59 lines
1.9 KiB
PHP
Raw Normal View History

<?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;
2023-10-26 17:00:29 +02:00
final class InternalChecker implements AfterClassLikeAnalysisInterface
{
/** @return null|false */
2022-12-18 17:15:15 +01:00
public static function afterStatementAnalysis(AfterClassLikeAnalysisEvent $event): ?bool
{
$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,
2022-12-18 17:15:15 +01:00
$storage->name,
),
$event->getStatementsSource()->getSuppressedIssues(),
2022-12-18 17:15:15 +01:00
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([
2022-12-18 17:15:15 +01:00
new FileManipulation($docblock_start, $docblock_end, $new_docblock_content),
]);
}
return null;
}
}