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;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Codebase;
|
2018-10-29 17:15:36 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
2018-11-12 16:46:55 +01:00
|
|
|
use Psalm\FileManipulation;
|
2018-10-29 17:15:36 +01:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Issue\TypeCoercion;
|
2018-11-12 16:11:08 +01:00
|
|
|
use Psalm\Plugin\Hook\AfterStatementAnalysisInterface;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\StatementsSource;
|
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
|
|
|
*
|
|
|
|
* @param string[] $suppressed_issues
|
|
|
|
* @param FileManipulation[] $file_replacements
|
|
|
|
*
|
|
|
|
* @return null|false
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public static function afterStatementAnalysis(
|
|
|
|
PhpParser\Node\Stmt $stmt,
|
2018-10-29 17:15:36 +01:00
|
|
|
Context $context,
|
2018-11-06 03:57:36 +01:00
|
|
|
StatementsSource $statements_source,
|
|
|
|
Codebase $codebase,
|
2018-10-29 17:15:36 +01:00
|
|
|
array &$file_replacements = []
|
|
|
|
) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Echo_) {
|
|
|
|
foreach ($stmt->exprs as $expr) {
|
2018-12-08 19:18:55 +01:00
|
|
|
if (!isset($expr->inferredType) || $expr->inferredType->hasMixed()) {
|
2018-10-29 17:15:36 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new TypeCoercion(
|
|
|
|
'Echo requires an unescaped string, ' . $expr->inferredType . ' provided',
|
2018-11-06 03:57:36 +01:00
|
|
|
new CodeLocation($statements_source, $expr)
|
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->inferredType->getTypes();
|
|
|
|
|
|
|
|
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 TypeCoercion(
|
|
|
|
'Echo requires an unescaped string, ' . $expr->inferredType . ' provided',
|
2018-11-06 03:57:36 +01:00
|
|
|
new CodeLocation($statements_source, $expr)
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|