Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TypeDuplication.php
Go to the documentation of this file.
1 <?php
9 
11 
13 {
17  const SUPPRESS_ANNOTATION = 'SuppressWarnings';
18 
19  const TYPE_DUPLICATIONS = 'Magento.TypeDuplication';
20 
24  protected $_argumentsReader;
25 
29  private $scalarTypesProvider;
30 
35  public function __construct(
36  \Magento\Framework\Code\Reader\ArgumentsReader $argumentsReader = null,
37  \Magento\Framework\Code\Reader\ScalarTypesProvider $scalarTypesProvider = null
38  ) {
39  $this->_argumentsReader = $argumentsReader ?: new \Magento\Framework\Code\Reader\ArgumentsReader();
40  $this->scalarTypesProvider = $scalarTypesProvider ?: new \Magento\Framework\Code\Reader\ScalarTypesProvider();
41  }
42 
50  public function validate($className)
51  {
52  $class = new \ReflectionClass($className);
53  $classArguments = $this->_argumentsReader->getConstructorArguments($class, true);
54 
55  $arguments = $this->_getObjectArguments($classArguments);
56 
57  $typeList = [];
58  $errors = [];
59  foreach ($arguments as $argument) {
60  $name = $argument['name'];
61  $type = $argument['type'];
62  if (in_array($type, $typeList)) {
63  $errors[] = 'Multiple type injection [' . $type . ']';
64  } elseif (isset($typeList[$name])) {
65  $errors[] = 'Variable name duplication. [$' . $name . ']';
66  }
67  $typeList[$name] = $type;
68  }
69 
70  if (!empty($errors)) {
71  if (false == $this->_ignoreWarning($class)) {
72  $classPath = str_replace('\\', '/', $class->getFileName());
73  throw new \Magento\Framework\Exception\ValidatorException(
74  new \Magento\Framework\Phrase(
75  'Argument type duplication in class %1 in %2%3%4',
76  [
77  $class->getName(),
78  $classPath,
79  PHP_EOL,
80  implode(PHP_EOL, $errors)
81  ]
82  )
83  );
84  }
85  }
86  return true;
87  }
88 
95  protected function _getObjectArguments(array $arguments)
96  {
97  $output = [];
98  foreach ($arguments as $argument) {
99  $type = $argument['type'];
100  if (!$type || in_array($type, $this->scalarTypesProvider->getTypes())) {
101  continue;
102  }
103  $reflection = new \ReflectionClass($type);
104  if (false == $reflection->isInterface()) {
105  $output[] = $argument;
106  }
107  }
108  return $output;
109  }
110 
117  protected function _ignoreWarning(\ReflectionClass $class)
118  {
119  $annotations = $this->_argumentsReader->getAnnotations($class);
120  if (isset($annotations[self::SUPPRESS_ANNOTATION])) {
121  return $annotations[self::SUPPRESS_ANNOTATION] == self::TYPE_DUPLICATIONS;
122  }
123  return false;
124  }
125 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13
__construct(\Magento\Framework\Code\Reader\ArgumentsReader $argumentsReader=null, \Magento\Framework\Code\Reader\ScalarTypesProvider $scalarTypesProvider=null)
$arguments
$errors
Definition: overview.phtml:9
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
if(!isset($_GET['name'])) $name
Definition: log.php:14