2021-03-20 03:41:41 +01:00
|
|
|
<?php
|
2022-12-18 17:15:15 +01:00
|
|
|
|
2021-03-20 03:41:41 +01:00
|
|
|
namespace Psalm\Example\Plugin;
|
|
|
|
|
|
|
|
use PhpParser\Node\Expr\ArrayItem;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent;
|
|
|
|
use Psalm\Plugin\EventHandler\RemoveTaintsInterface;
|
|
|
|
|
2023-10-26 17:00:29 +02:00
|
|
|
final class SafeArrayKeyChecker implements RemoveTaintsInterface
|
2021-03-20 03:41:41 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Called to see what taints should be removed
|
|
|
|
*
|
|
|
|
* @return list<string>
|
|
|
|
*/
|
2022-12-18 17:15:15 +01:00
|
|
|
public static function removeTaints(AddRemoveTaintsEvent $event): array
|
|
|
|
{
|
2021-03-20 03:41:41 +01:00
|
|
|
$item = $event->getExpr();
|
|
|
|
$statements_analyzer = $event->getStatementsSource();
|
|
|
|
if (!($item instanceof ArrayItem) || !($statements_analyzer instanceof StatementsAnalyzer)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$item_key_value = '';
|
|
|
|
if ($item->key) {
|
|
|
|
if ($item_key_type = $statements_analyzer->node_data->getType($item->key)) {
|
|
|
|
$key_type = $item_key_type;
|
|
|
|
|
|
|
|
if ($key_type->isSingleStringLiteral()) {
|
|
|
|
$item_key_value = $key_type->getSingleStringLiteral()->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($item_key_value === 'safe_key') {
|
|
|
|
return ['html'];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|