From d0cede314d53674b162c8224fd5f3e5b2d491c34 Mon Sep 17 00:00:00 2001 From: DigiLive Date: Thu, 10 Dec 2020 12:01:22 +0100 Subject: [PATCH] Optimize constant usage Instead of defining constants with the same name and values at different namespaces, they're now defined at an interface which can be shared among classes by implementing this interface. --- lib/jblond/Diff.php | 15 ++---------- lib/jblond/Diff/ConstantsInterface.php | 33 ++++++++++++++++++++++++++ lib/jblond/Diff/SequenceMatcher.php | 32 ++++++++++--------------- 3 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 lib/jblond/Diff/ConstantsInterface.php diff --git a/lib/jblond/Diff.php b/lib/jblond/Diff.php index c7b0368..f166ace 100644 --- a/lib/jblond/Diff.php +++ b/lib/jblond/Diff.php @@ -5,6 +5,7 @@ namespace jblond; use InvalidArgumentException; +use jblond\Diff\ConstantsInterface; use jblond\Diff\SequenceMatcher; use jblond\Diff\Similarity; use OutOfRangeException; @@ -26,20 +27,8 @@ * @version 2.3.0 * @link https://github.com/JBlond/php-diff */ -class Diff +class Diff implements ConstantsInterface { - /** - * Flag to disable ignore of successive empty/blank lines. - */ - public const DIFF_IGNORE_LINE_NONE = 0; - /** - * Flag to ignore successive empty lines. - */ - public const DIFF_IGNORE_LINE_EMPTY = 1; - /** - * Flag to ignore successive blank lines. (Lines which contain no or only non printable characters.) - */ - public const DIFF_IGNORE_LINE_BLANK = 2; /** * @var array The first version to compare. * Each element contains a line of this string. diff --git a/lib/jblond/Diff/ConstantsInterface.php b/lib/jblond/Diff/ConstantsInterface.php new file mode 100644 index 0000000..121e7cb --- /dev/null +++ b/lib/jblond/Diff/ConstantsInterface.php @@ -0,0 +1,33 @@ + + * @copyright (c) 2020 Mario Brandt + * @license New BSD License http://www.opensource.org/licenses/bsd-license.php + * @version 2.3.0 + * @link https://github.com/JBlond/php-diff + */ +interface ConstantsInterface +{ + /** + * Flag to disable ignore of successive empty/blank lines. + */ + public const DIFF_IGNORE_LINE_NONE = 0; + /** + * Flag to ignore empty lines. + */ + public const DIFF_IGNORE_LINE_EMPTY = 1; + /** + * Flag to ignore blank lines. (Lines which contain no or only non printable characters.) + */ + public const DIFF_IGNORE_LINE_BLANK = 2; + +} diff --git a/lib/jblond/Diff/SequenceMatcher.php b/lib/jblond/Diff/SequenceMatcher.php index 595f1b4..19ea8fa 100644 --- a/lib/jblond/Diff/SequenceMatcher.php +++ b/lib/jblond/Diff/SequenceMatcher.php @@ -20,20 +20,8 @@ * @version 2.3.0 * @link https://github.com/JBlond/php-diff */ -class SequenceMatcher +class SequenceMatcher implements ConstantsInterface { - /** - * Flag to disable ignore of successive empty/blank lines. - */ - public const DIFF_IGNORE_LINE_NONE = 0; - /** - * Flag to ignore empty lines. - */ - public const DIFF_IGNORE_LINE_EMPTY = 1; - /** - * Flag to ignore blank lines. (Lines which contain no or only non printable characters.) - */ - public const DIFF_IGNORE_LINE_BLANK = 2; /** * @var array The first sequence to compare against. */ @@ -360,12 +348,18 @@ public function getOpCodes(): array $part2 = array_slice($this->new, $j, $bj - $j); if ($this->options['ignoreLines'] == 2) { - array_walk($part1, function (&$line) { - $line = trim($line); - }); - array_walk($part2, function (&$line) { - $line = trim($line); - }); + array_walk( + $part1, + function (&$line) { + $line = trim($line); + } + ); + array_walk( + $part2, + function (&$line) { + $line = trim($line); + } + ); unset($line); }