Fix regression. Removed bad fixer. GETPOST(... int) is not GETPOSTINT

This commit is contained in:
Laurent Destailleur
2024-03-04 17:49:43 +01:00
parent a2f5d6ad0b
commit 61dd78b95e
3 changed files with 0 additions and 222 deletions

View File

@@ -1,98 +0,0 @@
<?php
declare(strict_types=1);
/* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
*/
use ast\Node;
use Phan\CodeBase;
use Phan\Language\Context;
use Phan\AST\UnionTypeVisitor;
//use Phan\Language\Element\FunctionInterface;
use Phan\Language\UnionType;
use Phan\Language\Type;
use Phan\PluginV3;
use Phan\PluginV3\AnalyzeFunctionCallCapability;
use Phan\Language\Element\FunctionInterface;
use Phan\Config;
/*
* 'GetPostFixerPlugin' => [ '\\Foo::bar', '\\Baz::bing' ],
* 'plugins' => [
* __DIR__.'plugins/GetPostFixerPlugin.php',
* [...]
* ]
*/
/**
* Prints out call sites of given functions or methods.
*/
final class GetPostFixerPlugin extends PluginV3 implements AnalyzeFunctionCallCapability
{
/**
* @param CodeBase $code_base Code base
*
* @return array
*/
public function getAnalyzeFunctionCallClosures(CodeBase $code_base): array
{
static $function_call_closures;
if ($function_call_closures === null) {
$function_call_closures = [];
$self = $this;
$func = 'GETPOST';
$function_call_closures[$func]
= static function (CodeBase $code_base, Context $context, FunctionInterface $function, array $args, ?Node $node = null) use ($self, $func) {
self::handleCall($code_base, $context, $node, $function, $args, $func, $self);
};
}
return $function_call_closures;
}
/**
* @param CodeBase $code_base Code base
* @param Context $context Context
* @param ?Node $node Node
* @param FunctionInterface $function Visited function information
* @param array $args Arguments to the function
* @param string $func_to_analyze Name of the function to analyze (as we defined it)
* @param GetPostFixerPlugin $self This visitor
*
* @return void
*/
private static function handleCall(CodeBase $code_base, Context $context, ?Node $node, FunctionInterface $function, array $args, string $func_to_analyze, $self): void
{
$expr = $args[1] ?? null;
if ($expr === null) {
return;
}
try {
$expr_type = UnionTypeVisitor::unionTypeFromNode($code_base, $context, $expr, false);
} catch (Exception $_) {
return;
}
$expr_value = $expr_type->getRealUnionType()->asValueOrNullOrSelf();
if (!is_string($expr_value)) {
return;
}
if ($expr_value !== 'int') {
return;
}
$self->emitIssue(
$code_base,
$context,
'GetPostShouldBeGetPostInt',
'Convert {FUNCTION} to {FUNCTION}',
[(string) $function->getFQSEN(), "GETPOSTINT"]
);
}
}
if (Config::isIssueFixingPluginEnabled()) {
require_once __DIR__ . '/GetPostFixerPlugin/fixers.php';
}
return new GetPostFixerPlugin();

View File

