PluginProbe
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 2.4.1
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v2.4.1
4.7.4 4.7.5 4.7.3 4.7.2 4.7.1 trunk 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 3.0.0 3.0.1 3.1.0 3.10.0 3.10.1 3.11.1 3.11.2 3.11.3 3.2.0 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 All 95 releases
cookiebot / addons / lib / ioc / php-di / phpdoc-reader / src / PhpDocReader / PhpParser / UseStatementParser.php

UseStatementParser.php in Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode 2.4.1, at addons/lib/ioc/php-di/phpdoc-reader/src/PhpDocReader/PhpParser/UseStatementParser.php

69 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace PhpDocReader\PhpParser;
4
5 use SplFileObject;
6
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 * @author Fabien Potencier <[email protected]>
13 * @author Christian Kaps <[email protected]>
14 */
15 class UseStatementParser
16 {
17 /**
18 * @return array A list with use statements in the form (Alias => FQN).
19 */
20 public function parseUseStatements(\ReflectionClass $class)
21 {
22 if (false === $filename = $class->getFilename()) {
23 return array();
24 }
25
26 $content = $this->getFileContent($filename, $class->getStartLine());
27
28 if (null === $content) {
29 return array();
30 }
31
32 $namespace = preg_quote($class->getNamespaceName());
33 $content = preg_replace('/^.*?(\bnamespace\s+' . $namespace . '\s*[;{].*)$/s', '\\1', $content);
34 $tokenizer = new TokenParser('<?php ' . $content);
35
36 $statements = $tokenizer->parseUseStatements($class->getNamespaceName());
37
38 return $statements;
39 }
40
41 /**
42 * Gets the content of the file right up to the given line number.
43 *
44 * @param string $filename The name of the file to load.
45 * @param integer $lineNumber The number of lines to read from file.
46 *
47 * @return string The content of the file.
48 */
49 private function getFileContent($filename, $lineNumber)
50 {
51 if ( ! is_file($filename)) {
52 return null;
53 }
54
55 $content = '';
56 $lineCnt = 0;
57 $file = new SplFileObject($filename);
58 while (!$file->eof()) {
59 if ($lineCnt++ == $lineNumber) {
60 break;
61 }
62
63 $content .= $file->fgets();
64 }
65
66 return $content;
67 }
68 }
69