1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 10:57:08 +01:00
psalm/examples/plugins/composer-based/echo-checker/EchoChecker.php

68 lines
2.4 KiB
PHP
Raw Normal View History

2018-10-29 17:15:36 +01:00
<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Example\Plugin\ComposerBased;
2018-10-29 17:15:36 +01:00
use PhpParser;
use Psalm\CodeLocation;
use Psalm\FileManipulation;
2018-10-29 17:15:36 +01:00
use Psalm\IssueBuffer;
use Psalm\Issue\ArgumentTypeCoercion;
use Psalm\Plugin\EventHandler\AfterStatementAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterStatementAnalysisEvent;
2018-10-29 17:15:36 +01:00
class EchoChecker implements AfterStatementAnalysisInterface
2018-10-29 17:15:36 +01:00
{
/**
2018-11-06 03:57:36 +01:00
* Called after a statement has been checked
2018-10-29 17:15:36 +01:00
*
* @param FileManipulation[] $file_replacements
*
* @return null|false
*/
public static function afterStatementAnalysis(AfterStatementAnalysisEvent $event): ?bool {
$stmt = $event->getStmt();
$statements_source = $event->getStatementsSource();
2018-10-29 17:15:36 +01:00
if ($stmt instanceof PhpParser\Node\Stmt\Echo_) {
foreach ($stmt->exprs as $expr) {
$expr_type = $statements_source->getNodeTypeProvider()->getType($expr);
if (!$expr_type || $expr_type->hasMixed()) {
2018-10-29 17:15:36 +01:00
if (IssueBuffer::accepts(
new ArgumentTypeCoercion(
'Echo requires an unescaped string, ' . $expr_type . ' provided',
new CodeLocation($statements_source, $expr),
'echo'
2018-10-29 17:15:36 +01:00
),
2018-11-06 03:57:36 +01:00
$statements_source->getSuppressedIssues()
2018-10-29 17:15:36 +01:00
)) {
// keep soldiering on
}
continue;
}
$types = $expr_type->getAtomicTypes();
2018-10-29 17:15:36 +01:00
foreach ($types as $type) {
if ($type instanceof \Psalm\Type\Atomic\TString
&& !$type instanceof \Psalm\Type\Atomic\TLiteralString
&& !$type instanceof \Psalm\Type\Atomic\THtmlEscapedString
) {
if (IssueBuffer::accepts(
new ArgumentTypeCoercion(
'Echo requires an unescaped string, ' . $expr_type . ' provided',
new CodeLocation($statements_source, $expr),
'echo'
2018-10-29 17:15:36 +01:00
),
2018-11-06 03:57:36 +01:00
$statements_source->getSuppressedIssues()
2018-10-29 17:15:36 +01:00
)) {
// keep soldiering on
}
}
}
}
}
return null;
2018-10-29 17:15:36 +01:00
}
}