Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Mar 6, 2022
1 parent c159f3d commit 6fb7a31
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 254 deletions.
29 changes: 7 additions & 22 deletions src/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,15 @@

final class Chunk
{
/**
* @var int
*/
private $start;
private int $start;

/**
* @var int
*/
private $startRange;
private int $startRange;

/**
* @var int
*/
private $end;
private int $end;

/**
* @var int
*/
private $endRange;
private int $endRange;

/**
* @var Line[]
*/
private $lines;
private array $lines;

public function __construct(int $start = 0, int $startRange = 1, int $end = 0, int $endRange = 1, array $lines = [])
{
Expand Down Expand Up @@ -66,15 +51,15 @@ public function getEndRange(): int
}

/**
* @return Line[]
* @psalm-return list<Line>
*/
public function getLines(): array
{
return $this->lines;
}

/**
* @param Line[] $lines
* @psalm-param list<Line> $lines
*/
public function setLines(array $lines): void
{
Expand Down
20 changes: 7 additions & 13 deletions src/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,17 @@

final class Diff
{
/**
* @var string
*/
private $from;
private string $from;

/**
* @var string
*/
private $to;
private string $to;

/**
* @var Chunk[]
* @psalm-var list<Chunk>
*/
private $chunks;
private array $chunks;

/**
* @param Chunk[] $chunks
* @psalm-param list<Chunk> $chunks
*/
public function __construct(string $from, string $to, array $chunks = [])
{
Expand All @@ -47,15 +41,15 @@ public function getTo(): string
}

/**
* @return Chunk[]
* @psalm-return list<Chunk>
*/
public function getChunks(): array
{
return $this->chunks;
}

/**
* @param Chunk[] $chunks
* @psalm-param list<Chunk> $chunks
*/
public function setChunks(array $chunks): void
{
Expand Down
85 changes: 8 additions & 77 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@
use function count;
use function current;
use function end;
use function get_class;
use function gettype;
use function is_array;
use function is_object;
use function is_string;
use function key;
use function min;
use function preg_split;
use function prev;
use function reset;
use function sprintf;
use function substr;
use SebastianBergmann\Diff\Output\DiffOutputBuilderInterface;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
Expand All @@ -45,44 +41,14 @@ final class Differ

public const NO_LINE_END_EOF_WARNING = 4;

/**
* @var DiffOutputBuilderInterface
*/
private $outputBuilder;

/**
* @param DiffOutputBuilderInterface $outputBuilder
*
* @throws InvalidArgumentException
*/
public function __construct($outputBuilder = null)
private DiffOutputBuilderInterface|UnifiedDiffOutputBuilder $outputBuilder;

public function __construct(DiffOutputBuilderInterface $outputBuilder)
{
if ($outputBuilder instanceof DiffOutputBuilderInterface) {
$this->outputBuilder = $outputBuilder;
} elseif (null === $outputBuilder) {
$this->outputBuilder = new UnifiedDiffOutputBuilder;
} elseif (is_string($outputBuilder)) {
// PHPUnit 6.1.4, 6.2.0, 6.2.1, 6.2.2, and 6.2.3 support
// @see https://github.com/sebastianbergmann/phpunit/issues/2734#issuecomment-314514056
// @deprecated
$this->outputBuilder = new UnifiedDiffOutputBuilder($outputBuilder);
} else {
throw new InvalidArgumentException(
sprintf(
'Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got %s.',
is_object($outputBuilder) ? 'instance of "' . get_class($outputBuilder) . '"' : gettype($outputBuilder) . ' "' . $outputBuilder . '"'
)
);
}
$this->outputBuilder = $outputBuilder;
}

/**
* Returns the diff between two arrays or strings as string.
*
* @param array|string $from
* @param array|string $to
*/
public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null): string
public function diff(array|string $from, array|string $to, LongestCommonSubsequenceCalculator $lcs = null): string
{
$diff = $this->diffToArray(
$this->normalizeDiffInput($from),
Expand All @@ -93,33 +59,14 @@ public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null)
return $this->outputBuilder->getDiff($diff);
}

/**
* Returns the diff between two arrays or strings as array.
*
* Each array element contains two elements:
* - [0] => mixed $token
* - [1] => 2|1|0
*
* - 2: REMOVED: $token was removed from $from
* - 1: ADDED: $token was added to $from
* - 0: OLD: $token is not changed in $to
*
* @param array|string $from
* @param array|string $to
* @param LongestCommonSubsequenceCalculator $lcs
*/
public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs = null): array
public function diffToArray(array|string $from, array|string $to, LongestCommonSubsequenceCalculator $lcs = null): array
{
if (is_string($from)) {
$from = $this->splitStringByLines($from);
} elseif (!is_array($from)) {
throw new InvalidArgumentException('"from" must be an array or string.');
}

if (is_string($to)) {
$to = $this->splitStringByLines($to);
} elseif (!is_array($to)) {
throw new InvalidArgumentException('"to" must be an array or string.');
}

[$from, $to, $start, $end] = self::getArrayDiffParted($from, $to);
Expand Down Expand Up @@ -172,12 +119,7 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs
return $diff;
}

/**
* Casts variable to string if it is not a string or array.
*
* @return array|string
*/
private function normalizeDiffInput($input)
private function normalizeDiffInput(array|string $input)
{
if (!is_array($input) && !is_string($input)) {
return (string) $input;
Expand All @@ -186,9 +128,6 @@ private function normalizeDiffInput($input)
return $input;
}

/**
* Checks if input is string, if so it will split it line-by-line.
*/
private function splitStringByLines(string $input): array
{
return preg_split('/(.*\R)/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
Expand All @@ -209,21 +148,13 @@ private function selectLcsImplementation(array $from, array $to): LongestCommonS
return new TimeEfficientLongestCommonSubsequenceCalculator;
}

/**
* Calculates the estimated memory footprint for the DP-based method.
*
* @return float|int
*/
private function calculateEstimatedFootprint(array $from, array $to)
private function calculateEstimatedFootprint(array $from, array $to): float|int
{
$itemSize = PHP_INT_SIZE === 4 ? 76 : 144;

return $itemSize * min(count($from), count($to)) ** 2;
}

/**
* Returns true if line ends don't match in a diff.
*/
private function detectUnmatchedLineEndings(array $diff): bool
{
$newLineBreaks = ['' => true];
Expand Down
12 changes: 3 additions & 9 deletions src/Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@ final class Line

public const UNCHANGED = 3;

/**
* @var int
*/
private $type;

/**
* @var string
*/
private $content;
private int $type;

private string $content;

public function __construct(int $type = self::UNCHANGED, string $content = '')
{
Expand Down
5 changes: 1 addition & 4 deletions src/Output/DiffOnlyOutputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
*/
final class DiffOnlyOutputBuilder implements DiffOutputBuilderInterface
{
/**
* @var string
*/
private $header;
private string $header;

public function __construct(string $header = "--- Original\n+++ New\n")
{
Expand Down
25 changes: 8 additions & 17 deletions src/Output/StrictUnifiedDiffOutputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
{
private static $default = [
private static array $default = [
'collapseRanges' => true, // ranges of length one are rendered with the trailing `,1`
'commonLineThreshold' => 6, // number of same lines before ending a new hunk and creating a new one (if needed)
'contextLines' => 3, // like `diff: -u, -U NUM, --unified[=NUM]`, for patch/git apply compatibility best to keep at least @ 3
Expand All @@ -43,30 +43,21 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
'toFileDate' => null,
];

/**
* @var bool
*/
private $changed;
private bool $changed;

/**
* @var bool
*/
private $collapseRanges;
private bool $collapseRanges;

/**
* @var int >= 0
* @psalm-var positive-int
*/
private $commonLineThreshold;
private int $commonLineThreshold;

/**
* @var string
*/
private $header;
private string $header;

/**
* @var int >= 0
* @psalm-var positive-int
*/
private $contextLines;
private int $contextLines;

public function __construct(array $options = [])
{
Expand Down
22 changes: 5 additions & 17 deletions src/Output/UnifiedDiffOutputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,18 @@
*/
final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
{
/**
* @var bool
*/
private $collapseRanges = true;
private bool $collapseRanges = true;

/**
* @var int >= 0
*/
private $commonLineThreshold = 6;
private int $commonLineThreshold = 6;

/**
* @var int >= 0
* @psalm-var positive-int
*/
private $contextLines = 3;

/**
* @var string
*/
private $header;
private string $header;

/**
* @var bool
*/
private $addLineNumbers;
private bool $addLineNumbers;

public function __construct(string $header = "--- Original\n+++ New\n", bool $addLineNumbers = false)
{
Expand Down
5 changes: 1 addition & 4 deletions tests/ChunkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
*/
final class ChunkTest extends TestCase
{
/**
* @var Chunk
*/
private $chunk;
private Chunk $chunk;

protected function setUp(): void
{
Expand Down
Loading

0 comments on commit 6fb7a31

Please sign in to comment.