Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require @internal annotation on Psalm\Internal symbols #7268

Merged
merged 3 commits into from Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions UPGRADING.md
Expand Up @@ -114,6 +114,8 @@
- `Psalm\Type\Atomic\TValueOfClassConstant`
- `Psalm\Type\Atomic\TVoid`
- `Psalm\Type\Union`
- While not a BC break per se, all classes / interfaces / traits / enums under
`Psalm\Internal` namespace are now marked `@internal`.

## Removed
- [BC] Property `Psalm\Codebase::$php_major_version` was removed, use
Expand Down
58 changes: 58 additions & 0 deletions examples/plugins/InternalChecker.php
@@ -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;
}
}