Debian Codegraph @

vendor/zircote/swagger-php/src/Analysers/TokenScanner.php

raw
1 <?php declare(strict_types=1);
2
3 /**
4 * @license Apache 2.0
5 */
6
7 namespace OpenApi\Analysers;
8
9 use PhpParser\Error;
10 use PhpParser\Node\Stmt\Class_;
11 use PhpParser\Node\Stmt\ClassLike;
12 use PhpParser\Node\Stmt\Enum_;
13 use PhpParser\Node\Stmt\Interface_;
14 use PhpParser\Node\Stmt\Namespace_;
15 use PhpParser\Node\Stmt\Trait_;
16 use PhpParser\Node\Stmt\Use_;
17 use PhpParser\ParserFactory;
18
19 /**
20 * High-level, PHP-token-based, scanner.
21 */
22 class TokenScanner
23 {
24 /**
25 * Scan a given file for all classes, interfaces, and traits.
26 *
27 * @return array{
28 * 'uses': array<string, class-string>,
29 * 'interfaces': list<class-string>,
30 * 'traits': list<class-string>,
31 * 'enums': list<class-string>,
32 * 'methods': list<string>,
33 * 'properties': list<string>,
34 * } File details
35 */
36 public function scanFile(string $filename): array
37 {
38 $parser = (new ParserFactory())->createForNewestSupportedVersion();
39 try {
40 $stmts = $parser->parse(file_get_contents($filename));
41 } catch (Error $error) {
42 throw new \RuntimeException($error->getMessage(), $error->getCode(), $error);
43 }
44
45 $result = [];
46 $result += $this->collect_stmts($stmts, '');
47 foreach ($stmts as $stmt) {
48 if ($stmt instanceof Namespace_) {
49 $namespace = (string) $stmt->name;
50
51 $result += $this->collect_stmts($stmt->stmts, $namespace);
52 }
53 }
54
55 return $result;
56 }
57
58 protected function collect_stmts(array $stmts, string $namespace): array
59 {
60 /** @var array $uses */
61 $uses = [];
62 $resolve = static function (string $name) use ($namespace, &$uses) {
63 if (array_key_exists($name, $uses)) {
64 return $uses[$name];
65 }
66
67 return $namespace . '\\' . $name;
68 };
69 $details = static function () use (&$uses): array {
70 return [
71 'uses' => $uses,
72 'interfaces' => [],
73 'traits' => [],
74 'enums' => [],
75 'methods' => [],
76 'properties' => [],
77 ];
78 };
79 $result = [];
80 foreach ($stmts as $stmt) {
81 switch ($stmt::class) {
82 case Use_::class:
83 $uses += $this->collect_uses($stmt);
84 break;
85 case Class_::class:
86 $result += $this->collect_class($stmt, $details(), $resolve);
87 break;
88 case Interface_::class:
89 $result += $this->collect_interface($stmt, $details(), $resolve);
90 break;
91 case Trait_::class:
92 case Enum_::class:
93 $result += $this->collect_classlike($stmt, $details(), $resolve);
94 break;
95 }
96 }
97
98 return $result;
99 }
100
101 protected function collect_uses(Use_ $stmt): array
102 {
103 $uses = [];
104
105 foreach ($stmt->uses as $use) {
106 $uses[(string) $use->getAlias()] = (string) $use->name;
107 }
108
109 return $uses;
110 }
111
112 protected function collect_classlike(ClassLike $stmt, array $details, callable $resolve): array
113 {
114 foreach ($stmt->getProperties() as $properties) {
115 foreach ($properties->props as $prop) {
116 $details['properties'][] = (string) $prop->name;
117 }
118 }
119
120 foreach ($stmt->getMethods() as $method) {
121 $details['methods'][] = (string) $method->name;
122 }
123
124 foreach ($stmt->getTraitUses() as $traitUse) {
125 foreach ($traitUse->traits as $trait) {
126 $details['traits'][] = $resolve((string) $trait);
127 }
128 }
129
130 return [
131 $resolve($stmt->name->name) => $details,
132 ];
133 }
134
135 protected function collect_class(Class_ $stmt, array $details, callable $resolve): array
136 {
137 foreach ($stmt->implements as $implement) {
138 $details['interfaces'][] = $resolve((string) $implement);
139 }
140
141 // promoted properties
142 if ($ctor = $stmt->getMethod('__construct')) {
143 foreach ($ctor->getParams() as $param) {
144 if ($param->flags) {
145 $details['properties'][] = $param->var->name;
146 }
147 }
148 }
149
150 return $this->collect_classlike($stmt, $details, $resolve);
151 }
152
153 protected function collect_interface(Interface_ $stmt, array $details, callable $resolve): array
154 {
155 foreach ($stmt->extends as $extend) {
156 $details['interfaces'][] = $resolve((string) $extend);
157 }
158
159 return $this->collect_classlike($stmt, $details, $resolve);
160 }
161 }