1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 10:17:33 +01:00
psalm/src/Psalm/Internal/Scanner/FileScanner.php

118 lines
2.7 KiB
PHP
Raw Normal View History

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;
2021-12-03 20:11:20 +01:00
use Psalm\Aliases;
use Psalm\Codebase;
2018-01-21 18:44:46 +01:00
use Psalm\FileSource;
2021-06-08 04:55:21 +02:00
use Psalm\Internal\PhpVisitor\ReflectorVisitor;
use Psalm\Progress\Progress;
use Psalm\Progress\VoidProgress;
2018-01-21 18:44:46 +01:00
use Psalm\Storage\FileStorage;
/**
* @internal
* @psalm-consistent-constructor
*/
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;
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;
}
public function scan(
Codebase $codebase,
FileStorage $file_storage,
bool $storage_from_cache = false,
?Progress $progress = null
): void {
if ($progress === null) {
$progress = new VoidProgress();
}
if ((!$this->will_analyze || $file_storage->deep_scan)
&& $storage_from_cache
&& !$codebase->register_stub_files
) {
2018-02-19 06:27:39 +01:00
return;
}
$stmts = $codebase->statements_provider->getStatementsForFile(
$file_storage->file_path,
$codebase->analysis_php_version_id,
$progress
);
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_)
) {
$file_storage->has_extra_statements = true;
break;
}
}
if ($this->will_analyze) {
$progress->debug('Deep scanning ' . $file_storage->file_path . "\n");
} else {
$progress->debug('Scanning ' . $file_storage->file_path . "\n");
}
2018-01-21 18:44:46 +01:00
$traverser = new NodeTraverser();
$traverser->addVisitor(
2020-10-24 01:53:04 +02:00
new ReflectorVisitor($codebase, $this, $file_storage)
);
2018-01-21 18:44:46 +01:00
$traverser->traverse($stmts);
$file_storage->deep_scan = $this->will_analyze;
2018-01-21 18:44:46 +01:00
}
public function getFilePath(): string
2018-01-21 18:44:46 +01:00
{
return $this->file_path;
}
public function getFileName(): string
2018-01-21 18:44:46 +01:00
{
return $this->file_name;
}
public function getRootFilePath(): string
2018-01-21 18:44:46 +01:00
{
return $this->file_path;
}
public function getRootFileName(): string
2018-01-21 18:44:46 +01:00
{
return $this->file_name;
}
2021-12-03 20:11:20 +01:00
public function getAliases(): Aliases
{
2021-12-03 20:11:20 +01:00
return new Aliases();
}
2018-01-21 18:44:46 +01:00
}