From ed94de5146cd5f05e4e75ab180256c9b746511ac Mon Sep 17 00:00:00 2001 From: orklah Date: Sun, 21 May 2023 16:11:25 +0200 Subject: [PATCH] fix offset for type param changes --- bin/update-property-map.php | 21 +++++--- .../FunctionDocblockManipulator.php | 2 +- .../ParamTypeManipulationTest.php | 48 +++++++++++++++++++ 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/bin/update-property-map.php b/bin/update-property-map.php index 1fefe51cfde..001a64022b2 100755 --- a/bin/update-property-map.php +++ b/bin/update-property-map.php @@ -11,6 +11,13 @@ // // What we are currently missing is properly parsing of 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 { throw new ErrorException($str, 0, $num, $file, $line); }); @@ -22,22 +29,22 @@ } } -$lexer = new PhpParser\Lexer\Emulative(); -$parser = (new PhpParser\ParserFactory)->create( - PhpParser\ParserFactory::PREFER_PHP7, +$lexer = new Emulative(); +$parser = (new ParserFactory)->create( + ParserFactory::PREFER_PHP7, $lexer, ); -$traverser = new PhpParser\NodeTraverser(); -$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver); +$traverser = new NodeTraverser(); +$traverser->addVisitor(new NameResolver); function extractClassesFromStatements(array $statements): array { $classes = []; foreach ($statements as $statement) { - if ($statement instanceof PhpParser\Node\Stmt\Class_) { + if ($statement instanceof Class_) { $classes[strtolower($statement->namespacedName->toString())] = true; } - if ($statement instanceof PhpParser\Node\Stmt\Namespace_) { + if ($statement instanceof Namespace_) { $classes += extractClassesFromStatements($statement->stmts); } } diff --git a/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php b/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php index 9b1762dba8b..b04952c0581 100644 --- a/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php +++ b/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php @@ -137,7 +137,7 @@ private function __construct(string $file_path, FunctionLike $stmt, ProjectAnaly if ($param->type) { $this->param_typehint_offsets[$param->var->name] = [ (int) $param->type->getAttribute('startFilePos'), - (int) $param->type->getAttribute('endFilePos'), + (int) $param->type->getAttribute('endFilePos') + 1, ]; } } diff --git a/tests/FileManipulation/ParamTypeManipulationTest.php b/tests/FileManipulation/ParamTypeManipulationTest.php index 78bfb8f18c2..bf182d5c482 100644 --- a/tests/FileManipulation/ParamTypeManipulationTest.php +++ b/tests/FileManipulation/ParamTypeManipulationTest.php @@ -365,6 +365,54 @@ class B extends A { 'issues_to_fix' => ['MissingParamType'], 'safe_types' => true, ], + 'ChangingTypeOfExplicitMixedParam' => [ + 'input' => 'setValue([1,2,3,4]); + + } + ', + 'output' => 'setValue([1,2,3,4]); + + } + ', + 'php_version' => '8.0', + 'issues_to_fix' => ['MissingParamType'], + 'safe_types' => false, + ], ]; } }