Skip to content

Commit 57d1211

Browse files
author
Volodymyr Kublytskyi
committed
MAGETWO-64523: Add static test on forbidden "final" keyword and eliminate it usage in code
- refactoring to remove complexity on getWhiteListMethod - added extracting of changed files from "git diff" to allow run static tests on dev environment without changed files list generation
1 parent eb31539 commit 57d1211

File tree

1 file changed

+64
-20
lines changed

1 file changed

+64
-20
lines changed

dev/tests/static/testsuite/Magento/Test/Php/LiveCodeTest.php

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Magento\Test\Php;
1010

11-
use Magento\Framework\App\Utility;
1211
use Magento\TestFramework\CodingStandard\Tool\CodeMessDetector;
1312
use Magento\TestFramework\CodingStandard\Tool\CodeSniffer;
1413
use Magento\TestFramework\CodingStandard\Tool\CodeSniffer\Wrapper;
@@ -74,44 +73,89 @@ private static function getChangedFilesBaseDir() {
7473
*/
7574
public static function getWhitelist($fileTypes = ['php'], $changedFilesBaseDir = '', $baseFilesFolder = '')
7675
{
77-
$globPatternsFolder = self::getBaseFilesFolder();
78-
if ('' !== $baseFilesFolder) {
79-
$globPatternsFolder = $baseFilesFolder;
76+
$changedFiles = self::getChangedFilesList($changedFilesBaseDir);
77+
if (empty($changedFiles)) {
78+
return [];
8079
}
80+
81+
$globPatternsFolder = ('' !== $baseFilesFolder) ? $baseFilesFolder : self::getBaseFilesFolder();
8182
$directoriesToCheck = Files::init()->readLists($globPatternsFolder . '/_files/whitelist/common.txt');
83+
$targetFiles = self::filterFiles($changedFiles, $fileTypes, $directoriesToCheck);
84+
85+
return $targetFiles;
86+
}
8287

88+
private static function getChangedFilesList($changedFilesBaseDir)
89+
{
8390
$changedFiles = [];
91+
8492
$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);
87104
}
105+
88106
array_walk(
89107
$changedFiles,
90108
function (&$file) {
91109
$file = BP . '/' . $file;
92110
}
93111
);
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) {
107140
return true;
108141
}
109142
}
110143
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);
111155
}
112156
);
113157

114-
return $changedFiles;
158+
return $filtered;
115159
}
116160

117161
/**

0 commit comments

Comments
 (0)