1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

fix offset for type param changes

This commit is contained in:
orklah 2023-05-21 16:11:25 +02:00
parent 22fd6fb9db
commit ed94de5146
3 changed files with 63 additions and 8 deletions

View File

@ -11,6 +11,13 @@ declare(strict_types=1);
// //
// What we are currently missing is properly parsing of <xi:include> directives. // What we are currently missing is properly parsing of <xi:include> directives.
use PhpParser\Lexer\Emulative;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
set_error_handler(function ($num, $str, $file, $line, $context = null): void { set_error_handler(function ($num, $str, $file, $line, $context = null): void {
throw new ErrorException($str, 0, $num, $file, $line); throw new ErrorException($str, 0, $num, $file, $line);
}); });
@ -22,22 +29,22 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php
} }
} }
$lexer = new PhpParser\Lexer\Emulative(); $lexer = new Emulative();
$parser = (new PhpParser\ParserFactory)->create( $parser = (new ParserFactory)->create(
PhpParser\ParserFactory::PREFER_PHP7, ParserFactory::PREFER_PHP7,
$lexer, $lexer,
); );
$traverser = new PhpParser\NodeTraverser(); $traverser = new NodeTraverser();
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver); $traverser->addVisitor(new NameResolver);
function extractClassesFromStatements(array $statements): array function extractClassesFromStatements(array $statements): array
{ {
$classes = []; $classes = [];
foreach ($statements as $statement) { foreach ($statements as $statement) {
if ($statement instanceof PhpParser\Node\Stmt\Class_) { if ($statement instanceof Class_) {
$classes[strtolower($statement->namespacedName->toString())] = true; $classes[strtolower($statement->namespacedName->toString())] = true;
} }
if ($statement instanceof PhpParser\Node\Stmt\Namespace_) { if ($statement instanceof Namespace_) {
$classes += extractClassesFromStatements($statement->stmts); $classes += extractClassesFromStatements($statement->stmts);
} }
} }

View File

@ -137,7 +137,7 @@ class FunctionDocblockManipulator
if ($param->type) { if ($param->type) {
$this->param_typehint_offsets[$param->var->name] = [ $this->param_typehint_offsets[$param->var->name] = [
(int) $param->type->getAttribute('startFilePos'), (int) $param->type->getAttribute('startFilePos'),
(int) $param->type->getAttribute('endFilePos'), (int) $param->type->getAttribute('endFilePos') + 1,
]; ];
} }
} }

View File

@ -365,6 +365,54 @@ class ParamTypeManipulationTest extends FileManipulationTestCase
'issues_to_fix' => ['MissingParamType'], 'issues_to_fix' => ['MissingParamType'],
'safe_types' => true, 'safe_types' => true,
], ],
'ChangingTypeOfExplicitMixedParam' => [
'input' => '<?php
class ConfigContainer
{
public function setValue(mixed $value): void
{
}
}
function foo(){
$config = new ConfigContainer();
$config->setValue([1,2,3,4]);
}
',
'output' => '<?php
class ConfigContainer
{
/**
* @param int[] $value
*
* @psalm-param list{1, 2, 3, 4} $value
*/
public function setValue(array $value): void
{
}
}
function foo(){
$config = new ConfigContainer();
$config->setValue([1,2,3,4]);
}
',
'php_version' => '8.0',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => false,
],
]; ];
} }
} }