1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-29 20:28:59 +01:00

Fix #1417 - store root file path when scanning for manipulations

This commit is contained in:
Matthew Brown 2019-03-02 15:07:26 -05:00
parent 4ba15ec18d
commit efbcf7dc5b
6 changed files with 104 additions and 10 deletions

View File

@ -96,12 +96,22 @@ class TemplateAnalyzer extends Psalm\Internal\Analyzer\FileAnalyzer
$constructor_id = $class . '::__construct';
$this->project_analyzer->getMethodMutations($constructor_id, $this_context);
$this->project_analyzer->getMethodMutations(
$constructor_id,
$this_context,
$this->getRootFilePath(),
$this->getRootFileName()
);
$this_context->vars_in_scope['$this'] = new Type\Union([new Type\Atomic\TNamedObject($class)]);
// check the actual method
$this->project_analyzer->getMethodMutations($method_id, $this_context);
$this->project_analyzer->getMethodMutations(
$method_id,
$this_context,
$this->getRootFilePath(),
$this->getRootFileName()
);
$view_context = new Context();
$view_context->self = self::VIEW_CLASS;

View File

@ -277,7 +277,12 @@ class FileAnalyzer extends SourceAnalyzer implements StatementsSource
$class_analyzer_to_examine = $this->class_analyzers_to_analyze[strtolower($fq_class_name)];
} else {
if (!$from_project_analyzer) {
$this->project_analyzer->getMethodMutations($method_id, $this_context);
$this->project_analyzer->getMethodMutations(
$method_id,
$this_context,
$this->getRootFilePath(),
$this->getRootFileName()
);
}
return;

View File

@ -810,12 +810,18 @@ class ProjectAnalyzer
*
* @return void
*/
public function getMethodMutations($original_method_id, Context $this_context)
{
public function getMethodMutations(
$original_method_id,
Context $this_context,
string $root_file_path,
string $root_file_name
) {
list($fq_class_name) = explode('::', $original_method_id);
$file_analyzer = $this->getFileAnalyzerForClassLike($fq_class_name);
$file_analyzer->setRootFilePath($root_file_path, $root_file_name);
$appearing_method_id = $this->codebase->methods->getAppearingMethodId($original_method_id);
if (!$appearing_method_id) {

View File

@ -88,7 +88,12 @@ class CallAnalyzer
$context->initialized_methods[$method_id] = true;
}
$project_analyzer->getMethodMutations($method_id, $context);
$project_analyzer->getMethodMutations(
$method_id,
$context,
$source->getRootFilePath(),
$source->getRootFileName()
);
}
} elseif ($context->collect_initializations &&
$context->self &&
@ -186,7 +191,12 @@ class CallAnalyzer
$old_self = $context->self;
$context->self = $declaring_fq_class_name;
$project_analyzer->getMethodMutations($declaring_method_id, $context);
$project_analyzer->getMethodMutations(
$declaring_method_id,
$context,
$source->getRootFilePath(),
$source->getRootFileName()
);
$context->self = $old_self;
}

View File

@ -491,6 +491,54 @@ class IncludeTest extends TestCase
'hoist_constants' => false,
'error_levels' => ['DuplicateClass', 'MissingPropertyType'],
],
'functionsDefined' => [
'files' => [
getcwd() . DIRECTORY_SEPARATOR . 'index.php' => '<?php
include "func.php";
include "Base.php";
include "Child.php";',
getcwd() . DIRECTORY_SEPARATOR . 'func.php' => '<?php
namespace ns;
function func(): void {}
define("ns\\cons", 0);
cons;',
getcwd() . DIRECTORY_SEPARATOR . 'Base.php' => '<?php
namespace ns;
func();
cons;
class Base {
public function __construct() {}
}',
getcwd() . DIRECTORY_SEPARATOR . 'Child.php' => '<?php
namespace ns;
func();
cons;
class Child extends Base {
/**
* @var int
*/
public $x;
public function __construct() {
parent::__construct();
$this->x = 5;
}
}',
],
'files_to_check' => [
getcwd() . DIRECTORY_SEPARATOR . 'index.php',
],
],
];
}

View File

@ -93,7 +93,12 @@ class MethodMutationTest extends TestCase
$this->project_analyzer->getCodebase()->scanFiles();
$method_context = new Context();
$method_context->collect_mutations = true;
$this->project_analyzer->getMethodMutations('FooController::barBar', $method_context);
$this->project_analyzer->getMethodMutations(
'FooController::barBar',
$method_context,
'somefile.php',
'somefile.php'
);
$this->assertSame('UserViewData', (string)$method_context->vars_in_scope['$this->user_viewdata']);
$this->assertSame('string', (string)$method_context->vars_in_scope['$this->user_viewdata->name']);
@ -131,7 +136,12 @@ class MethodMutationTest extends TestCase
$this->project_analyzer->getCodebase()->scanFiles();
$method_context = new Context();
$method_context->collect_mutations = true;
$this->project_analyzer->getMethodMutations('FooController::__construct', $method_context);
$this->project_analyzer->getMethodMutations(
'FooController::__construct',
$method_context,
'somefile.php',
'somefile.php'
);
$this->assertSame('Foo', (string)$method_context->vars_in_scope['$this->foo']);
}
@ -168,7 +178,12 @@ class MethodMutationTest extends TestCase
$this->project_analyzer->getCodebase()->scanFiles();
$method_context = new Context();
$method_context->collect_mutations = true;
$this->project_analyzer->getMethodMutations('FooController::__construct', $method_context);
$this->project_analyzer->getMethodMutations(
'FooController::__construct',
$method_context,
'somefile.php',
'somefile.php'
);
$this->assertSame('Foo', (string)$method_context->vars_in_scope['$this->foo']);
}