1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-23 06:11:25 +01:00

63 lines
2.1 KiB
PHP
Raw Normal View History

2018-10-29 12:15:36 -04:00
<?php
2022-01-03 22:37:41 +02:00
2018-11-05 21:57:36 -05:00
namespace Psalm\Example\Plugin\ComposerBased;
2018-10-29 12:15:36 -04:00
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Issue\ArgumentTypeCoercion;
2022-01-03 22:37:41 +02:00
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\AfterStatementAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterStatementAnalysisEvent;
2021-12-13 04:45:57 +01:00
use Psalm\Type\Atomic\TLiteralString;
2022-01-03 22:37:41 +02:00
use Psalm\Type\Atomic\TString;
2018-10-29 12:15:36 -04:00
class EchoChecker implements AfterStatementAnalysisInterface
2018-10-29 12:15:36 -04:00
{
/**
2018-11-05 21:57:36 -05:00
* Called after a statement has been checked
2018-10-29 12:15:36 -04:00
*
* @return null|false
*/
2022-01-03 22:37:41 +02:00
public static function afterStatementAnalysis(AfterStatementAnalysisEvent $event): ?bool
{
$stmt = $event->getStmt();
$statements_source = $event->getStatementsSource();
2018-10-29 12:15:36 -04: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()) {
2022-12-16 22:00:34 -06:00
IssueBuffer::maybeAdd(
new ArgumentTypeCoercion(
'Echo requires an unescaped string, ' . $expr_type . ' provided',
new CodeLocation($statements_source, $expr),
2022-12-18 10:15:15 -06:00
'echo',
2018-10-29 12:15:36 -04:00
),
2022-12-18 10:15:15 -06:00
$statements_source->getSuppressedIssues(),
2022-12-16 22:00:34 -06:00
);
2018-10-29 12:15:36 -04:00
continue;
}
$types = $expr_type->getAtomicTypes();
2018-10-29 12:15:36 -04:00
foreach ($types as $type) {
2021-12-13 04:45:57 +01:00
if ($type instanceof TString
&& !$type instanceof TLiteralString
2018-10-29 12:15:36 -04:00
) {
2022-12-16 22:00:34 -06:00
IssueBuffer::maybeAdd(
new ArgumentTypeCoercion(
'Echo requires an unescaped string, ' . $expr_type . ' provided',
new CodeLocation($statements_source, $expr),
2022-12-18 10:15:15 -06:00
'echo',
2018-10-29 12:15:36 -04:00
),
2022-12-18 10:15:15 -06:00
$statements_source->getSuppressedIssues(),
2022-12-16 22:00:34 -06:00
);
2018-10-29 12:15:36 -04:00
}
}
}
}
return null;
2018-10-29 12:15:36 -04:00
}
}