|
8 | 8 |
|
9 | 9 | namespace Magento\Test\Php;
|
10 | 10 |
|
11 |
| -use Magento\Framework\App\Utility; |
12 | 11 | use Magento\TestFramework\CodingStandard\Tool\CodeMessDetector;
|
13 | 12 | use Magento\TestFramework\CodingStandard\Tool\CodeSniffer;
|
14 | 13 | use Magento\TestFramework\CodingStandard\Tool\CodeSniffer\Wrapper;
|
@@ -74,44 +73,89 @@ private static function getChangedFilesBaseDir() {
|
74 | 73 | */
|
75 | 74 | public static function getWhitelist($fileTypes = ['php'], $changedFilesBaseDir = '', $baseFilesFolder = '')
|
76 | 75 | {
|
77 |
| - $globPatternsFolder = self::getBaseFilesFolder(); |
78 |
| - if ('' !== $baseFilesFolder) { |
79 |
| - $globPatternsFolder = $baseFilesFolder; |
| 76 | + $changedFiles = self::getChangedFilesList($changedFilesBaseDir); |
| 77 | + if (empty($changedFiles)) { |
| 78 | + return []; |
80 | 79 | }
|
| 80 | + |
| 81 | + $globPatternsFolder = ('' !== $baseFilesFolder) ? $baseFilesFolder : self::getBaseFilesFolder(); |
81 | 82 | $directoriesToCheck = Files::init()->readLists($globPatternsFolder . '/_files/whitelist/common.txt');
|
| 83 | + $targetFiles = self::filterFiles($changedFiles, $fileTypes, $directoriesToCheck); |
| 84 | + |
| 85 | + return $targetFiles; |
| 86 | + } |
82 | 87 |
|
| 88 | + private static function getChangedFilesList($changedFilesBaseDir) |
| 89 | + { |
83 | 90 | $changedFiles = [];
|
| 91 | + |
84 | 92 | $globFilesListPattern = ($changedFilesBaseDir ?: self::getChangedFilesBaseDir()) . '/_files/changed_files*';
|
85 |
| - foreach (glob($globFilesListPattern) as $listFile) { |
86 |
| - $changedFiles = array_merge($changedFiles, file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)); |
| 93 | + $listFiles = glob($globFilesListPattern); |
| 94 | + if (count($listFiles)) { |
| 95 | + foreach ($listFiles as $listFile) { |
| 96 | + $changedFiles = array_merge( |
| 97 | + $changedFiles, |
| 98 | + file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) |
| 99 | + ); |
| 100 | + } |
| 101 | + } else { |
| 102 | + // if no list files, probably, this is the dev environment |
| 103 | + @exec('git diff --name-only', $changedFiles); |
87 | 104 | }
|
| 105 | + |
88 | 106 | array_walk(
|
89 | 107 | $changedFiles,
|
90 | 108 | function (&$file) {
|
91 | 109 | $file = BP . '/' . $file;
|
92 | 110 | }
|
93 | 111 | );
|
94 |
| - $changedFiles = array_filter( |
95 |
| - $changedFiles, |
96 |
| - function ($path) use ($directoriesToCheck, $fileTypes) { |
97 |
| - if (!file_exists($path)) { |
98 |
| - return false; |
99 |
| - } |
100 |
| - $path = realpath($path); |
101 |
| - foreach ($directoriesToCheck as $directory) { |
102 |
| - $directory = realpath($directory); |
103 |
| - if (strpos($path, $directory) === 0) { |
104 |
| - if (!empty($fileTypes)) { |
105 |
| - return in_array(pathinfo($path, PATHINFO_EXTENSION), $fileTypes); |
106 |
| - } |
| 112 | + |
| 113 | + return $changedFiles; |
| 114 | + } |
| 115 | + |
| 116 | + private static function filterFiles(array $files, array $allowedFileTypes, array $allowedDirectories) |
| 117 | + { |
| 118 | + if (empty($allowedFileTypes)) { |
| 119 | + $fileHasAllowedType = function () { |
| 120 | + return true; |
| 121 | + }; |
| 122 | + } else { |
| 123 | + $fileHasAllowedType = function ($file) use ($allowedFileTypes) { |
| 124 | + return in_array(pathinfo($file, PATHINFO_EXTENSION), $allowedFileTypes); |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + if (empty($allowedDirectories)) { |
| 129 | + $fileIsInAllowedDirectory = function () { |
| 130 | + return true; |
| 131 | + }; |
| 132 | + } else { |
| 133 | + $allowedDirectories = array_map('realpath', $allowedDirectories); |
| 134 | + usort($allowedDirectories, function ($dir1, $dir2) { |
| 135 | + return strlen($dir1) - strlen($dir2); |
| 136 | + }); |
| 137 | + $fileIsInAllowedDirectory = function ($file) use ($allowedDirectories) { |
| 138 | + foreach ($allowedDirectories as $directory) { |
| 139 | + if (strpos($file, $directory) === 0) { |
107 | 140 | return true;
|
108 | 141 | }
|
109 | 142 | }
|
110 | 143 | return false;
|
| 144 | + }; |
| 145 | + } |
| 146 | + |
| 147 | + $filtered = array_filter( |
| 148 | + $files, |
| 149 | + function ($file) use ($fileHasAllowedType, $fileIsInAllowedDirectory) { |
| 150 | + $file = realpath($file); |
| 151 | + if (false === $file) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + return $fileHasAllowedType($file) && $fileIsInAllowedDirectory($file); |
111 | 155 | }
|
112 | 156 | );
|
113 | 157 |
|
114 |
| - return $changedFiles; |
| 158 | + return $filtered; |
115 | 159 | }
|
116 | 160 |
|
117 | 161 | /**
|
|
0 commit comments