Skip to content

Commit

Permalink
Optimize constant usage
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DigiLive authored and DigiLive committed Dec 18, 2020
1 parent dabb467 commit 3591515
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
15 changes: 2 additions & 13 deletions lib/jblond/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace jblond;

use InvalidArgumentException;
use jblond\Diff\ConstantsInterface;
use jblond\Diff\SequenceMatcher;
use jblond\Diff\Similarity;
use OutOfRangeException;
Expand All @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions lib/jblond/Diff/ConstantsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace jblond\Diff;
/**
* Constant Interface
*
* Defines the library constants which needs to be shared across the different classes.
*
* PHP version 7.2 or greater
*
* @package jblond
* @author Ferry Cools <[email protected]>
* @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;

}
32 changes: 13 additions & 19 deletions lib/jblond/Diff/SequenceMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 3591515

Please sign in to comment.