Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AutogeneratedClassNotInConstructorFinder.php
Go to the documentation of this file.
1 <?php
7 
13 {
17  private $classNameExtractor;
18 
24  public function __construct(
25  ClassNameExtractor $classNameExtractor
26  ) {
27  $this->classNameExtractor = $classNameExtractor;
28  }
29 
36  public function find($fileContent)
37  {
38  $classNames = [];
39 
40  preg_match_all(
41  '/(get|create)\(\s*([a-z0-9\\\\]+)::class\s*\)/im',
42  $fileContent,
43  $shortNameMatches
44  );
45 
46  if (isset($shortNameMatches[2])) {
47  foreach ($shortNameMatches[2] as $shortName) {
48  if (substr($shortName, 0, 1) !== '\\') {
49  $class = $this->matchPartialNamespace($fileContent, $shortName);
50  } else {
51  $class = $shortName;
52  }
53  $class = ltrim($class, '\\');
54 
55  if (\Magento\Framework\App\Utility\Classes::isVirtual($class)) {
56  continue;
57  }
58 
59  $className = $this->classNameExtractor->getNameWithNamespace($fileContent);
60  if ($className) {
61  $arguments = $this->getConstructorArguments($className);
62  if (in_array($class, $arguments)) {
63  continue;
64  }
65  }
66 
67  $classNames[] = $class;
68  }
69  }
70  return $classNames;
71  }
72 
79  private function getConstructorArguments($className)
80  {
81  $arguments = [];
82  $reflectionClass = new \ReflectionClass($className);
83  $constructor = $reflectionClass->getConstructor();
84  if ($constructor) {
85  $classParameters = $constructor->getParameters();
86  foreach ($classParameters as $classParameter) {
87  if ($classParameter->getType()) {
88  $parameterType = $classParameter->getType();
89  $arguments[] = ltrim($parameterType, '\\');
90  }
91  }
92  }
93  return $arguments;
94  }
95 
103  private function matchPartialNamespace($fileContent, $shortName)
104  {
105  preg_match_all(
106  '/^use\s([a-z0-9\\\\]+' . str_replace('\\', '\\\\', $shortName) . ');$/im',
107  $fileContent,
108  $fullNameMatches
109  );
110  if (isset($fullNameMatches[1][0])) {
111  $class = $fullNameMatches[1][0];
112  } else {
113  preg_match_all(
114  '/^use\s([a-z0-9\\\\]+)\sas\s' . str_replace('\\', '\\\\', $shortName) . ';$/im',
115  $fileContent,
116  $fullNameAliasMatches
117  );
118  if (isset($fullNameAliasMatches[1][0])) {
119  $class = $fullNameAliasMatches[1][0];
120  } else {
121  $forwardSlashPos = strpos($shortName, '\\');
122  $partialNamespace = substr($shortName, 0, $forwardSlashPos);
123  preg_match_all(
124  '/^use\s([a-z0-9\\\\]+)' . $partialNamespace . ';$/im',
125  $fileContent,
126  $partialNamespaceMatches
127  );
128  if ($forwardSlashPos && isset($partialNamespaceMatches[1][0])) {
129  $class = $partialNamespaceMatches[1][0] . $shortName;
130  } else {
131  $class = $this->classNameExtractor->getNamespace($fileContent) . '\\' . $shortName;
132  }
133  }
134  }
135  return $class;
136  }
137 }
$_option $_optionId $class
Definition: date.phtml:13
$reflectionClass
Definition: categories.php:25
$arguments
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31