2018-01-21 18:44:46 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Scanner;
|
2018-01-21 18:44:46 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use PhpParser\NodeTraverser;
|
2018-01-21 19:38:51 +01:00
|
|
|
use Psalm\Codebase;
|
2018-01-21 18:44:46 +01:00
|
|
|
use Psalm\FileSource;
|
2019-05-30 16:30:41 +02:00
|
|
|
use Psalm\Progress\Progress;
|
|
|
|
use Psalm\Progress\VoidProgress;
|
2018-01-21 18:44:46 +01:00
|
|
|
use Psalm\Storage\FileStorage;
|
2020-03-15 04:54:42 +01:00
|
|
|
use Psalm\Internal\PhpVisitor\ReflectorVisitor;
|
2018-01-21 18:44:46 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
2020-08-06 16:18:55 +02:00
|
|
|
* @psalm-consistent-constructor
|
2018-12-02 00:37:49 +01:00
|
|
|
*/
|
2018-01-21 18:44:46 +01:00
|
|
|
class FileScanner implements FileSource
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $file_path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $file_name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $will_analyze;
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function __construct(string $file_path, string $file_name, bool $will_analyze)
|
2018-01-21 18:44:46 +01:00
|
|
|
{
|
|
|
|
$this->file_path = $file_path;
|
|
|
|
$this->file_name = $file_name;
|
|
|
|
$this->will_analyze = $will_analyze;
|
|
|
|
}
|
|
|
|
|
2018-02-19 17:53:30 +01:00
|
|
|
public function scan(
|
|
|
|
Codebase $codebase,
|
|
|
|
FileStorage $file_storage,
|
2020-09-07 01:36:47 +02:00
|
|
|
bool $storage_from_cache = false,
|
|
|
|
?Progress $progress = null
|
2020-10-12 21:02:52 +02:00
|
|
|
): void {
|
2019-05-30 16:30:41 +02:00
|
|
|
if ($progress === null) {
|
|
|
|
$progress = new VoidProgress();
|
|
|
|
}
|
|
|
|
|
2018-02-23 04:52:22 +01:00
|
|
|
if ((!$this->will_analyze || $file_storage->deep_scan)
|
|
|
|
&& $storage_from_cache
|
2018-06-30 21:29:37 +02:00
|
|
|
&& !$codebase->register_stub_files
|
2018-02-23 04:52:22 +01:00
|
|
|
) {
|
2018-02-19 06:27:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-19 17:53:30 +01:00
|
|
|
$stmts = $codebase->statements_provider->getStatementsForFile(
|
|
|
|
$file_storage->file_path,
|
2020-08-09 22:23:43 +02:00
|
|
|
$codebase->php_major_version . '.' . $codebase->php_minor_version,
|
2019-05-30 16:30:41 +02:00
|
|
|
$progress
|
2018-02-19 17:53:30 +01:00
|
|
|
);
|
|
|
|
|
2018-05-23 05:38:27 +02:00
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if (!$stmt instanceof PhpParser\Node\Stmt\ClassLike
|
|
|
|
&& !$stmt instanceof PhpParser\Node\Stmt\Function_
|
2019-03-16 17:34:48 +01:00
|
|
|
&& !($stmt instanceof PhpParser\Node\Stmt\Expression
|
|
|
|
&& $stmt->expr instanceof PhpParser\Node\Expr\Include_)
|
2018-05-23 05:38:27 +02:00
|
|
|
) {
|
|
|
|
$file_storage->has_extra_statements = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 16:30:41 +02:00
|
|
|
if ($this->will_analyze) {
|
|
|
|
$progress->debug('Deep scanning ' . $file_storage->file_path . "\n");
|
|
|
|
} else {
|
|
|
|
$progress->debug('Scanning ' . $file_storage->file_path . "\n");
|
2018-02-19 17:53:30 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 18:44:46 +01:00
|
|
|
$traverser = new NodeTraverser();
|
2019-02-07 18:25:57 +01:00
|
|
|
$traverser->addVisitor(
|
|
|
|
new ReflectorVisitor($codebase, $file_storage, $this)
|
|
|
|
);
|
2019-06-30 03:32:26 +02:00
|
|
|
|
2018-01-21 18:44:46 +01:00
|
|
|
$traverser->traverse($stmts);
|
2018-02-21 19:54:11 +01:00
|
|
|
|
|
|
|
$file_storage->deep_scan = $this->will_analyze;
|
2018-01-21 18:44:46 +01:00
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getFilePath(): string
|
2018-01-21 18:44:46 +01:00
|
|
|
{
|
|
|
|
return $this->file_path;
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getFileName(): string
|
2018-01-21 18:44:46 +01:00
|
|
|
{
|
|
|
|
return $this->file_name;
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getRootFilePath(): string
|
2018-01-21 18:44:46 +01:00
|
|
|
{
|
|
|
|
return $this->file_path;
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getRootFileName(): string
|
2018-01-21 18:44:46 +01:00
|
|
|
{
|
|
|
|
return $this->file_name;
|
|
|
|
}
|
2018-02-23 21:39:33 +01:00
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getAliases(): \Psalm\Aliases
|
2018-02-23 21:39:33 +01:00
|
|
|
{
|
|
|
|
return new \Psalm\Aliases();
|
|
|
|
}
|
2018-01-21 18:44:46 +01:00
|
|
|
}
|