Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 25 additions & 52 deletions src/CLI/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,37 @@

final class Arguments
{
/**
* @psalm-var list<string>
*/
private array $directories;

/**
* @psalm-var list<string>
*/
private array $suffixes;

/**
* @psalm-var list<string>
*/
private array $exclude;

private ?string $pmdCpdXmlLogfile;

private bool $githubLogOutput;

private int $linesThreshold;

private int $tokensThreshold;

private bool $fuzzy;

private bool $verbose;

private bool $help;

private bool $version;

private ?string $algorithm;

private int $editDistance;

private int $headEquality;

/**
* @param list<string> $directories
* @param list<string> $suffixes
* @param list<string> $exclude
*/
public function __construct(array $directories, array $suffixes, array $exclude, ?string $pmdCpdXmlLogfile, bool $githubLogOutput, int $linesThreshold, int $tokensThreshold, bool $fuzzy, bool $verbose, bool $help, bool $version, ?string $algorithm, int $editDistance, int $headEquality)
public function __construct(
/**
* @psalm-var list<string>
*/
private readonly array $directories,
/**
* @psalm-var list<string>
*/
private readonly array $suffixes,
/**
* @psalm-var list<string>
*/
private readonly array $exclude,
private readonly ?string $pmdCpdXmlLogfile,
private readonly bool $githubLogOutput,
private readonly int $linesThreshold,
private readonly int $tokensThreshold,
private readonly bool $fuzzy,
private readonly bool $verbose,
private readonly bool $help,
private readonly bool $version,
private readonly ?string $algorithm,
private readonly int $editDistance,
private readonly int $headEquality
)
{
$this->directories = $directories;
$this->suffixes = $suffixes;
$this->exclude = $exclude;
$this->pmdCpdXmlLogfile = $pmdCpdXmlLogfile;
$this->githubLogOutput = $githubLogOutput;
$this->linesThreshold = $linesThreshold;
$this->tokensThreshold = $tokensThreshold;
$this->fuzzy = $fuzzy;
$this->verbose = $verbose;
$this->help = $help;
$this->version = $version;
$this->algorithm = $algorithm;
$this->editDistance = $editDistance;
$this->headEquality = $headEquality;
}

/**
Expand Down
15 changes: 3 additions & 12 deletions src/CodeClone.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,19 @@

final class CodeClone
{
private int $numberOfLines;

private int $numberOfTokens;

/**
* @psalm-var array<string,CodeCloneFile>
*/
private array $files = [];

private string $id;
private readonly string $id;

private string $lines = '';

public function __construct(CodeCloneFile $fileA, CodeCloneFile $fileB, int $numberOfLines, int $numberOfTokens)
public function __construct(CodeCloneFile $fileA, CodeCloneFile $fileB, private readonly int $numberOfLines, private readonly int $numberOfTokens)
{
$this->add($fileA);
$this->add($fileB);

$this->numberOfLines = $numberOfLines;
$this->numberOfTokens = $numberOfTokens;
$this->id = md5($this->lines());
}

Expand Down Expand Up @@ -63,9 +56,7 @@ public function lines(string $indent = ''): string
$this->lines = implode(
'',
array_map(
static function (string $line) use ($indent): string {
return $indent.$line;
},
static fn(string $line): string => $indent.$line,
\array_slice(
file($file->name()),
$file->startLine() - 1,
Expand Down
10 changes: 2 additions & 8 deletions src/CodeCloneFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@

final class CodeCloneFile
{
private string $id;
private readonly string $id;

private string $name;

private int $startLine;

public function __construct(string $name, int $startLine)
public function __construct(private readonly string $name, private readonly int $startLine)
{
$this->name = $name;
$this->startLine = $startLine;
$this->id = $this->name.':'.$this->startLine;
}

Expand Down
4 changes: 1 addition & 3 deletions src/CodeCloneMapIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public function __construct(CodeCloneMap $clones)

usort(
$this->clones,
static function (CodeClone $a, CodeClone $b): int {
return $a->numberOfLines() <=> $b->numberOfLines();
}
static fn(CodeClone $a, CodeClone $b): int => $a->numberOfLines() <=> $b->numberOfLines()
);

$this->clones = array_reverse($this->clones);
Expand Down
5 changes: 1 addition & 4 deletions src/Detector/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@

final class Detector
{
private AbstractStrategy $strategy;

public function __construct(AbstractStrategy $strategy)
public function __construct(private readonly AbstractStrategy $strategy)
{
$this->strategy = $strategy;
}

public function copyPasteDetection(Finder $files): CodeCloneMap
Expand Down
5 changes: 1 addition & 4 deletions src/Detector/Strategy/AbstractStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ abstract class AbstractStrategy
\T_NS_SEPARATOR => true,
];

protected StrategyConfiguration $config;

public function __construct(StrategyConfiguration $config)
public function __construct(protected StrategyConfiguration $config)
{
$this->config = $config;
}

public function setConfig(StrategyConfiguration $config): void
Expand Down
10 changes: 5 additions & 5 deletions src/Detector/Strategy/StrategyConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

final class StrategyConfiguration
{
private int $minLines;
private readonly int $minLines;

private int $minTokens;
private readonly int $minTokens;

private int $editDistance;
private readonly int $editDistance;

private int $headEquality;
private readonly int $headEquality;

private bool $fuzzy;
private readonly bool $fuzzy;

public function __construct(Arguments $arguments)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Detector/Strategy/SuffixTree/AbstractToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace SebastianBergmann\PHPCPD\Detector\Strategy\SuffixTree;

abstract class AbstractToken
abstract class AbstractToken implements \Stringable
{
/** @var int */
public $tokenCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ public function findClones(int $minLength, int $maxErrors, int $headEquality): a

/** @var CloneInfo[] $values */
$values = array_values($map);
usort($values, static function (CloneInfo $a, CloneInfo $b): int {
return $b->length - $a->length;
});
usort($values, static fn(CloneInfo $a, CloneInfo $b): int => $b->length - $a->length);

return $values;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Detector/Strategy/SuffixTree/CloneInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,14 @@ class CloneInfo
*/
public $otherClones;

/**
/** Constructor. */
public function __construct(int $length, int $position, /**
* Number of occurrences of the clone.
*/
private int $occurrences;

/** Constructor. */
public function __construct(int $length, int $position, int $occurrences, AbstractToken $token, PairList $otherClones)
private readonly int $occurrences, AbstractToken $token, PairList $otherClones)
{
$this->length = $length;
$this->position = $position;
$this->occurrences = $occurrences;
$this->token = $token;
$this->otherClones = $otherClones;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Detector/Strategy/SuffixTree/Sentinel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class Sentinel extends AbstractToken
{
/** @var int The hash value used. */
private int $hash;
private readonly int $hash;

public function __construct()
{
Expand Down
13 changes: 4 additions & 9 deletions src/Detector/Strategy/SuffixTree/SuffixTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ class SuffixTree
*/
protected int $INFTY;

/**
* The word we are working on.
*
* @var AbstractToken[]
*/
protected $word;

/**
* The number of nodes created so far.
*
Expand Down Expand Up @@ -151,9 +144,11 @@ class SuffixTree
*
* @param AbstractToken[] $word
*/
public function __construct($word)
public function __construct(/**
* The word we are working on.
*/
protected $word)
{
$this->word = $word;
$size = \count($word);
$this->INFTY = $size;

Expand Down
6 changes: 3 additions & 3 deletions src/Detector/Strategy/SuffixTree/SuffixTreeHashTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ class SuffixTreeHashTable
*/
private array $allowedSizes = [53, 97, 193, 389, 769, 1543,
3079, 6151, 12289, 24593, 49157, 98317, 196613, 393241, 786433,
1572869, 3145739, 6291469, 12582917, 25165843, 50331653, 100663319,
201326611, 402653189, 805306457, 1610612741, ];
1_572_869, 3_145_739, 6_291_469, 12_582_917, 25_165_843, 50_331_653, 100_663_319,
201_326_611, 402_653_189, 805_306_457, 1_610_612_741, ];

/**
* The size of the hash table.
*/
private int $tableSize;
private readonly int $tableSize;

/**
* Storage space for the node part of the key.
Expand Down
6 changes: 1 addition & 5 deletions src/Log/AbstractXmlLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ abstract class AbstractXmlLogger
{
protected \DOMDocument $document;

private string $filename;

public function __construct(string $filename)
public function __construct(private readonly string $filename)
{
$this->document = new \DOMDocument('1.0', 'UTF-8');
$this->document->formatOutput = true;

$this->filename = $filename;
}

abstract public function processClones(CodeCloneMap $clones): void;
Expand Down