Skip to content

Commit

Permalink
Merge pull request #9803 from orklah/zefzef
Browse files Browse the repository at this point in the history
fix offset for type param changes
  • Loading branch information
orklah committed May 21, 2023
2 parents 22fd6fb + ed94de5 commit cb34901
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
21 changes: 14 additions & 7 deletions bin/update-property-map.php
Expand Up @@ -11,6 +11,13 @@
//
// 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 {
throw new ErrorException($str, 0, $num, $file, $line);
});
Expand All @@ -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);
}
}
Expand Down
Expand Up @@ -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,
];
}
}
Expand Down
48 changes: 48 additions & 0 deletions tests/FileManipulation/ParamTypeManipulationTest.php
Expand Up @@ -365,6 +365,54 @@ class B extends A {
'issues_to_fix' => ['MissingParamType'],
'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,
],
];
}
}

0 comments on commit cb34901

Please sign in to comment.