Ecco una descrizione del comando Magento 2 utilizzato per verificare la duplicazione del codice.
Il comando per controllare la duplicazione del codice / copia-incolla è di seguito.
php bin/magento dev:tests:run static
Questo comando andrà prima nella dev/tests/staticcartella. Qui puoi vedere il file di dichiarazione phpunit.xml.dist per questa suite di test.
<testsuites>
<testsuite name="Less Static Code Analysis">
<file>testsuite/Magento/Test/Less/LiveCodeTest.php</file>
</testsuite>
<testsuite name="Javascript Static Code Analysis">
<file>testsuite/Magento/Test/Js/LiveCodeTest.php</file>
</testsuite>
<testsuite name="PHP Coding Standard Verification">
<file>testsuite/Magento/Test/Php/LiveCodeTest.php</file>
</testsuite>
<testsuite name="Code Integrity Tests">
<directory>testsuite/Magento/Test/Integrity</directory>
</testsuite>
<testsuite name="Xss Unsafe Output Test">
<file>testsuite/Magento/Test/Php/XssPhtmlTemplateTest.php</file>
</testsuite>
</testsuites>
In questo file troverai il codice sopra che definirà quale file eseguire per diversi test di codice.
Per restringere il campo puoi vedere PHP Coding Standard Verification testsuiteQuesto eseguirà il file testuite / Magento / Test / Php / LiveCodeTest.php
Quando apri questo file, troverai diverse funzioni per verificare la presenza di diversi tipi di problemi di codice. La funzione che verrà eseguita ètestCopyPaste
public function testCopyPaste()
{
$reportFile = self::$reportDir . '/phpcpd_report.xml';
$copyPasteDetector = new CopyPasteDetector($reportFile);
if (!$copyPasteDetector->canRun()) {
$this->markTestSkipped('PHP Copy/Paste Detector is not available.');
}
$blackList = [];
foreach (glob(__DIR__ . '/_files/phpcpd/blacklist/*.txt') as $list) {
$blackList = array_merge($blackList, file($list, FILE_IGNORE_NEW_LINES));
}
$copyPasteDetector->setBlackList($blackList);
$result = $copyPasteDetector->run([BP]);
$output = "";
if (file_exists($reportFile)) {
$output = file_get_contents($reportFile);
}
$this->assertTrue(
$result,
"PHP Copy/Paste Detector has found error(s):" . PHP_EOL . $output
);
}
Qui troverai un codice che verrà utilizzato per inserire nella blacklist qualsiasi file / cartella da questo controllo del codice.
foreach (glob(__DIR__ . '/_files/phpcpd/blacklist/*.txt') as $list) {
$blackList = array_merge($blackList, file($list, FILE_IGNORE_NEW_LINES));
}
Questa foreachfunzione verificherà la presenza di qualsiasi .txtfile aggiunto nella posizione dev / test / static / testsuite / Magento / Test / Php / _files / phpcpd / blacklist . Leggerà il file e ignorerà tutte le cartelle da escludere dal processo di rilevamento del codice copia incolla.
Dopo aver aggiunto tutti i file / cartelle della lista nera al codice, verrà eseguito sotto il codice.
$result = $copyPasteDetector->run([BP]);
Questo codice eseguirà la runfunzione del file dev / tests / static / framework / Magento / TestFramework / CodingStandard / Tool / CopyPasteDetector.php .
public function run(array $whiteList)
{
$blackListStr = ' ';
foreach ($this->blacklist as $file) {
$file = escapeshellarg(trim($file));
if (!$file) {
continue;
}
$blackListStr .= '--exclude ' . $file . ' ';
}
$vendorDir = require BP . '/app/etc/vendor_path.php';
$command = 'php ' . BP . '/' . $vendorDir . '/bin/phpcpd' . ' --log-pmd ' . escapeshellarg(
$this->reportFile
) . ' --names-exclude "*Test.php" --min-lines 13' . $blackListStr . ' ' . implode(' ', $whiteList);
exec($command, $output, $exitCode);
return !(bool)$exitCode;
}
Qui, il codice aggiunge tutte le blacklistedcartelle / i file --excludenell'elenco.
Successivamente eseguirà il vendor/bin/phpcpdcomando.
Qui nel comando stesso ha Magento
escluso tutti i
Testfile per codice
--names-exclude "*Test.php"
Ha anche saltato tutti i duplicati del codice che sono meno di 13 righe per codice
--min-lines 13
L'output per l'esecuzione di questo comando verrà aggiunto al file definito nella testCopyPastefunzione. Il nome file per il rilevamento copia-incolla è phpcpd_report.xml situato nella posizione dev / test / static / report .
Dopo aver eseguito correttamente il comando, l'output verrà aggiunto ai file di report.