Skip to content

Commit

Permalink
Merge pull request #7268 from weirdan/internal-is-internal
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Jan 3, 2022
2 parents ec023f4 + 2066a21 commit e157a6e
Show file tree
Hide file tree
Showing 205 changed files with 640 additions and 297 deletions.
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;
}
}

0 comments on commit e157a6e

Please sign in to comment.