2018-10-29 17:15:36 +01:00
|
|
|
<?php
|
2022-01-03 21:37:41 +01:00
|
|
|
|
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;
|
2019-04-26 00:02:19 +02:00
|
|
|
use Psalm\Issue\ArgumentTypeCoercion;
|
2022-01-03 21:37:41 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2021-01-06 15:05:53 +01:00
|
|
|
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 21:37:41 +01:00
|
|
|
use Psalm\Type\Atomic\TString;
|
2018-10-29 17:15:36 +01:00
|
|
|
|
2018-11-12 16:11:08 +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
|
|
|
*
|
|
|
|
* @return null|false
|
|
|
|
*/
|
2022-01-03 21:37:41 +01:00
|
|
|
public static function afterStatementAnalysis(AfterStatementAnalysisEvent $event): ?bool
|
|
|
|
{
|
2021-01-06 15:05:53 +01:00
|
|
|
$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) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$expr_type = $statements_source->getNodeTypeProvider()->getType($expr);
|
|
|
|
|
|
|
|
if (!$expr_type || $expr_type->hasMixed()) {
|
2022-12-17 05:00:34 +01:00
|
|
|
IssueBuffer::maybeAdd(
|
2019-04-26 00:02:19 +02:00
|
|
|
new ArgumentTypeCoercion(
|
2019-11-25 17:44:54 +01:00
|
|
|
'Echo requires an unescaped string, ' . $expr_type . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
new CodeLocation($statements_source, $expr),
|
2022-12-18 17:15:15 +01:00
|
|
|
'echo',
|
2018-10-29 17:15:36 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$statements_source->getSuppressedIssues(),
|
2022-12-17 05:00:34 +01:00
|
|
|
);
|
2018-10-29 17:15:36 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
$types = $expr_type->getAtomicTypes();
|
2018-10-29 17:15:36 +01:00
|
|
|
|
|
|
|
foreach ($types as $type) {
|
2021-12-13 04:45:57 +01:00
|
|
|
if ($type instanceof TString
|
|
|
|
&& !$type instanceof TLiteralString
|
2018-10-29 17:15:36 +01:00
|
|
|
) {
|
2022-12-17 05:00:34 +01:00
|
|
|
IssueBuffer::maybeAdd(
|
2019-04-26 00:02:19 +02:00
|
|
|
new ArgumentTypeCoercion(
|
2019-11-25 17:44:54 +01:00
|
|
|
'Echo requires an unescaped string, ' . $expr_type . ' provided',
|
2019-04-26 00:02:19 +02:00
|
|
|
new CodeLocation($statements_source, $expr),
|
2022-12-18 17:15:15 +01:00
|
|
|
'echo',
|
2018-10-29 17:15:36 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$statements_source->getSuppressedIssues(),
|
2022-12-17 05:00:34 +01:00
|
|
|
);
|
2018-10-29 17:15:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 21:02:52 +02:00
|
|
|
|
|
|
|
return null;
|
2018-10-29 17:15:36 +01:00
|
|
|
}
|
|
|
|
}
|