PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / prefixed / php-di / phpdoc-reader / src / PhpDocReader / PhpParser / UseStatementParser.php
matomo / app / vendor / prefixed / php-di / phpdoc-reader / src / PhpDocReader / PhpParser Last commit date
TokenParser.php 1 year ago UseStatementParser.php 1 year ago
UseStatementParser.php
57 lines
1 <?php
2
3 declare (strict_types=1);
4 namespace Matomo\Dependencies\PhpDocReader\PhpParser;
5
6 use SplFileObject;
7 /**
8 * Parses a file for "use" declarations.
9 *
10 * Class taken and adapted from doctrine/annotations to avoid pulling the whole package.
11 *
12 * Authors: Fabien Potencier <fabien@symfony.com> and Christian Kaps <christian.kaps@mohiva.com>
13 */
14 class UseStatementParser
15 {
16 /**
17 * @return array A list with use statements in the form (Alias => FQN).
18 */
19 public function parseUseStatements(\ReflectionClass $class) : array
20 {
21 $filename = $class->getFilename();
22 if ($filename === \false) {
23 return [];
24 }
25 $content = $this->getFileContent($filename, $class->getStartLine());
26 if ($content === null) {
27 return [];
28 }
29 $namespace = preg_quote($class->getNamespaceName(), '/');
30 $content = preg_replace('/^.*?(\\bnamespace\\s+' . $namespace . '\\s*[;{].*)$/s', '\\1', $content);
31 $tokenizer = new TokenParser('<?php ' . $content);
32 return $tokenizer->parseUseStatements($class->getNamespaceName());
33 }
34 /**
35 * Gets the content of the file right up to the given line number.
36 *
37 * @param string $filename The name of the file to load.
38 * @param int $lineNumber The number of lines to read from file.
39 */
40 private function getFileContent(string $filename, int $lineNumber) : string
41 {
42 if (!is_file($filename)) {
43 throw new \RuntimeException("Unable to read file {$filename}");
44 }
45 $content = '';
46 $lineCnt = 0;
47 $file = new SplFileObject($filename);
48 while (!$file->eof()) {
49 if ($lineCnt++ === $lineNumber) {
50 break;
51 }
52 $content .= $file->fgets();
53 }
54 return $content;
55 }
56 }
57