SourceArgumentsReader.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Code\Reader;
  7. class SourceArgumentsReader
  8. {
  9. /**
  10. * Namespace separator
  11. * @deprecated
  12. * @see \Magento\Framework\Code\Reader\NamespaceResolver::NS_SEPARATOR
  13. */
  14. const NS_SEPARATOR = '\\';
  15. /**
  16. * @var NamespaceResolver
  17. */
  18. private $namespaceResolver;
  19. /**
  20. * @param NamespaceResolver|null $namespaceResolver
  21. */
  22. public function __construct(NamespaceResolver $namespaceResolver = null)
  23. {
  24. $this->namespaceResolver = $namespaceResolver ?: new NamespaceResolver();
  25. }
  26. /**
  27. * Read constructor argument types from source code and perform namespace resolution if required.
  28. *
  29. * @param \ReflectionClass $class
  30. * @param bool $inherited
  31. * @return array List of constructor argument types.
  32. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  33. * @SuppressWarnings(PHPMD.NPathComplexity)
  34. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  35. */
  36. public function getConstructorArgumentTypes(
  37. \ReflectionClass $class,
  38. $inherited = false
  39. ) {
  40. $output = [null];
  41. if (!$class->getFileName() || false == $class->hasMethod(
  42. '__construct'
  43. ) || !$inherited && $class->getConstructor()->class !== $class->getName()
  44. ) {
  45. return $output;
  46. }
  47. //Reading parameters' types.
  48. $params = $class->getConstructor()->getParameters();
  49. /** @var string[] $types */
  50. $types = [];
  51. foreach ($params as $param) {
  52. //For the sake of backward compatibility.
  53. $typeName = '';
  54. if ($param->isArray()) {
  55. //For the sake of backward compatibility.
  56. $typeName = 'array';
  57. } else {
  58. try {
  59. $paramClass = $param->getClass();
  60. if ($paramClass) {
  61. $typeName = '\\' .$paramClass->getName();
  62. }
  63. } catch (\ReflectionException $exception) {
  64. //If there's a problem loading a class then ignore it and
  65. //just return it's name.
  66. $typeName = '\\' .$param->getType()->getName();
  67. }
  68. }
  69. $types[] = $typeName;
  70. }
  71. if (!$types) {
  72. //For the sake of backward compatibility.
  73. $types = [null];
  74. }
  75. return $types;
  76. }
  77. /**
  78. * Perform namespace resolution if required and return fully qualified name.
  79. *
  80. * @param string $argument
  81. * @param array $availableNamespaces
  82. * @return string
  83. * @deprecated 101.0.0
  84. * @see getConstructorArgumentTypes
  85. */
  86. protected function resolveNamespaces($argument, $availableNamespaces)
  87. {
  88. return $this->namespaceResolver->resolveNamespace($argument, $availableNamespaces);
  89. }
  90. /**
  91. * Remove default value from argument.
  92. *
  93. * @param string $argument
  94. * @param string $token
  95. * @return string
  96. *
  97. * @deprecated 102.0.0 Not used anymore.
  98. */
  99. protected function removeToken($argument, $token)
  100. {
  101. $position = strpos($argument, $token);
  102. if (is_numeric($position)) {
  103. return substr($argument, 0, $position);
  104. }
  105. return $argument;
  106. }
  107. /**
  108. * Get all imported namespaces.
  109. *
  110. * @param array $file
  111. * @return array
  112. * @deprecated 101.0.0
  113. * @see getConstructorArgumentTypes
  114. */
  115. protected function getImportedNamespaces(array $file)
  116. {
  117. return $this->namespaceResolver->getImportedNamespaces($file);
  118. }
  119. }