Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7268 from weirdan/internal-is-internal
- Loading branch information
Showing
205 changed files
with
640 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.