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

135 lines
2.9 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;
use Psalm\Codebase;
2018-01-21 18:44:46 +01:00
use Psalm\FileSource;
use Psalm\Storage\FileStorage;
use Psalm\Internal\Visitor\ReflectorVisitor;
2018-01-21 18:44:46 +01:00
/**
* @internal
*/
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;
/**
* @param string $file_path
* @param string $file_name
* @param bool $will_analyze
*/
public function __construct($file_path, $file_name, $will_analyze)
{
$this->file_path = $file_path;
$this->file_name = $file_name;
$this->will_analyze = $will_analyze;
}
/**
* @param array<mixed, PhpParser\Node> $stmts
2018-02-19 06:27:39 +01:00
* @param bool $storage_from_cache
* @param bool $debug_output
2018-01-21 18:44:46 +01:00
*
* @return void
*/
public function scan(
Codebase $codebase,
FileStorage $file_storage,
$storage_from_cache = false,
$debug_output = false
) {
if ((!$this->will_analyze || $file_storage->deep_scan)
&& $storage_from_cache
&& !$file_storage->has_trait
&& !$file_storage->has_docblock_issues
&& !$codebase->register_stub_files
) {
2018-02-19 06:27:39 +01:00
return;
}
$stmts = $codebase->statements_provider->getStatementsForFile(
$file_storage->file_path,
$debug_output
);
foreach ($stmts as $stmt) {
if (!$stmt instanceof PhpParser\Node\Stmt\ClassLike
&& !$stmt instanceof PhpParser\Node\Stmt\Function_
&& !$stmt instanceof PhpParser\Node\Expr\Include_
) {
$file_storage->has_extra_statements = true;
break;
}
}
if ($debug_output) {
if ($this->will_analyze) {
echo 'Deep scanning ' . $file_storage->file_path . "\n";
} else {
echo 'Scanning ' . $file_storage->file_path . "\n";
}
}
2018-01-21 18:44:46 +01:00
$traverser = new NodeTraverser();
$traverser->addVisitor(new ReflectorVisitor($codebase, $file_storage, $this));
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
}
/**
* @return string
*/
public function getFilePath()
{
return $this->file_path;
}
/**
* @return string
*/
public function getFileName()
{
return $this->file_name;
}
/**
* @return string
*/
public function getRootFilePath()
2018-01-21 18:44:46 +01:00
{
return $this->file_path;
}
/**
* @return string
*/
public function getRootFileName()
2018-01-21 18:44:46 +01:00
{
return $this->file_name;
}
/**
* @return \Psalm\Aliases
*/
public function getAliases()
{
return new \Psalm\Aliases();
}
2018-01-21 18:44:46 +01:00
}