@@ -1,123 +0,0 @@
<?php
/* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
*/
declare(strict_types=1);
use Microsoft\PhpParser\Node\Expression\CallExpression;
use Microsoft\PhpParser\Node\QualifiedName;
use Phan\AST\TolerantASTConverter\NodeUtils;
use Phan\CodeBase;
use Phan\IssueInstance;
use Phan\Library\FileCacheEntry;
use Phan\Plugin\Internal\IssueFixingPlugin\FileEdit;
use Phan\Plugin\Internal\IssueFixingPlugin\FileEditSet;
use Phan\Plugin\Internal\IssueFixingPlugin\IssueFixer;
use Microsoft\PhpParser\Node\Expression\ArgumentExpression;
use Microsoft\PhpParser\Node\DelimitedList\ArgumentExpressionList;
use Microsoft\PhpParser\Node\StringLiteral;
/**
* Implements --automatic-fix for GetPostFixerPlugin
*
* This is a prototype, there are various features it does not implement.
*/
call_user_func(static function (): void {
/**
* @param $code_base @unused-param
* @return ?FileEditSet a representation of the edit to make to replace a call to a function alias with a call to the original function
*/
$fix = static function (CodeBase $code_base, FileCacheEntry $contents, IssueInstance $instance): ?FileEditSet {
$line = $instance->getLine();
$new_name = (string) $instance->getTemplateParameters()[1];
if ($new_name !== "GETPOSTINT") {
return null;
}
$function_repr = (string) $instance->getTemplateParameters()[0];
if (!preg_match('{\\\\(\w+)}', $function_repr, $match)) {
return null;
}
$expected_name = $match[1];
$edits = [];
foreach ($contents->getNodesAtLine($line) as $node) {
if (!$node instanceof ArgumentExpressionList) {
continue;
}
$arguments = $node->children;
if (!in_array(count($arguments), [3, 5])) { // ',' included !
print "Arg Count is ".count($arguments)." - Skip $instance".PHP_EOL;
continue;
}
$is_actual_call = $node->parent instanceof CallExpression;
if (!$is_actual_call) {
print "Not actual call - Skip $instance".PHP_EOL;
continue;
}
$callable = $node->parent;
$callableExpression = $callable->callableExpression;
if ($callableExpression instanceof Microsoft\PhpParser\Node\QualifiedName) {
$actual_name = $callableExpression->getResolvedName();
} else {
print "Callable expression is ".get_class($callableExpression)."- Skip $instance".PHP_EOL;
continue;
}
if ((string) $actual_name !== (string) $expected_name) {
print "Name unexpected '$actual_name'!='$expected_name' - Skip $instance".PHP_EOL;
continue;
}
foreach ($arguments as $i => $argument) {
print "Type$i: ".get_class($argument).PHP_EOL;
}
$arg2 = $arguments[2];
if ($arg2 instanceof ArgumentExpression && $arg2->expression instanceof StringLiteral) {
// Get the string value of the StringLiteral
$stringValue = $arg2->expression->getStringContentsText();
} else {
print "Expression is not string ".get_class($arg2)."/".get_class($arg2->expression)."- Skip $instance".PHP_EOL;
continue;
}
print "Fixture elem on $line - $new_name - $function_repr - arg: $stringValue".PHP_EOL;
// Get the first argument (delimiter)
$delimiter = $arguments[1];
// Get the second argument
$secondArgument = $arguments[2];
// Get the start position of the delimiter
$arg_start_pos = $delimiter->getStartPosition();
// Get the end position of the second argument
$arg_end_pos = $secondArgument->getEndPosition();
// @phan-suppress-next-line PhanThrowTypeAbsentForCall
$start = $callableExpression->getStartPosition();
// @phan-suppress-next-line PhanThrowTypeAbsentForCall
$end = $callableExpression->getEndPosition();
// Remove second argument
$edits[] = new FileEdit($arg_start_pos, $arg_end_pos, "");
// Replace call with GETPOSTINT
$edits[] = new FileEdit($start, $end, (($file_contents[$start] ?? '') === '\\' ? '\\' : '') . $new_name);
}
if ($edits) {
return new FileEditSet($edits);
}
return null;
};
IssueFixer::registerFixerClosure(
'GetPostShouldBeGetPostInt',
$fix
);
});

View File

@@ -998,7 +998,6 @@ function GETPOST($paramname, $check = 'alphanohtml', $method = 0, $filter = null
*/
function GETPOSTINT($paramname, $method = 0)
{
// @phan-suppress-next-line GetPostShouldBeGetPostInt
return (int) GETPOST($paramname, 'int', $method, null, null, 0);
}