Passed
Push — master ( 755472...23808d )
by Adam
03:58
created

ListItemMatchStrategy::isMatch()   C

Complexity

Conditions 11
Paths 10

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 11.1336

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 11
eloc 28
c 1
b 0
f 1
nc 10
nop 2
dl 0
loc 49
ccs 26
cts 29
cp 0.8966
crap 11.1336
rs 5.2653

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Caxy\HtmlDiff\Strategy;
4
5
use Caxy\HtmlDiff\Preprocessor;
6
7
class ListItemMatchStrategy implements MatchStrategyInterface
8
{
9
    /**
10
     * @var int
11
     */
12
    protected $similarityThreshold;
13
14
    /**
15
     * @var float
16
     */
17
    protected $lengthRatioThreshold;
18
19
    /**
20
     * @var float
21
     */
22
    protected $commonTextRatioThreshold;
23
24
    /**
25
     * ListItemMatchStrategy constructor.
26
     *
27
     * @param int   $similarityThreshold
28
     * @param float $lengthRatioThreshold
29
     * @param float $commonTextRatioThreshold
30
     */
31 7
    public function __construct($similarityThreshold = 80, $lengthRatioThreshold = 0.1, $commonTextRatioThreshold = 0.6)
32
    {
33 7
        $this->similarityThreshold = $similarityThreshold;
34 7
        $this->lengthRatioThreshold = $lengthRatioThreshold;
35 7
        $this->commonTextRatioThreshold = $commonTextRatioThreshold;
36 7
    }
37
38
    /**
39
     * @param string $a
40
     * @param string $b
41
     *
42
     * @return bool
43
     */
44 7
    public function isMatch($a, $b)
45
    {
46 7
        $percentage = null;
47
48
        // Strip tags and check similarity
49 7
        $aStripped = strip_tags($a);
50 7
        $bStripped = strip_tags($b);
51 7
        similar_text($aStripped, $bStripped, $percentage);
52
53 7
        if ($percentage >= $this->similarityThreshold) {
54 7
            return true;
55
        }
56
57
        // Check w/o stripped tags
58 6
        similar_text($a, $b, $percentage);
59 6
        if ($percentage >= $this->similarityThreshold) {
60
            return true;
61
        }
62
63
        // Check common prefix/ suffix length
64 6
        $aCleaned = trim($aStripped);
65 6
        $bCleaned = trim($bStripped);
66 6
        if (strlen($aCleaned) === 0 || strlen($bCleaned) === 0) {
67 1
            $aCleaned = $a;
68 1
            $bCleaned = $b;
69 1
        }
70 6
        if (strlen($aCleaned) === 0 || strlen($bCleaned) === 0) {
71
            return false;
72
        }
73 6
        $prefixIndex = Preprocessor::diffCommonPrefix($aCleaned, $bCleaned);
74 6
        $suffixIndex = Preprocessor::diffCommonSuffix($aCleaned, $bCleaned);
75
76
        // Use shorter string, and see how much of it is leftover
77 6
        $len = min(strlen($aCleaned), strlen($bCleaned));
78 6
        $remaining = $len - ($prefixIndex + $suffixIndex);
79 6
        $strLengthPercent = $len / max(strlen($a), strlen($b));
80
81 6
        if ($remaining === 0 && $strLengthPercent > $this->lengthRatioThreshold) {
82
            return true;
83
        }
84
85 6
        $percentCommon = ($prefixIndex + $suffixIndex) / $len;
86
87 6
        if ($strLengthPercent > 0.1 && $percentCommon > $this->commonTextRatioThreshold) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $strLengthPercent...mmonTextRatioThreshold;.
Loading history...
88 2
            return true;
89
        }
90
91 6
        return false;
92
    }
93
}
94