Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ArgumentsReader.php
Go to the documentation of this file.
1 <?php
7 
9 {
10  const NO_DEFAULT_VALUE = 'NO-DEFAULT';
11 
15  private $namespaceResolver;
16 
20  private $scalarTypesProvider;
21 
26  public function __construct(
27  NamespaceResolver $namespaceResolver = null,
28  ScalarTypesProvider $scalarTypesProvider = null
29  ) {
30  $this->namespaceResolver = $namespaceResolver ?: new NamespaceResolver();
31  $this->scalarTypesProvider = $scalarTypesProvider ?: new ScalarTypesProvider();
32  }
33 
44  public function getConstructorArguments(\ReflectionClass $class, $groupByPosition = false, $inherited = false)
45  {
46  $output = [];
50  if ($class->isInterface() || !$class->getFileName() || false == $class->hasMethod(
51  '__construct'
52  ) || !$inherited && $class->getConstructor()->class != $class->getName()
53  ) {
54  return $output;
55  }
56 
57  $constructor = new \Zend\Code\Reflection\MethodReflection($class->getName(), '__construct');
58  foreach ($constructor->getParameters() as $parameter) {
59  $name = $parameter->getName();
60  $position = $parameter->getPosition();
61  $index = $groupByPosition ? $position : $name;
62  $default = null;
63  if ($parameter->isOptional()) {
64  if ($parameter->isDefaultValueAvailable()) {
65  $value = $parameter->getDefaultValue();
66  if (true == is_array($value)) {
67  $default = $this->_varExportMin($value);
68  } elseif (true == is_int($value)) {
69  $default = $value;
70  } else {
71  $default = $parameter->getDefaultValue();
72  }
73  } elseif ($parameter->allowsNull()) {
74  $default = null;
75  }
76  }
77 
78  $output[$index] = [
79  'name' => $name,
80  'position' => $position,
81  'type' => $this->processType($class, $parameter),
82  'isOptional' => $parameter->isOptional(),
83  'default' => $default,
84  ];
85  }
86  return $output;
87  }
88 
96  private function processType(\ReflectionClass $class, \Zend\Code\Reflection\ParameterReflection $parameter)
97  {
98  if ($parameter->getClass()) {
99  return NamespaceResolver::NS_SEPARATOR . $parameter->getClass()->getName();
100  }
101 
102  $type = $parameter->detectType();
103 
104  if ($type === 'null') {
105  return null;
106  }
107 
108  if (strpos($type, '[]') !== false) {
109  return 'array';
110  }
111 
112  if (!in_array($type, $this->scalarTypesProvider->getTypes())) {
113  $availableNamespaces = $this->namespaceResolver->getImportedNamespaces(file($class->getFileName()));
114  $availableNamespaces[0] = $class->getNamespaceName();
115  return $this->namespaceResolver->resolveNamespace($type, $availableNamespaces);
116  }
117 
118  return $type;
119  }
120 
128  public function getParentCall(\ReflectionClass $class, array $classArguments)
129  {
131  if (!$class->getFileName()) {
132  return null;
133  }
134 
135  $trimFunction = function (&$value) {
136  $value = trim($value, PHP_EOL . ' $');
137  };
138 
139  $method = $class->getMethod('__construct');
140  $start = $method->getStartLine();
141  $end = $method->getEndLine();
142  $length = $end - $start;
143 
144  $source = file($class->getFileName());
145  $content = implode('', array_slice($source, $start, $length));
146  $pattern = '/parent::__construct\(([ ' .
147  PHP_EOL .
148  ']*[$]{1}[a-zA-Z0-9_]*,)*[ ' .
149  PHP_EOL .
150  ']*' .
151  '([$]{1}[a-zA-Z0-9_]*){1}[' .
152  PHP_EOL .
153  ' ]*\);/';
154 
155  if (!preg_match($pattern, $content, $matches)) {
156  return null;
157  }
158 
159  $arguments = $matches[0];
160  if (!trim($arguments)) {
161  return null;
162  }
163 
164  $arguments = substr(trim($arguments), 20, -2);
165  $arguments = explode(',', $arguments);
166  array_walk($arguments, $trimFunction);
167 
168  $output = [];
169  foreach ($arguments as $argumentPosition => $argumentName) {
170  $type = isset($classArguments[$argumentName]) ? $classArguments[$argumentName]['type'] : null;
171  $output[$argumentPosition] = [
172  'name' => $argumentName,
173  'position' => $argumentPosition,
174  'type' => $type,
175  ];
176  }
177  return $output;
178  }
179 
187  public function isCompatibleType($requiredType, $actualType)
188  {
190  if ($requiredType === $actualType) {
191  return true;
192  }
193 
195  if ($requiredType === null || $actualType === null) {
196  return true;
197  }
198 
203  if ($requiredType === 'array' || $actualType === 'array') {
204  return false;
205  }
206 
207  if ($requiredType === 'mixed' || $actualType === 'mixed') {
208  return true;
209  }
210 
211  return is_subclass_of($actualType, $requiredType);
212  }
213 
220  protected function _varExportMin($var)
221  {
222  if (is_array($var)) {
223  $toImplode = [];
224  foreach ($var as $key => $value) {
225  $toImplode[] = var_export($key, true) . ' => ' . $this->_varExportMin($value);
226  }
227  $code = 'array(' . implode(', ', $toImplode) . ')';
228  return $code;
229  } else {
230  return var_export($var, true);
231  }
232  }
233 
240  public function getAnnotations(\ReflectionClass $class)
241  {
242  $regexp = '(@([a-z_][a-z0-9_]+)\(([^\)]+)\))i';
243  $docBlock = $class->getConstructor()->getDocComment();
244  $annotations = [];
245  preg_match_all($regexp, $docBlock, $matches);
246  foreach (array_keys($matches[0]) as $index) {
247  $name = $matches[1][$index];
248  $value = trim($matches[2][$index], '" ');
249  $annotations[$name] = $value;
250  }
251 
252  return $annotations;
253  }
254 }
is_subclass_of($obj, $className)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$pattern
Definition: website.php:22
getConstructorArguments(\ReflectionClass $class, $groupByPosition=false, $inherited=false)
$source
Definition: source.php:23
$start
Definition: listing.phtml:18
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
$arguments
__construct(NamespaceResolver $namespaceResolver=null, ScalarTypesProvider $scalarTypesProvider=null)
$index
Definition: list.phtml:44
getParentCall(\ReflectionClass $class, array $classArguments)
$code
Definition: info.phtml:12
if(!isset($_GET['name'])) $name
Definition: log.php:14