@@ -36,22 +36,64 @@ public function detect($filePath, $functions)
36
36
$ result = [];
37
37
$ regexp = $ this ->composeRegexp ($ functions );
38
38
39
- if ($ regexp ) {
40
- $ fileContent = \Magento \TestFramework \Utility \ChangedFiles::getChangedContent ($ filePath );
41
- $ matches = preg_grep ($ regexp , explode ("\n" , $ fileContent ));
42
- $ file = file ($ filePath );
43
- if (!empty ($ matches )) {
44
- foreach ($ matches as $ line ) {
45
- $ actualFunctions = [];
46
- foreach ($ functions as $ function ) {
47
- if (false !== strpos ($ line , $ function )) {
48
- $ actualFunctions [] = $ function ;
49
- }
39
+ if (!$ regexp ) {
40
+ return $ result ;
41
+ }
42
+
43
+ $ fileContent = \Magento \TestFramework \Utility \ChangedFiles::getChangedContent ($ filePath );
44
+ $ file = file ($ filePath );
45
+
46
+ return $ fileContent
47
+ ? $ this ->grepChangedContent ($ file , $ regexp , $ functions , $ fileContent )
48
+ : $ this ->grepFile ($ file , $ regexp );
49
+ }
50
+
51
+ /**
52
+ * Grep only changed content.
53
+ *
54
+ * @param array $file
55
+ * @param string $regexp
56
+ * @param string[] $functions
57
+ * @param string $fileContent
58
+ * @return array
59
+ */
60
+ public function grepChangedContent (array $ file , $ regexp , $ functions , $ fileContent )
61
+ {
62
+ $ result = [];
63
+ $ matches = preg_grep ($ regexp , explode ("\n" , $ fileContent ));
64
+ if (!empty ($ matches )) {
65
+ foreach ($ matches as $ line ) {
66
+ $ actualFunctions = [];
67
+ foreach ($ functions as $ function ) {
68
+ if (false !== strpos ($ line , $ function )) {
69
+ $ actualFunctions [] = $ function ;
50
70
}
51
- $ result [array_search ($ line . "\n" , $ file ) + 1 ] = $ actualFunctions ;
52
71
}
72
+ $ result [array_search ($ line . "\n" , $ file ) + 1 ] = $ actualFunctions ;
53
73
}
54
74
}
75
+
76
+ return $ result ;
77
+ }
78
+
79
+ /**
80
+ * Grep File.
81
+ *
82
+ * @param array $file
83
+ * @param string $regexp
84
+ * @return array
85
+ */
86
+ public function grepFile (array $ file , $ regexp )
87
+ {
88
+ $ result = [];
89
+ array_unshift ($ file , '' );
90
+ $ lines = preg_grep ($ regexp , $ file );
91
+ foreach ($ lines as $ lineNumber => $ line ) {
92
+ if (preg_match_all ($ regexp , $ line , $ matches )) {
93
+ $ result [$ lineNumber ] = $ matches [1 ];
94
+ }
95
+ }
96
+
55
97
return $ result ;
56
98
}
57
99
0 commit comments