Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Nov 16, 2017
1 parent ea9991b commit 222b755
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 148 deletions.
21 changes: 8 additions & 13 deletions src/CLI/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\ArrayInput;

/**
* TextUI frontend for PHPCPD.
*
* @since Class available since Release 2.0.0
*/
class Application extends AbstractApplication
{
public function __construct()
{
$version = new Version('3.0.0', dirname(dirname(__DIR__)));
$version = new Version('3.0.0', \dirname(\dirname(__DIR__)));
parent::__construct('phpcpd', $version->getVersion());
}

Expand Down Expand Up @@ -81,7 +76,7 @@ public function doRun(InputInterface $input, OutputInterface $output)

if (!$input->hasParameterOption('--quiet')) {
$output->write(
sprintf(
\sprintf(
"phpcpd %s by Sebastian Bergmann.\n\n",
$this->getVersion()
)
Expand All @@ -102,15 +97,15 @@ public function doRun(InputInterface $input, OutputInterface $output)

private function disableXdebug()
{
if (!extension_loaded('xdebug')) {
if (!\extension_loaded('xdebug')) {
return;
}

ini_set('xdebug.scream', 0);
ini_set('xdebug.max_nesting_level', 8192);
ini_set('xdebug.show_exception_trace', 0);
ini_set('xdebug.show_error_trace', 0);
\ini_set('xdebug.scream', 0);
\ini_set('xdebug.max_nesting_level', 8192);
\ini_set('xdebug.show_exception_trace', 0);
\ini_set('xdebug.show_error_trace', 0);

xdebug_disable();
\xdebug_disable();
}
}
15 changes: 6 additions & 9 deletions src/CLI/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;

/**
* @since Class available since Release 2.0.0
*/
class Command extends AbstractCommand
{
/**
Expand Down Expand Up @@ -61,7 +58,7 @@ protected function configure()
null,
InputOption::VALUE_REQUIRED,
'A comma-separated list of paths regexps to exclude (example: "#var/.*_tmp#")',
array()
[]
)
->addOption(
'exclude',
Expand Down Expand Up @@ -131,7 +128,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$progressBar = null;

if ($input->getOption('progress')) {
$progressBar = new ProgressBar($output, count($files));
$progressBar = new ProgressBar($output, \count($files));
$progressBar->start();
}

Expand Down Expand Up @@ -169,7 +166,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
print \PHP_Timer::resourceUsage() . "\n";
}

if (count($clones) > 0) {
if (\count($clones) > 0) {
exit(1);
}
}
Expand All @@ -184,9 +181,9 @@ private function handleCSVOption(InputInterface $input, $option)
{
$result = $input->getOption($option);

if (!is_array($result)) {
$result = explode(',', $result);
array_map('trim', $result);
if (!\is_array($result)) {
$result = \explode(',', $result);
\array_map('trim', $result);
}

return $result;
Expand Down
17 changes: 6 additions & 11 deletions src/CodeClone.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@

namespace SebastianBergmann\PHPCPD;

/**
* Represents an exact code clone.
*
* @since Class available since Release 1.1.0
*/
class CodeClone
{
/**
Expand Down Expand Up @@ -57,7 +52,7 @@ public function __construct(CodeCloneFile $fileA, CodeCloneFile $fileB, $size, $

$this->size = $size;
$this->tokens = $tokens;
$this->id = md5($this->getLines());
$this->id = \md5($this->getLines());
}

/**
Expand Down Expand Up @@ -94,16 +89,16 @@ public function getFiles()
public function getLines($indent = '')
{
if (empty($this->lines)) {
$file = current($this->files);
$file = \current($this->files);

$this->lines = implode(
$this->lines = \implode(
'',
array_map(
\array_map(
function ($line) use ($indent) {
return $indent . $line;
},
array_slice(
file($file->getName()),
\array_slice(
\file($file->getName()),
$file->getStartLine() - 1,
$this->size
)
Expand Down
44 changes: 5 additions & 39 deletions src/CodeCloneFile.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,14 @@
<?php
/**
* phpcpd
/*
* This file is part of PHP Copy/Paste Detector (PHPCPD).
*
* Copyright (c) 2009-2015, Sebastian Bergmann <sebastian@phpunit.de>.
* All rights reserved.
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Sebastian Bergmann nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @since File available since Release 1.5.0
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\PHPCPD;

/**
* Represents an exact code clone file.
*
* @since Class available since Release 1.5.0
*/
class CodeCloneFile
{
/**
Expand Down
15 changes: 5 additions & 10 deletions src/CodeCloneMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@

namespace SebastianBergmann\PHPCPD;

/**
* A map of exact clones.
*
* @since Class available since Release 1.1.0
*/
class CodeCloneMap implements \Countable, \Iterator
{
/**
Expand Down Expand Up @@ -67,7 +62,7 @@ public function addClone(CodeClone $clone)
}
}

$this->numberOfDuplicatedLines += $clone->getSize() * (count($clone->getFiles()) - 1);
$this->numberOfDuplicatedLines += $clone->getSize() * (\count($clone->getFiles()) - 1);

foreach ($clone->getFiles() as $file) {
if (!isset($this->filesWithClones[$file->getName()])) {
Expand Down Expand Up @@ -99,7 +94,7 @@ public function getPercentage()
$percent = 100;
}

return sprintf('%01.2F%%', $percent);
return \sprintf('%01.2F%%', $percent);
}

/**
Expand Down Expand Up @@ -127,15 +122,15 @@ public function setNumLines($numLines)
*/
public function count()
{
return count($this->clones);
return \count($this->clones);
}

/**
* @return int
*/
public function getNumberOfFilesWithClones()
{
return count($this->filesWithClones);
return \count($this->filesWithClones);
}

/**
Expand All @@ -161,7 +156,7 @@ public function rewind()
*/
public function valid()
{
return $this->position < count($this->clones);
return $this->position < \count($this->clones);
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Detector/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
use SebastianBergmann\PHPCPD\CodeCloneMap;
use Symfony\Component\Console\Helper\ProgressBar;

/**
* PHPCPD code analyser.
*
* @since Class available since Release 1.0.0
*/
class Detector
{
/**
Expand Down
5 changes: 0 additions & 5 deletions src/Detector/Strategy/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@

use SebastianBergmann\PHPCPD\CodeCloneMap;

/**
* Abstract base class for strategies to detect code clones.
*
* @since Class available since Release 1.4.0
*/
abstract class AbstractStrategy
{
/**
Expand Down
31 changes: 13 additions & 18 deletions src/Detector/Strategy/Default.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
use SebastianBergmann\PHPCPD\CodeCloneFile;
use SebastianBergmann\PHPCPD\CodeCloneMap;

/**
* Default strategy for detecting code clones.
*
* @since Class available since Release 1.4.0
*/
class DefaultStrategy extends AbstractStrategy
{
/**
Expand All @@ -32,24 +27,24 @@ class DefaultStrategy extends AbstractStrategy
*/
public function processFile($file, $minLines, $minTokens, CodeCloneMap $result, $fuzzy = false)
{
$buffer = file_get_contents($file);
$buffer = \file_get_contents($file);
$currentTokenPositions = [];
$currentTokenRealPositions = [];
$currentSignature = '';
$tokens = token_get_all($buffer);
$tokens = \token_get_all($buffer);
$tokenNr = 0;
$lastTokenLine = 0;

$result->setNumLines(
$result->getNumLines() + substr_count($buffer, "\n")
$result->getNumLines() + \substr_count($buffer, "\n")
);

unset($buffer);

foreach (array_keys($tokens) as $key) {
foreach (\array_keys($tokens) as $key) {
$token = $tokens[$key];

if (is_array($token)) {
if (\is_array($token)) {
if (!isset($this->tokensIgnoreList[$token[0]])) {
if ($tokenNr == 0) {
$currentTokenPositions[$tokenNr] = $token[2] - $lastTokenLine;
Expand All @@ -64,15 +59,15 @@ public function processFile($file, $minLines, $minTokens, CodeCloneMap $result,
$token[1] = 'variable';
}

$currentSignature .= chr($token[0] & 255) .
pack('N*', crc32($token[1]));
$currentSignature .= \chr($token[0] & 255) .
\pack('N*', \crc32($token[1]));
}

$lastTokenLine = $token[2];
}
}

$count = count($currentTokenPositions);
$count = \count($currentTokenPositions);
$firstLine = 0;
$firstRealLine = 0;
$found = false;
Expand All @@ -82,9 +77,9 @@ public function processFile($file, $minLines, $minTokens, CodeCloneMap $result,
$line = $currentTokenPositions[$tokenNr];
$realLine = $currentTokenRealPositions[$tokenNr];

$hash = substr(
md5(
substr(
$hash = \substr(
\md5(
\substr(
$currentSignature,
$tokenNr * 5,
$minTokens * 5
Expand Down Expand Up @@ -112,7 +107,7 @@ public function processFile($file, $minLines, $minTokens, CodeCloneMap $result,
$lastLine = $currentTokenPositions[$lastToken];
$lastRealLine = $currentTokenRealPositions[$lastToken];
$numLines = $lastLine + 1 - $firstLine;
$realNumLines = $lastRealLine +1 - $firstRealLine;
$realNumLines = $lastRealLine + 1 - $firstRealLine;

if ($numLines >= $minLines &&
($fileA != $file ||
Expand Down Expand Up @@ -144,7 +139,7 @@ public function processFile($file, $minLines, $minTokens, CodeCloneMap $result,
$lastLine = $currentTokenPositions[$lastToken];
$lastRealLine = $currentTokenRealPositions[$lastToken];
$numLines = $lastLine + 1 - $firstLine;
$realNumLines = $lastRealLine +1 - $firstRealLine;
$realNumLines = $lastRealLine + 1 - $firstRealLine;

if ($numLines >= $minLines &&
($fileA != $file || $firstLineA != $firstRealLine)) {
Expand Down
Loading

0 comments on commit 222b755

Please sign in to comment.