Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FunctionDetector.php
Go to the documentation of this file.
1 <?php
7 
12 {
34  public function detect($filePath, $functions)
35  {
36  $result = [];
37  $regexp = $this->composeRegexp($functions);
38 
39  if (!$regexp) {
40  return $result;
41  }
42 
44  $file = file($filePath);
45 
46  return $fileContent
47  ? $this->grepChangedContent($file, $regexp, $functions, $fileContent)
48  : $this->grepFile($file, $regexp);
49  }
50 
60  public function grepChangedContent(array $file, $regexp, $functions, $fileContent)
61  {
62  $result = [];
63  $matches = preg_grep($regexp, explode("\n", $fileContent));
64  if (!empty($matches)) {
65  foreach ($matches as $line) {
66  $actualFunctions = [];
67  foreach ($functions as $function) {
68  if (false !== strpos($line, $function)) {
69  $actualFunctions[] = $function;
70  }
71  }
72  $result[array_search($line . "\n", $file) + 1] = $actualFunctions;
73  }
74  }
75 
76  return $result;
77  }
78 
86  public function grepFile(array $file, $regexp)
87  {
88  $result = [];
89  array_unshift($file, '');
90  $lines = preg_grep($regexp, $file);
91  foreach ($lines as $lineNumber => $line) {
92  if (preg_match_all($regexp, $line, $matches)) {
93  $result[$lineNumber] = $matches[1];
94  }
95  }
96 
97  return $result;
98  }
99 
106  private function composeRegexp(array $functions)
107  {
108  if (empty($functions)) {
109  return '';
110  }
111  return '/(?<!function |->|::)\b(' . join('|', $functions) . ')\s*\(/i';
112  }
113 }
grepChangedContent(array $file, $regexp, $functions, $fileContent)