Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CopyPasteDetector.php
Go to the documentation of this file.
1 <?php
11 
13 
15 {
21  private $reportFile;
22 
28  private $blacklist;
29 
35  public function __construct($reportFile)
36  {
37  $this->reportFile = $reportFile;
38  }
39 
43  public function setBlackList(array $blackList)
44  {
45  $this->blacklist = $blackList;
46  }
47 
55  public function canRun()
56  {
57  exec($this->getCommand() . ' --version', $output, $exitCode);
58  return $exitCode === 0;
59  }
60 
69  public function run(array $whiteList)
70  {
71  $blacklistedDirs = [];
72  $blacklistedFileNames = [];
73  foreach ($this->blacklist as $file) {
74  $file = escapeshellarg(trim($file));
75  if (!$file) {
76  continue;
77  }
78  $ext = pathinfo($file, PATHINFO_EXTENSION);
79  if ($ext != '') {
80  $blacklistedFileNames[] = $file;
81  } else {
82  $blacklistedDirs[] = '--exclude ' . $file . ' ';
83  }
84  }
85 
86  $command = $this->getCommand() . ' --log-pmd ' . escapeshellarg($this->reportFile)
87  . ' --names-exclude ' . join(',', $blacklistedFileNames) . ' --min-lines 13 ' . join(' ', $blacklistedDirs)
88  . ' ' . implode(' ', $whiteList);
89  exec($command, $output, $exitCode);
90 
91  return !(bool)$exitCode;
92  }
93 
99  private function getCommand()
100  {
101  $vendorDir = require BP . '/app/etc/vendor_path.php';
102  return 'php ' . BP . '/' . $vendorDir . '/bin/phpcpd';
103  }
104 }
exec($command, array &$output=null, &$return_var=null)
if(!file_exists(VENDOR_PATH)) $vendorDir
Definition: autoload.php:25
const BP
Definition: autoload.php:14