1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix bad namespacing

This commit is contained in:
Brown 2019-06-05 11:15:52 -04:00
parent 4dba1e694f
commit 3e15fa8bfe
4 changed files with 43 additions and 17 deletions

View File

@ -161,12 +161,16 @@ class ClassAnalyzer extends ClassLikeAnalyzer
$file_manipulations
);
} elseif (!$source_ns) {
$class_start_pos = (int) $class->getAttribute('startFilePos');
$first_statement_pos = $this->getFileAnalyzer()->getFirstStatementOffset();
if ($first_statement_pos === -1) {
$first_statement_pos = (int) $class->getAttribute('startFilePos');
}
$file_manipulations = [
new \Psalm\FileManipulation(
$class_start_pos,
$class_start_pos,
$first_statement_pos,
$first_statement_pos,
'namespace ' . $destination_ns . ';' . "\n\n",
true
)

View File

@ -89,6 +89,11 @@ class FileAnalyzer extends SourceAnalyzer implements StatementsSource
*/
public $codebase;
/**
* @var int
*/
private $first_statement_offset = -1;
/**
* @param string $file_path
* @param string $file_name
@ -153,6 +158,15 @@ class FileAnalyzer extends SourceAnalyzer implements StatementsSource
return;
}
if ($codebase->alter_code) {
foreach ($stmts as $stmt) {
if (!$stmt instanceof PhpParser\Node\Stmt\Declare_) {
$this->first_statement_offset = (int) $stmt->getAttribute('startFilePos');
break;
}
}
}
$statements_analyzer = new StatementsAnalyzer($this);
$leftover_stmts = $this->populateCheckers($stmts);
@ -581,4 +595,9 @@ class FileAnalyzer extends SourceAnalyzer implements StatementsSource
{
return $this->codebase;
}
public function getFirstStatementOffset() : int
{
return $this->first_statement_offset;
}
}

View File

@ -141,18 +141,13 @@ class ConstFetchAnalyzer
}
}
if ($codebase->methods_to_move
&& $context->calling_method_id
&& isset($codebase->methods_to_move[strtolower($context->calling_method_id)])
) {
$destination_method_id = $codebase->methods_to_move[strtolower($context->calling_method_id)];
$codebase->classlikes->airliftClassLikeReference(
if ($codebase->alter_code) {
$codebase->classlikes->handleClassLikeReferenceInMigration(
$codebase,
$statements_analyzer,
$stmt->class,
$fq_class_name,
explode('::', $destination_method_id)[0],
$statements_analyzer->getFilePath(),
(int) $stmt->class->getAttribute('startFilePos'),
(int) $stmt->class->getAttribute('endFilePos') + 1
$context->calling_method_id
);
}

View File

@ -266,6 +266,8 @@ class ClassMoveTest extends \Psalm\Tests\TestCase
],
'moveClassIntoNamespace' => [
'<?php
use Exception;
class A {
/** @var ?Exception */
public $x;
@ -278,6 +280,8 @@ class ClassMoveTest extends \Psalm\Tests\TestCase
$b->bar();
}
echo \A::class;
return new Exception("bad");
}
@ -286,19 +290,23 @@ class ClassMoveTest extends \Psalm\Tests\TestCase
'<?php
namespace Foo\Bar\Baz;
use Exception;
class B {
/** @var null|\Exception */
/** @var null|Exception */
public $x;
/**
* @param \ArrayObject<int, self> $a
*/
public function foo(\ArrayObject $a) : \Exception {
public function foo(\ArrayObject $a) : Exception {
foreach ($a as $b) {
$b->bar();
}
return new \Exception("bad");
echo self::class;
return new Exception("bad");
}
public function bar() : void {}