mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Allow functions to be included too
This commit is contained in:
parent
03db293ed9
commit
46dbe83cbd
@ -32,12 +32,12 @@ class FileChecker extends SourceChecker implements StatementsSource
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $include_file_name;
|
||||
protected $actual_file_name;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $include_file_path;
|
||||
protected $actual_file_path;
|
||||
|
||||
/**
|
||||
* @var array<string, string>
|
||||
@ -978,30 +978,17 @@ class FileChecker extends SourceChecker implements StatementsSource
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
*/
|
||||
public function getIncludeFileName()
|
||||
{
|
||||
return $this->include_file_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
*/
|
||||
public function getIncludeFilePath()
|
||||
{
|
||||
return $this->include_file_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $file_name
|
||||
* @param string|null $file_path
|
||||
* @param string $file_name
|
||||
* @param string $file_path
|
||||
* @return void
|
||||
*/
|
||||
public function setIncludeFileName($file_name, $file_path)
|
||||
public function setFileName($file_name, $file_path)
|
||||
{
|
||||
$this->include_file_name = $file_name;
|
||||
$this->include_file_path = $file_path;
|
||||
$this->actual_file_name = $this->file_name;
|
||||
$this->actual_file_path = $this->file_path;
|
||||
|
||||
$this->file_name = $file_name;
|
||||
$this->file_path = $file_path;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1009,7 +996,7 @@ class FileChecker extends SourceChecker implements StatementsSource
|
||||
*/
|
||||
public function getCheckedFileName()
|
||||
{
|
||||
return $this->include_file_name ?: $this->file_name;
|
||||
return $this->actual_file_name ?: $this->file_name;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1017,7 +1004,7 @@ class FileChecker extends SourceChecker implements StatementsSource
|
||||
*/
|
||||
public function getCheckedFilePath()
|
||||
{
|
||||
return $this->include_file_path ?: $this->file_path;
|
||||
return $this->actual_file_path ?: $this->file_path;
|
||||
}
|
||||
|
||||
public function getSuppressedIssues()
|
||||
|
@ -142,42 +142,18 @@ abstract class SourceChecker implements StatementsSource
|
||||
return $this->source->getFilePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
*/
|
||||
public function getIncludeFileName()
|
||||
{
|
||||
if ($this->source === null) {
|
||||
throw new \UnexpectedValueException('$source cannot be null');
|
||||
}
|
||||
|
||||
return $this->source->getIncludeFileName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
*/
|
||||
public function getIncludeFilePath()
|
||||
{
|
||||
if ($this->source === null) {
|
||||
throw new \UnexpectedValueException('$source cannot be null');
|
||||
}
|
||||
|
||||
return $this->source->getIncludeFilePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $file_name
|
||||
* @param string|null $file_path
|
||||
* @return void
|
||||
*/
|
||||
public function setIncludeFileName($file_name, $file_path)
|
||||
public function setFileName($file_name, $file_path)
|
||||
{
|
||||
if ($this->source === null) {
|
||||
throw new \UnexpectedValueException('$source cannot be null');
|
||||
}
|
||||
|
||||
$this->source->setIncludeFileName($file_name, $file_path);
|
||||
$this->source->setFileName($file_name, $file_path);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -626,7 +626,7 @@ class StatementsChecker extends SourceChecker implements StatementsSource
|
||||
$path_to_file = getcwd() . '/' . $path_to_file;
|
||||
}
|
||||
} else {
|
||||
$path_to_file = self::getPathTo($stmt->expr, $this->getIncludeFileName() ?: $this->getFileName());
|
||||
$path_to_file = self::getPathTo($stmt->expr, $this->getFileName());
|
||||
}
|
||||
|
||||
if ($path_to_file) {
|
||||
@ -654,7 +654,12 @@ class StatementsChecker extends SourceChecker implements StatementsSource
|
||||
|
||||
if ($this->getFileChecker()->fileExists($path_to_file)) {
|
||||
$include_stmts = FileChecker::getStatementsForFile($path_to_file);
|
||||
$include_file_checker = new FileChecker($path_to_file, $current_file_checker->project_checker);
|
||||
$include_file_checker = new FileChecker(
|
||||
$path_to_file,
|
||||
$current_file_checker->project_checker,
|
||||
$include_stmts
|
||||
);
|
||||
$include_file_checker->setFileName($this->getFileName(), $this->getFilePath());
|
||||
$include_file_checker->visit($context);
|
||||
$include_file_checker->analyze();
|
||||
return null;
|
||||
|
@ -65,16 +65,6 @@ interface StatementsSource
|
||||
*/
|
||||
public function getFilePath();
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getIncludeFileName();
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getIncludeFilePath();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@ -89,7 +79,7 @@ interface StatementsSource
|
||||
* @param string|null $file_name
|
||||
* @param string|null $file_path
|
||||
*/
|
||||
public function setIncludeFileName($file_name, $file_path);
|
||||
public function setFileName($file_name, $file_path);
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
|
@ -39,7 +39,7 @@ class IncludeTest extends PHPUnit_Framework_TestCase
|
||||
);
|
||||
|
||||
$file2_checker = new FileChecker(
|
||||
'file2.php',
|
||||
getcwd() . '/file2.php',
|
||||
$this->project_checker,
|
||||
self::$parser->parse('<?php
|
||||
require("file1.php");
|
||||
@ -79,7 +79,7 @@ class IncludeTest extends PHPUnit_Framework_TestCase
|
||||
);
|
||||
|
||||
$file2_checker = new FileChecker(
|
||||
'file3.php',
|
||||
getcwd() . '/file3.php',
|
||||
$this->project_checker,
|
||||
self::$parser->parse('<?php
|
||||
require("file2.php");
|
||||
@ -108,7 +108,7 @@ class IncludeTest extends PHPUnit_Framework_TestCase
|
||||
);
|
||||
|
||||
$file2_checker = new FileChecker(
|
||||
'file2.php',
|
||||
getcwd() . '/file2.php',
|
||||
$this->project_checker,
|
||||
self::$parser->parse('<?php
|
||||
require("file1.php");
|
||||
@ -123,4 +123,28 @@ class IncludeTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
$file2_checker->visitAndAnalyzeMethods();
|
||||
}
|
||||
|
||||
public function testRequireFunction()
|
||||
{
|
||||
$this->project_checker->registerFile(
|
||||
getcwd() . '/file1.php',
|
||||
'<?php
|
||||
function fooFoo() : void {
|
||||
|
||||
}
|
||||
'
|
||||
);
|
||||
|
||||
$file2_checker = new FileChecker(
|
||||
getcwd() . '/file2.php',
|
||||
$this->project_checker,
|
||||
self::$parser->parse('<?php
|
||||
require("file1.php");
|
||||
|
||||
fooFoo();
|
||||
')
|
||||
);
|
||||
|
||||
$file2_checker->visitAndAnalyzeMethods();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user