diff --git a/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php b/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php index 42b34560ae430467bfa59fdfa76cbfdec6e297b1..a25b16c2277f3e7052840e9cf1fbbe2660cc4462 100644 --- a/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php +++ b/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php @@ -60,10 +60,14 @@ class ChangedFiles */ public static function getChangedContent($fileName) { + $data = []; $extension = self::getFileExtension($fileName); $fileName = ltrim(str_replace(BP, '', $fileName), DIRECTORY_SEPARATOR); - $changedContent = file_get_contents(BP . sprintf(self::CHANGED_FILES_CONTENT_FILE, $extension)); - $data = json_decode($changedContent, true); + $changedFilesContentFile = BP . sprintf(self::CHANGED_FILES_CONTENT_FILE, $extension); + if (file_exists($changedFilesContentFile)) { + $changedContent = file_get_contents($changedFilesContentFile); + $data = json_decode($changedContent, true); + } return isset($data[$fileName]) ? $data[$fileName] : ''; } diff --git a/dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php b/dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php index 308b34855a3e44ec0df2143f26553b192ad8eef5..69162d3dfba2e6d67acb9c9bad48dbb03fc3c41a 100644 --- a/dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php +++ b/dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php @@ -36,22 +36,64 @@ class FunctionDetector $result = []; $regexp = $this->composeRegexp($functions); - if ($regexp) { - $fileContent = \Magento\TestFramework\Utility\ChangedFiles::getChangedContent($filePath); - $matches = preg_grep($regexp, explode("\n", $fileContent)); - $file = file($filePath); - if (!empty($matches)) { - foreach ($matches as $line) { - $actualFunctions = []; - foreach ($functions as $function) { - if (false !== strpos($line, $function)) { - $actualFunctions[] = $function; - } + if (!$regexp) { + return $result; + } + + $fileContent = \Magento\TestFramework\Utility\ChangedFiles::getChangedContent($filePath); + $file = file($filePath); + + return $fileContent + ? $this->grepChangedContent($file, $regexp, $functions, $fileContent) + : $this->grepFile($file, $regexp); + } + + /** + * Grep only changed content. + * + * @param array $file + * @param string $regexp + * @param string[] $functions + * @param string $fileContent + * @return array + */ + public function grepChangedContent(array $file, $regexp, $functions, $fileContent) + { + $result = []; + $matches = preg_grep($regexp, explode("\n", $fileContent)); + if (!empty($matches)) { + foreach ($matches as $line) { + $actualFunctions = []; + foreach ($functions as $function) { + if (false !== strpos($line, $function)) { + $actualFunctions[] = $function; } - $result[array_search($line . "\n", $file) + 1] = $actualFunctions; } + $result[array_search($line . "\n", $file) + 1] = $actualFunctions; } } + + return $result; + } + + /** + * Grep File. + * + * @param array $file + * @param string $regexp + * @return array + */ + public function grepFile(array $file, $regexp) + { + $result = []; + array_unshift($file, ''); + $lines = preg_grep($regexp, $file); + foreach ($lines as $lineNumber => $line) { + if (preg_match_all($regexp, $line, $matches)) { + $result[$lineNumber] = $matches[1]; + } + } + return $result; } diff --git a/dev/tests/static/get_github_changes.php b/dev/tests/static/get_github_changes.php index 7e57a13552de1de97be4849f5127dd46df57ed4d..16a4f7d8e090de40c4881f1e3a60015bb377396b 100644 --- a/dev/tests/static/get_github_changes.php +++ b/dev/tests/static/get_github_changes.php @@ -56,7 +56,11 @@ function saveChangedFileContent(GitRepo $repo) { $changedFilesContentFileName = BP . Magento\TestFramework\Utility\ChangedFiles::CHANGED_FILES_CONTENT_FILE; foreach ($repo->getChangedContentFiles() as $key => $changedContentFile) { - file_put_contents(sprintf($changedFilesContentFileName, $key), json_encode($changedContentFile), FILE_APPEND); + $filePath = sprintf($changedFilesContentFileName, $key); + $oldContent = file_exists($filePath) ? file_get_contents($filePath) : '{}'; + $oldData = json_decode($oldContent, true); + $data = array_merge($oldData, $changedContentFile); + file_put_contents($filePath, json_encode($data)); } }