-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlugin.php
More file actions
66 lines (58 loc) · 2.47 KB
/
Copy pathPlugin.php
File metadata and controls
66 lines (58 loc) · 2.47 KB
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
<?php
namespace Andyg0808\PsalmCallgraph;
use Psalm\Plugin\EventHandler\AfterFunctionCallAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterFunctionCallAnalysisEvent;
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
use Psalm\Plugin\PluginEntryPointInterface;
use Psalm\Plugin\RegistrationInterface;
use Psalm\Context;
use SimpleXMLElement;
class Plugin implements PluginEntryPointInterface, AfterFunctionCallAnalysisInterface, AfterMethodCallAnalysisInterface
{
public const FILENAME = "./callers.csv";
/** @return void */
public function __invoke(RegistrationInterface $psalm, ?SimpleXMLElement $config = null): void
{
$file = fopen("./callers.csv", "w");
fclose($file);
// This is plugin entry point. You can initialize things you need here,
// and hook them into psalm using RegistrationInterface
//
// Here's some examples:
// 1. Add a stub file
// ```php
// $psalm->addStubFile(__DIR__ . '/stubs/YourStub.php');
// ```
foreach ($this->getStubFiles() as $file) {
$psalm->addStubFile($file);
}
// Psalm allows arbitrary content to be stored under you plugin entry in
// its config file, psalm.xml, so your plugin users can put some configuration
// values there. They will be provided to your plugin entry point in $config
// parameter, as a SimpleXmlElement object. If there's no configuration present,
// null will be passed instead.
}
/** @return list<string> */
private function getStubFiles(): array
{
return glob(__DIR__ . '/stubs/*.phpstub') ?: [];
}
public static function afterFunctionCallAnalysis(AfterFunctionCallAnalysisEvent $event): void {
$file = fopen(self::FILENAME, "a");
$call = $event->getFunctionId();
$madeBy = self::getCaller($event->getContext());
fputcsv($file, [$madeBy, $call]);
fclose($file);
}
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void {
$file = fopen(self::FILENAME, "a");
$call = $event->getMethodId();
$madeBy = self::getCaller($event->getContext());
fputcsv($file, [$madeBy, $call]);
fclose($file);
}
private static function getCaller(Context $context): string {
return $context->calling_method_id ?: $context->calling_function_id ?: "";
}
}