forked from vimeo/psalm
/
FileBasedPluginAdapter.php
68 lines (51 loc) · 1.75 KB
/
FileBasedPluginAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace Psalm;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Scanner\FileScanner;
use Psalm\Plugin\RegistrationInterface;
use SimpleXMLElement;
use UnexpectedValueException;
use function assert;
use function class_exists;
use function reset;
use function str_replace;
use const DIRECTORY_SEPARATOR;
class FileBasedPluginAdapter implements Plugin\PluginEntryPointInterface
{
/** @var string */
private $path;
/** @var Codebase */
private $codebase;
/** @var Config */
private $config;
public function __construct(string $path, Config $config, Codebase $codebase)
{
if (!$path) {
throw new UnexpectedValueException('$path cannot be empty');
}
$this->path = $path;
$this->config = $config;
$this->codebase = $codebase;
}
public function __invoke(RegistrationInterface $registration, ?SimpleXMLElement $config = null): void
{
$fq_class_name = $this->getPluginClassForPath($this->path);
/** @psalm-suppress UnresolvableInclude */
require_once($this->path);
assert(class_exists($fq_class_name));
$registration->registerHooksFromClass($fq_class_name);
}
private function getPluginClassForPath(string $path): string
{
$codebase = $this->codebase;
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
$file_storage = $codebase->createFileStorageForPath($path);
$file_to_scan = new FileScanner($path, $this->config->shortenFileName($path), true);
$file_to_scan->scan(
$codebase,
$file_storage
);
$declared_classes = ClassLikeAnalyzer::getClassesForFile($codebase, $path);
return reset($declared_classes);
}
}