Skip to content

Commit

Permalink
Cleanup: Use TaintKind constants instead of string values
Browse files Browse the repository at this point in the history
  • Loading branch information
ohader committed Feb 24, 2024
1 parent eeffee7 commit 288d80f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Psalm\Type\Atomic\TNonEmptyArray;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TString;
use Psalm\Type\TaintKind;
use Psalm\Type\Union;
use UnexpectedValueException;

Expand Down Expand Up @@ -646,9 +647,9 @@ private static function taintReturnType(
$pattern = substr($pattern, 2, -1);

if (self::simpleExclusion($pattern, $first_arg_value[0])) {
$removed_taints[] = 'html';
$removed_taints[] = 'has_quotes';
$removed_taints[] = 'sql';
$removed_taints[] = TaintKind::INPUT_HTML;
$removed_taints[] = TaintKind::INPUT_HAS_QUOTES;
$removed_taints[] = TaintKind::INPUT_SQL;
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/Psalm/Internal/Provider/AddRemoveTaints/HtmlFunctionTainter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psalm\Plugin\EventHandler\AddTaintsInterface;
use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent;
use Psalm\Plugin\EventHandler\RemoveTaintsInterface;
use Psalm\Type\TaintKind;

use function count;
use function strtolower;
Expand Down Expand Up @@ -47,24 +48,24 @@ public static function addTaints(AddRemoveTaintsEvent $event): array

if ($second_arg === null) {
if ($statements_analyzer->getCodebase()->analysis_php_version_id >= 8_01_00) {
return ['html', 'has_quotes'];
return [TaintKind::INPUT_HTML, TaintKind::INPUT_HAS_QUOTES];
}
return ['html'];
return [TaintKind::INPUT_HTML];
}

$second_arg_value = $statements_analyzer->node_data->getType($second_arg);

if (!$second_arg_value || !$second_arg_value->isSingleIntLiteral()) {
return ['html'];
return [TaintKind::INPUT_HTML];
}

$second_arg_value = $second_arg_value->getSingleIntLiteral()->value;

if (($second_arg_value & ENT_QUOTES) === ENT_QUOTES) {
return ['html', 'has_quotes'];
return [TaintKind::INPUT_HTML, TaintKind::INPUT_HAS_QUOTES];
}

return ['html'];
return [TaintKind::INPUT_HTML];
}

return [];
Expand Down Expand Up @@ -99,24 +100,24 @@ public static function removeTaints(AddRemoveTaintsEvent $event): array

if ($second_arg === null) {
if ($statements_analyzer->getCodebase()->analysis_php_version_id >= 8_01_00) {
return ['html', 'has_quotes'];
return [TaintKind::INPUT_HTML, TaintKind::INPUT_HAS_QUOTES];
}
return ['html'];
return [TaintKind::INPUT_HTML];
}

$second_arg_value = $statements_analyzer->node_data->getType($second_arg);

if (!$second_arg_value || !$second_arg_value->isSingleIntLiteral()) {
return ['html'];
return [TaintKind::INPUT_HTML];
}

$second_arg_value = $second_arg_value->getSingleIntLiteral()->value;

if (($second_arg_value & ENT_QUOTES) === ENT_QUOTES) {
return ['html', 'has_quotes'];
return [TaintKind::INPUT_HTML, TaintKind::INPUT_HAS_QUOTES];
}

return ['html'];
return [TaintKind::INPUT_HTML];
}

return [];
Expand Down

0 comments on commit 288d80f

Please sign in to comment.