Skip to content

Commit

Permalink
Release of new version 6.10.16
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcherng committed Mar 7, 2024
1 parent 297d6a9 commit b136ee3
Show file tree
Hide file tree
Showing 36 changed files with 181 additions and 233 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG/CHANGELOG_v6.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
## VERSION 6 UNIFIED

* Version **6.10** - feat: add methods to get diff statistics
* 2024-03-08 02:00 **6.10.16** fix Differ::getStatistics()
* 297d6a9 fix: Differ::getStatistics() not working when no difference
* 2024-03-05 17:05 **6.10.15** new differ option: fullContextIfIdentical
* 42dd214 chore: fix php-cs-fixer deprecated options
* 239c68f feat: add new differ option: fullContextIfIdentical (#79)
Expand Down
4 changes: 2 additions & 2 deletions example/demo_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// the two sample files for comparison
$oldFile = __DIR__ . '/old_file.txt';
$newFile = __DIR__ . '/new_file.txt';
$oldString = \file_get_contents($oldFile);
$newString = \file_get_contents($newFile);
$oldString = file_get_contents($oldFile);
$newString = file_get_contents($newFile);

// options for Diff class
$diffOptions = [
Expand Down
39 changes: 20 additions & 19 deletions src/DiffHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function getProjectDirectory(): string
{
static $path;

return $path = $path ?? \realpath(__DIR__ . '/..');
return $path = $path ?? realpath(__DIR__ . '/..');
}

/**
Expand All @@ -37,31 +37,31 @@ public static function getRenderersInfo(): array
return $info;
}

$glob = \implode(\DIRECTORY_SEPARATOR, [
static::getProjectDirectory(),
$glob = implode(\DIRECTORY_SEPARATOR, [
self::getProjectDirectory(),
'src',
'Renderer',
'{' . \implode(',', RendererConstant::RENDERER_TYPES) . '}',
'{' . implode(',', RendererConstant::RENDERER_TYPES) . '}',
'*.php',
]);

$fileNames = \array_map(
$fileNames = array_map(
// get basename without file extension
function (string $file): string {
return \pathinfo($file, \PATHINFO_FILENAME);
static function (string $file): string {
return pathinfo($file, \PATHINFO_FILENAME);
},
// paths of all Renderer files
\glob($glob, \GLOB_BRACE)
glob($glob, \GLOB_BRACE)
);

$renderers = \array_filter(
$renderers = array_filter(
$fileNames,
// only normal class files are wanted
function (string $fileName): bool {
static function (string $fileName): bool {
return
\substr($fileName, 0, 8) !== 'Abstract'
&& \substr($fileName, -9) !== 'Interface'
&& \substr($fileName, -5) !== 'Trait';
substr($fileName, 0, 8) !== 'Abstract'
&& substr($fileName, -9) !== 'Interface'
&& substr($fileName, -5) !== 'Trait';
}
);

Expand All @@ -80,7 +80,7 @@ function (string $fileName): bool {
*/
public static function getAvailableRenderers(): array
{
return \array_keys(self::getRenderersInfo());
return array_keys(self::getRenderersInfo());
}

/**
Expand All @@ -97,7 +97,7 @@ public static function getStyleSheet(): string
return $fileContent;
}

$filePath = static::getProjectDirectory() . '/example/diff-table.css';
$filePath = self::getProjectDirectory() . '/example/diff-table.css';

$file = new \SplFileObject($filePath, 'r');

Expand Down Expand Up @@ -133,16 +133,17 @@ public static function calculate(
array $rendererOptions = []
): string {
// always convert into array form
\is_string($old) && ($old = \explode("\n", $old));
\is_string($new) && ($new = \explode("\n", $new));
\is_string($old) && ($old = explode("\n", $old));
\is_string($new) && ($new = explode("\n", $new));

return RendererFactory::getInstance($renderer)
->setOptions($rendererOptions)
->render(
Differ::getInstance()
->setOldNew($old, $new)
->setOptions($differOptions)
);
)
;
}

/**
Expand Down Expand Up @@ -172,7 +173,7 @@ public static function calculateFiles(
$oldFile = new \SplFileObject($old, 'r');
$newFile = new \SplFileObject($new, 'r');

return static::calculate(
return self::calculate(
// fread() requires the length > 0 hence we plus 1 for empty files
$oldFile->fread($oldFile->getSize() + 1),
$newFile->fread($newFile->getSize() + 1),
Expand Down
16 changes: 9 additions & 7 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function setNew(array $new): self
*/
public function setOptions(array $options): self
{
$mergedOptions = $options + static::$defaultOptions;
$mergedOptions = $options + self::$defaultOptions;

if ($this->options !== $mergedOptions) {
$this->options = $mergedOptions;
Expand Down Expand Up @@ -268,7 +268,7 @@ public static function getInstance(): self
{
static $singleton;

return $singleton = $singleton ?? new static([], []);
return $singleton = $singleton ?? new self([], []);
}

/**
Expand Down Expand Up @@ -334,7 +334,8 @@ public function getGroupedOpcodes(): array
} else {
$opcodes = $this->sequenceMatcher
->setSequences($old, $new)
->getGroupedOpcodes($this->options['context']);
->getGroupedOpcodes($this->options['context'])
;
}

$this->getGroupedOpcodesPost($opcodes);
Expand Down Expand Up @@ -369,7 +370,8 @@ public function getGroupedOpcodesGnu(): array
} else {
$opcodes = $this->sequenceMatcher
->setSequences($old, $new)
->getGroupedOpcodes($this->options['context']);
->getGroupedOpcodes($this->options['context'])
;
}

$this->getGroupedOpcodesGnuPost($opcodes);
Expand All @@ -394,10 +396,10 @@ private function getGroupedOpcodesPre(array &$old, array &$new): void
];

$this->oldSrcLength = \count($old);
\array_push($old, ...$eolAtEofHelperLines);
array_push($old, ...$eolAtEofHelperLines);

$this->newSrcLength = \count($new);
\array_push($new, ...$eolAtEofHelperLines);
array_push($new, ...$eolAtEofHelperLines);
}

/**
Expand Down Expand Up @@ -517,7 +519,7 @@ private function finalize(): self
*/
private function resetCachedResults(): self
{
foreach (static::CACHED_PROPERTIES as $property => $value) {
foreach (self::CACHED_PROPERTIES as $property => $value) {
$this->{$property} = $value;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Exception/FileNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class FileNotFoundException extends \Exception
{
public function __construct(string $filepath = '', int $code = 0, \Throwable $previous = null)
public function __construct(string $filepath = '', int $code = 0, ?\Throwable $previous = null)
{
parent::__construct("File not found: {$filepath}", $code, $previous);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/UnsupportedFunctionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class UnsupportedFunctionException extends \Exception
{
public function __construct(string $funcName = '', int $code = 0, \Throwable $previous = null)
public function __construct(string $funcName = '', int $code = 0, ?\Throwable $previous = null)
{
parent::__construct("Unsupported function: {$funcName}", $code, $previous);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Factory/LineRendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public static function getInstance(string $type, ...$ctorArgs): AbstractLineRend
*/
public static function make(string $type, ...$ctorArgs): AbstractLineRenderer
{
$className = RendererConstant::RENDERER_NAMESPACE . '\\Html\\LineRenderer\\' . \ucfirst($type);
$className = RendererConstant::RENDERER_NAMESPACE . '\\Html\\LineRenderer\\' . ucfirst($type);

if (!\class_exists($className)) {
if (!class_exists($className)) {
throw new \InvalidArgumentException("LineRenderer not found: {$type}");
}

Expand Down
2 changes: 1 addition & 1 deletion src/Factory/RendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function resolveRenderer(string $renderer): ?string
foreach (RendererConstant::RENDERER_TYPES as $type) {
$className = RendererConstant::RENDERER_NAMESPACE . "\\{$type}\\{$renderer}";

if (\class_exists($className)) {
if (class_exists($className)) {
return $cache[$renderer] = $className;
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ public function getOptions(): array
}

/**
* {@inheritdoc}
*
* @final
*
* @todo mark this method with "final" in the next major release
Expand All @@ -176,21 +174,16 @@ public function getResultForIdenticals(): string
*/
abstract public function getResultForIdenticalsDefault(): string;

/**
* {@inheritdoc}
*/
final public function render(Differ $differ): string
{
$this->changesAreRaw = true;

// the "no difference" situation may happen frequently
return $differ->getOldNewComparison() === 0 && !$differ->options['fullContextIfIdentical']
? $this->getResultForIdenticals()
: $this->renderWorker($differ);
}

/**
* {@inheritdoc}
*/
final public function renderArray(array $differArray): string
{
$this->changesAreRaw = false;
Expand Down Expand Up @@ -241,6 +234,6 @@ protected function _(string $text, bool $escapeHtml = true): string
{
$text = $this->t->translate($text);

return $escapeHtml ? \htmlspecialchars($text) : $text;
return $escapeHtml ? htmlspecialchars($text) : $text;
}
}
33 changes: 12 additions & 21 deletions src/Renderer/Html/AbstractHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ abstract class AbstractHtml extends AbstractRenderer
*/
public const AUTO_FORMAT_CHANGES = true;

/**
* {@inheritdoc}
*/
public function getResultForIdenticalsDefault(): string
{
return '';
Expand Down Expand Up @@ -105,19 +102,13 @@ public function getChanges(Differ $differ): array
return $changes;
}

/**
* {@inheritdoc}
*/
protected function renderWorker(Differ $differ): string
{
$rendered = $this->redererChanges($this->getChanges($differ));

return $this->cleanUpDummyHtmlClosures($rendered);
}

/**
* {@inheritdoc}
*/
protected function renderArrayWorker(array $differArray): string
{
$this->ensureChangesUseIntTag($differArray);
Expand Down Expand Up @@ -199,7 +190,7 @@ final protected function formatChanges(array &$changes): void

/** @phan-suppress-next-line PhanTypeInvalidLeftOperandOfBitwiseOp */
if ($block['tag'] & (SequenceMatcher::OP_REP | SequenceMatcher::OP_DEL)) {
$block['old']['lines'] = \str_replace(
$block['old']['lines'] = str_replace(
RendererConstant::HTML_CLOSURES,
RendererConstant::HTML_CLOSURES_DEL,
$block['old']['lines']
Expand All @@ -208,7 +199,7 @@ final protected function formatChanges(array &$changes): void

/** @phan-suppress-next-line PhanTypeInvalidLeftOperandOfBitwiseOp */
if ($block['tag'] & (SequenceMatcher::OP_REP | SequenceMatcher::OP_INS)) {
$block['new']['lines'] = \str_replace(
$block['new']['lines'] = str_replace(
RendererConstant::HTML_CLOSURES,
RendererConstant::HTML_CLOSURES_INS,
$block['new']['lines']
Expand All @@ -232,10 +223,10 @@ protected function formatLines(array $lines): array
* we can glue lines into a string and call functions for one time.
* After that, we split the string back into lines.
*/
return \explode(
return explode(
RendererConstant::IMPLODE_DELIMITER,
$this->formatStringFromLines(
\implode(
implode(
RendererConstant::IMPLODE_DELIMITER,
$lines
)
Expand Down Expand Up @@ -288,16 +279,16 @@ protected function expandTabs(string $string, int $tabSize = 4, bool $onlyLeadin
}

if ($onlyLeadingTabs) {
return \preg_replace_callback(
return preg_replace_callback(
"/^[ \t]{1,}/mS", // tabs and spaces may be mixed
function (array $matches) use ($tabSize): string {
return \str_replace("\t", \str_repeat(' ', $tabSize), $matches[0]);
static function (array $matches) use ($tabSize): string {
return str_replace("\t", str_repeat(' ', $tabSize), $matches[0]);
},
$string
);
}

return \str_replace("\t", \str_repeat(' ', $tabSize), $string);
return str_replace("\t", str_repeat(' ', $tabSize), $string);
}

/**
Expand All @@ -309,7 +300,7 @@ function (array $matches) use ($tabSize): string {
*/
protected function htmlSafe(string $string): string
{
return \htmlspecialchars($string, \ENT_NOQUOTES, 'UTF-8');
return htmlspecialchars($string, \ENT_NOQUOTES, 'UTF-8');
}

/**
Expand All @@ -321,7 +312,7 @@ protected function htmlSafe(string $string): string
*/
protected function htmlFixSpaces(string $string): string
{
return \str_replace(' ', ' ', $string);
return str_replace(' ', ' ', $string);
}

/**
Expand All @@ -333,7 +324,7 @@ protected function htmlFixSpaces(string $string): string
*/
protected function htmlReplaceSpacesToHtmlTag(string $string): string
{
return \strtr($string, [
return strtr($string, [
' ' => '<span class="ch sp"> </span>',
"\t" => "<span class=\"ch tab\">\t</span>",
]);
Expand Down Expand Up @@ -367,7 +358,7 @@ protected function ensureChangesUseIntTag(array &$changes): void
*/
protected function cleanUpDummyHtmlClosures(string $string): string
{
return \str_replace(
return str_replace(
[
RendererConstant::HTML_CLOSURES_DEL[0] . RendererConstant::HTML_CLOSURES_DEL[1],
RendererConstant::HTML_CLOSURES_INS[0] . RendererConstant::HTML_CLOSURES_INS[1],
Expand Down
Loading

0 comments on commit b136ee3

Please sign in to comment.