@@ -37,58 +37,48 @@ public function __construct(NamespaceResolver $namespaceResolver = null)
37
37
* @SuppressWarnings(PHPMD.NPathComplexity)
38
38
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
39
39
*/
40
- public function getConstructorArgumentTypes (\ReflectionClass $ class , $ inherited = false )
41
- {
40
+ public function getConstructorArgumentTypes (
41
+ \ReflectionClass $ class ,
42
+ $ inherited = false
43
+ ) {
42
44
$ output = [null ];
43
45
if (!$ class ->getFileName () || false == $ class ->hasMethod (
44
46
'__construct '
45
47
) || !$ inherited && $ class ->getConstructor ()->class !== $ class ->getName ()
46
48
) {
47
49
return $ output ;
48
50
}
49
- $ reflectionConstructor = $ class ->getConstructor ();
50
- $ fileContent = file ($ class ->getFileName ());
51
- $ availableNamespaces = $ this ->namespaceResolver ->getImportedNamespaces ($ fileContent );
52
- $ availableNamespaces [0 ] = $ class ->getNamespaceName ();
53
- $ constructorStartLine = $ reflectionConstructor ->getStartLine () - 1 ;
54
- $ constructorEndLine = $ reflectionConstructor ->getEndLine ();
55
- $ fileContent = array_slice ($ fileContent , $ constructorStartLine , $ constructorEndLine - $ constructorStartLine );
56
- $ source = '<?php ' . trim (implode ('' , $ fileContent ));
57
-
58
- // Remove parameter default value.
59
- $ source = preg_replace ("/ = (.*)/ " , ',) ' , $ source );
60
51
61
- $ methodTokenized = token_get_all ($ source );
62
- $ argumentsStart = array_search ('( ' , $ methodTokenized ) + 1 ;
63
- $ argumentsEnd = array_search (') ' , $ methodTokenized );
64
- $ arguments = array_slice ($ methodTokenized , $ argumentsStart , $ argumentsEnd - $ argumentsStart );
65
- foreach ($ arguments as &$ argument ) {
66
- is_array ($ argument ) ?: $ argument = [1 => $ argument ];
67
- }
68
- unset($ argument );
69
- $ arguments = array_filter ($ arguments , function ($ token ) {
70
- $ blacklist = [T_VARIABLE , T_WHITESPACE ];
71
- if (isset ($ token [0 ]) && in_array ($ token [0 ], $ blacklist )) {
72
- return false ;
52
+ //Reading parameters' types.
53
+ $ params = $ class ->getConstructor ()->getParameters ();
54
+ /** @var string[] $types */
55
+ $ types = [];
56
+ foreach ($ params as $ param ) {
57
+ //For the sake of backward compatibility.
58
+ $ typeName = '' ;
59
+ if ($ param ->isArray ()) {
60
+ //For the sake of backward compatibility.
61
+ $ typeName = 'array ' ;
62
+ } else {
63
+ try {
64
+ $ paramClass = $ param ->getClass ();
65
+ if ($ paramClass ) {
66
+ $ typeName = '\\' .$ paramClass ->getName ();
67
+ }
68
+ } catch (\ReflectionException $ exception ) {
69
+ //If there's a problem loading a class then ignore it and
70
+ //just return it's name.
71
+ $ typeName = '\\' .$ param ->getType ()->getName ();
72
+ }
73
73
}
74
- return true ;
75
- });
76
- $ arguments = array_map (function ($ element ) {
77
- return $ element [1 ];
78
- }, $ arguments );
79
- $ arguments = array_values ($ arguments );
80
- $ arguments = implode ('' , $ arguments );
81
- if (empty ($ arguments )) {
82
- return $ output ;
74
+ $ types [] = $ typeName ;
83
75
}
84
- $ arguments = explode (', ' , $ arguments );
85
- foreach ($ arguments as $ key => &$ argument ) {
86
- $ argument = $ this ->removeToken ($ argument , '= ' );
87
- $ argument = $ this ->removeToken ($ argument , '& ' );
88
- $ argument = $ this ->namespaceResolver ->resolveNamespace ($ argument , $ availableNamespaces );
76
+ if (!$ types ) {
77
+ //For the sake of backward compatibility.
78
+ $ types = [null ];
89
79
}
90
- unset( $ argument );
91
- return $ arguments ;
80
+
81
+ return $ types ;
92
82
}
93
83
94
84
/**
@@ -98,7 +88,7 @@ public function getConstructorArgumentTypes(\ReflectionClass $class, $inherited
98
88
* @param array $availableNamespaces
99
89
* @return string
100
90
* @deprecated 100.2.0
101
- * @see \Magento\Framework\Code\Reader\NamespaceResolver::resolveNamespace
91
+ * @see getConstructorArgumentTypes
102
92
*/
103
93
protected function resolveNamespaces ($ argument , $ availableNamespaces )
104
94
{
@@ -111,6 +101,8 @@ protected function resolveNamespaces($argument, $availableNamespaces)
111
101
* @param string $argument
112
102
* @param string $token
113
103
* @return string
104
+ *
105
+ * @deprecated Not used anymore.
114
106
*/
115
107
protected function removeToken ($ argument , $ token )
116
108
{
@@ -127,7 +119,7 @@ protected function removeToken($argument, $token)
127
119
* @param array $file
128
120
* @return array
129
121
* @deprecated 100.2.0
130
- * @see \Magento\Framework\Code\Reader\NamespaceResolver::getImportedNamespaces
122
+ * @see getConstructorArgumentTypes
131
123
*/
132
124
protected function getImportedNamespaces (array $ file )
133
125
{
0 commit comments