/** * @covers PHPCPD_Detector::copyPasteDetection * @covers PHPCPD_Clone::getLines * @dataProvider strategyProvider */ public function testDetectingSimpleClonesWorks($strategy) { $detector = new PHPCPD_Detector(new $strategy()); $clones = $detector->copyPasteDetection(array(TEST_FILES_PATH . 'Html' . DIRECTORY_SEPARATOR . 'testA.tpl', TEST_FILES_PATH . 'Html' . DIRECTORY_SEPARATOR . 'testB.tpl')); $clones = $clones->getClones(); $this->assertEquals(6, count($clones)); $this->assertEquals(TEST_FILES_PATH . 'Html' . DIRECTORY_SEPARATOR . 'testA.tpl', $clones[0]->aFile); $this->assertEquals(71, $clones[0]->aStartLine); $this->assertEquals(TEST_FILES_PATH . 'Html' . DIRECTORY_SEPARATOR . 'testB.tpl', $clones[0]->bFile); $this->assertEquals(71, $clones[0]->bStartLine); $this->assertEquals(39, $clones[0]->size); $this->assertEquals(30, $clones[0]->tokens); $this->assertEquals(TEST_FILES_PATH . 'Html' . DIRECTORY_SEPARATOR . 'testB.tpl', $clones[4]->aFile); $this->assertEquals(1697, $clones[4]->aStartLine); $this->assertEquals(TEST_FILES_PATH . 'Html' . DIRECTORY_SEPARATOR . 'testB.tpl', $clones[4]->bFile); $this->assertEquals(1750, $clones[4]->bStartLine); $this->assertEquals(12, $clones[4]->size); $this->assertEquals(15, $clones[4]->tokens); $this->assertEquals(' public function </span><span class="default">equals</span><span class="keyword">( </span><span class="default">Table </span><span class="keyword">&</span><span class="default">$o </span><span class="keyword">)<br /> {<br /> return </span><span class="default">TRUE</span><span class="keyword">;<br /> }<br /> }<br /> <br /> </span><span class="default">$chair </span><span class="keyword">= new </span><span class="default">Chair</span><span class="keyword">();<br /> </span><span class="default">$table </span><span class="keyword">= new </span><span class="default">Table</span><span class="keyword">();<br /> <br /> echo </span><span class="default">$chair</span><span class="keyword">-></span><span class="default">equals</span><span class="keyword">( </span><span class="default">$table </span><span class="keyword">);<br /> ', $clones[4]->getLines()); }
/** * @covers PHPCPD_Detector::copyPasteDetection * @covers PHPCPD_Clone::getLines * @dataProvider strategyProvider */ public function testDetectingSimpleClonesWorks($strategy) { $detector = new PHPCPD_Detector(new $strategy()); $clones = $detector->copyPasteDetection(array(TEST_FILES_PATH . 'JS' . DIRECTORY_SEPARATOR . 'testA.js', TEST_FILES_PATH . 'JS' . DIRECTORY_SEPARATOR . 'testB.js')); $clones = $clones->getClones(); $this->assertEquals(3, count($clones)); $this->assertEquals(TEST_FILES_PATH . 'JS' . DIRECTORY_SEPARATOR . 'testA.js', $clones[0]->aFile); $this->assertEquals(338, $clones[0]->aStartLine); $this->assertEquals(TEST_FILES_PATH . 'JS' . DIRECTORY_SEPARATOR . 'testB.js', $clones[0]->bFile); $this->assertEquals(22, $clones[0]->bStartLine); $this->assertEquals(39, $clones[0]->size); $this->assertEquals(176, $clones[0]->tokens); $this->assertEquals(TEST_FILES_PATH . 'JS' . DIRECTORY_SEPARATOR . 'testA.js', $clones[2]->aFile); $this->assertEquals(818, $clones[2]->aStartLine); $this->assertEquals(TEST_FILES_PATH . 'JS' . DIRECTORY_SEPARATOR . 'testB.js', $clones[2]->bFile); $this->assertEquals(132, $clones[2]->bStartLine); $this->assertEquals(14, $clones[2]->size); $this->assertEquals(80, $clones[2]->tokens); $this->assertEquals(" });\n\n each(\n ['find', 'children', 'parent', 'parents',\n 'next', 'nextAll', 'prev', 'prevAll',\n 'first', 'last', 'closest', 'siblings',\n 'parentsUntil', 'nextUntil', 'prevUntil'],\n function(i, methodName) {\n\n var pureMethod = jQueryMethods[methodName];\n\n addCheck(methodName, 2, function(selector){\n\n if ( !internal && lint.enabledReports.noElementsFound && !runFunction(pureMethod, arguments, true, this).length ) {\n", $clones[2]->getLines()); }
/** * @covers PHPCPD_Detector::copyPasteDetection * @covers PHPCPD_Clone::getLines * @dataProvider strategyProvider */ public function testDetectingSimpleClonesWorks($strategy) { $detector = new PHPCPD_Detector(new $strategy()); $clones = $detector->copyPasteDetection(array(TEST_FILES_PATH . 'Math.php')); $clones = $clones->getClones(); $this->assertEquals(TEST_FILES_PATH . 'Math.php', $clones[0]->aFile); $this->assertEquals(86, $clones[0]->aStartLine); $this->assertEquals(TEST_FILES_PATH . 'Math.php', $clones[0]->bFile); $this->assertEquals(150, $clones[0]->bStartLine); $this->assertEquals(28, $clones[0]->size); $this->assertEquals(68, $clones[0]->tokens); $this->assertEquals(' public function div($v1, $v2) { $v3 = $v1 / ($v2 + $v1); if ($v3 > 14) { $v4 = 0; for ($i = 0; $i < $v3; $i++) { $v4 += ($v2 * $i); } } $v5 = ($v4 < $v3 ? ($v3 - $v4) : ($v4 - $v3)); $v6 = ($v1 * $v2 * $v3 * $v4 * $v5); $d = array($v1, $v2, $v3, $v4, $v5, $v6); $v7 = 1; for ($i = 0; $i < $v6; $i++) { shuffle( $d ); $v7 = $v7 + $i * end($d); } $v8 = $v7; foreach ( $d as $x ) { $v8 *= $x; ', $clones[0]->getLines()); }
/** * @covers PHPCPD_Detector::copyPasteDetection * @covers PHPCPD_Clone::getLines * @dataProvider strategyProvider */ public function testDetectingSimpleClonesWorks($strategy) { $detector = new PHPCPD_Detector(new $strategy()); $clones = $detector->copyPasteDetection(array(TEST_FILES_PATH . 'CSS' . DIRECTORY_SEPARATOR . 'testA.css', TEST_FILES_PATH . 'CSS' . DIRECTORY_SEPARATOR . 'testB.css')); $clones = $clones->getClones(); $this->assertEquals(5, count($clones)); $this->assertEquals(TEST_FILES_PATH . 'CSS' . DIRECTORY_SEPARATOR . 'testA.css', $clones[0]->aFile); $this->assertEquals(31, $clones[0]->aStartLine); $this->assertEquals(TEST_FILES_PATH . 'CSS' . DIRECTORY_SEPARATOR . 'testA.css', $clones[0]->bFile); $this->assertEquals(64, $clones[0]->bStartLine); $this->assertEquals(2, $clones[0]->size); $this->assertEquals(2, $clones[0]->tokens); $this->assertEquals(TEST_FILES_PATH . 'CSS' . DIRECTORY_SEPARATOR . 'testA.css', $clones[4]->aFile); $this->assertEquals(90, $clones[4]->aStartLine); $this->assertEquals(TEST_FILES_PATH . 'CSS' . DIRECTORY_SEPARATOR . 'testB.css', $clones[4]->bFile); $this->assertEquals(4, $clones[4]->bStartLine); $this->assertEquals(18, $clones[4]->size); $this->assertEquals(51, $clones[4]->tokens); $this->assertEquals('background: #fff url("/icons/ho/background_navi_top.gif") repeat-x 0 0; border-bottom: 1px solid #EBE9EA; border-top: 1px solid #f6f6f6; ', $clones[3]->getLines()); }
/** * Executes PHPCPD against PhingFile or a FileSet * * @return void */ public function main() { if (!isset($this->_file) and count($this->_filesets) == 0) { throw new BuildException("Missing either a nested fileset or attribute 'file' set"); } if (count($this->_formatters) == 0) { // turn legacy format attribute into formatter $fmt = new PHPCPDFormatterElement($this); $fmt->setType($this->_format); $fmt->setUseFile(false); $this->_formatters[] = $fmt; } $this->validateFormatters(); $filesToParse = array(); if ($this->_file instanceof PhingFile) { $filesToParse[] = $this->_file->getPath(); } else { // append any files in filesets foreach ($this->_filesets as $fs) { $files = $fs->getDirectoryScanner($this->project)->getIncludedFiles(); foreach ($files as $filename) { $f = new PhingFile($fs->getDir($this->project), $filename); $filesToParse[] = $f->getAbsolutePath(); } } } $this->log('Processing files...'); $detector = new PHPCPD_Detector(new PHPCPD_Detector_Strategy_Default()); $clones = $detector->copyPasteDetection($filesToParse, $this->_minLines, $this->_minTokens); $this->log('Finished copy/paste detection'); foreach ($this->_formatters as $fe) { $formatter = $fe->getFormatter(); $formatter->processClones($clones, $this->project, $fe->getUseFile(), $fe->getOutfile()); } }
/** * Checks the extension with php copy and paste detector */ protected function _checkWithPhpCopyPasteDetector() { $facade = new \File_Iterator_Facade(); $files = $facade->getFilesAsArray($this->_extensionPath); $strategy = new \PHPCPD_Detector_Strategy_Default(); $detector = new \PHPCPD_Detector($strategy, null); try { $clones = $detector->copyPasteDetection($files, self::PHP_CPD_MIN_LINES, self::PHP_CPD_MIN_TOKENS); } catch (\Exception $e) { return; } $this->_addIssue(array('type' => 'duplicated_code', 'files' => $clones->getFilesWithClones(), 'comment' => round((double) $clones->getPercentage(), 2), 'occurrences' => $clones->count())); }
/** * Main method. */ public static function main() { $input = new ezcConsoleInput(); $output = new ezcConsoleOutput(); $input->registerOption(new ezcConsoleOption('', 'exclude', ezcConsoleInput::TYPE_STRING, array(), TRUE)); $input->registerOption(new ezcConsoleOption('h', 'help', ezcConsoleInput::TYPE_NONE, NULL, FALSE, '', '', array(), array(), FALSE, FALSE, TRUE)); $input->registerOption(new ezcConsoleOption('', 'log-pmd', ezcConsoleInput::TYPE_STRING)); $input->registerOption(new ezcConsoleOption('', 'min-lines', ezcConsoleInput::TYPE_INT, 5)); $input->registerOption(new ezcConsoleOption('', 'min-tokens', ezcConsoleInput::TYPE_INT, 70)); $input->registerOption(new ezcConsoleOption('', 'save-memory', ezcConsoleInput::TYPE_NONE, NULL, FALSE)); $input->registerOption(new ezcConsoleOption('', 'suffixes', ezcConsoleInput::TYPE_STRING, 'php', FALSE)); $input->registerOption(new ezcConsoleOption('', 'quiet', ezcConsoleInput::TYPE_NONE, NULL, FALSE)); $input->registerOption(new ezcConsoleOption('v', 'version', ezcConsoleInput::TYPE_NONE, NULL, FALSE, '', '', array(), array(), FALSE, FALSE, TRUE)); $input->registerOption(new ezcConsoleOption('', 'verbose', ezcConsoleInput::TYPE_NONE)); try { $input->process(); } catch (ezcConsoleOptionException $e) { print $e->getMessage() . PHP_EOL; exit(1); } if ($input->getOption('help')->value) { self::showHelp(); exit(0); } else { if ($input->getOption('version')->value) { self::printVersionString(); exit(0); } } $arguments = $input->getArguments(); $exclude = $input->getOption('exclude')->value; if (is_array($exclude) && count($exclude) == 1) { array_map('trim', explode(',', array_pop($exclude))); } $logPmd = $input->getOption('log-pmd')->value; $minLines = $input->getOption('min-lines')->value; $minTokens = $input->getOption('min-tokens')->value; $saveMemory = $input->getOption('save-memory')->value; $suffixes = array_map('trim', explode(',', $input->getOption('suffixes')->value)); $quiet = $input->getOption('quiet')->value; if ($input->getOption('verbose')->value !== FALSE) { $verbose = $output; } else { $verbose = NULL; } if (!empty($arguments)) { $facade = new File_Iterator_Facade(); $result = $facade->getFilesAsArray($arguments, $suffixes, array(), $exclude, TRUE); $files = $result['files']; $commonPath = $result['commonPath']; unset($result); } else { self::showHelp(); exit(1); } if (empty($files)) { self::showError('No files found to scan.' . PHP_EOL); } self::printVersionString(); if (!$saveMemory) { $strategy = new PHPCPD_Detector_Strategy_Default(); } else { $strategy = new PHPCPD_Detector_Strategy_SaveMemory(); } $detector = new PHPCPD_Detector($strategy, $verbose); $clones = $detector->copyPasteDetection($files, $minLines, $minTokens); $printer = new PHPCPD_TextUI_ResultPrinter(); $printer->printResult($clones, $commonPath, !$quiet); unset($printer); if ($logPmd) { $pmd = new PHPCPD_Log_XML_PMD($logPmd); $pmd->processClones($clones); unset($pmd); } if (count($clones) > 0) { exit(1); } }