Passed
Pull Request — master (#10)
by Akpé Aurelle Emmanuel Moïse
02:03
created

complexCommonTextSimilarities   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 44
eloc 72
dl 0
loc 136
rs 8.8798
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A areStems() 0 14 5
A getDomain() 0 3 4
A wordsAddedOrRemoved() 0 10 3
A strippedUrl() 0 8 5
A isUrl() 0 6 5
A punctuationChangesOccured() 0 10 5
A aoeStemming() 0 8 5
A filter() 0 19 6
A waorDiff() 0 3 2
A acronymOrExpanded() 0 11 4

How to fix   Complexity   

Complex Class

Complex classes like complexCommonTextSimilarities often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use complexCommonTextSimilarities, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
*
4
* @Name : similar-text
5
* @Programmer : Akpé Aurelle Emmanuel Moïse Zinsou
6
* @Date : 2019-04-01
7
* @Released under : https://github.com/manuwhat/similar-text/blob/master/LICENSE
8
* @Repository : https://github.com/manuwhat/similar
9
*
10
**/
11
12
13
namespace EZAMA{
14
    
15
    class complexCommonTextSimilarities extends simpleCommonTextSimilarities
16
    {
17
        const URL_FORMAT_EXTENDED_PATTERN = '/^((https?|ftps?|file):\/\/){0,1}'.// protocol
18
                                            '(([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+'.// username
19
                                            '(:([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+)?'.// password
20
                                            '@)?(?#'.// auth requires @
21
                                            ')((([a-z0-9]\.|[a-z0-9][a-z0-9-]*[a-z0-9]\.)*'.// domain segments AND
22
                                            '[a-z][a-z0-9-]*[a-z0-9]'.// top level domain OR
23
                                            '|((\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])\.){3}'.
24
                                            '(\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])'.// IP address
25
                                            ')(:\d+)?'.// port
26
                                            ')(((\/+([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)*'.// path
27
                                            '(\?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)'.// query string
28
                                            '?)?)?'.// path and query string optional
29
                                            '(#([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?'.// fragment
30
                                            '$/i';
31
32
33
34
35
        const URL_POSIX_FORMAT = '"^(\b(https?|ftps?|file):\/\/)?[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#\/%=~_|]$"i';
36
        
37
        protected static function isUrl($url, &$getDomain = '')
38
        {
39
            $matches = array();
40
            $bool = is_string($url) && preg_match('/\./', $url) && preg_match(self::URL_POSIX_FORMAT, $url) && preg_match(self::URL_FORMAT_EXTENDED_PATTERN, $url, $matches);
41
            $getDomain = $bool ? self::getDomain($url, $matches[1]) : '';
42
            return $bool;
43
        }
44
        
45
        protected static function getDomain($url, $match)
46
        {
47
            return ($getDomain = explode('.', parse_url($url, $match ? PHP_URL_HOST : PHP_URL_PATH))) ? (($c = count($getDomain)) > 1 ? ($getDomain[$c - 2]) : '') : '';
48
        }
49
        
50
        public static function strippedUrl($a, $b)
51
        {
52
            if (self::isUrl($a, $domain) && is_string($b)) {
53
                return $domain === trim($b);
54
            } elseif (self::isUrl($b, $domain) && is_string($a)) {
55
                return $domain === trim($a);
56
            } else {
57
                return false;
58
            }
59
        }
60
        public static function areStems($a, $b)
61
        {
62
            if (!is_string($a) || !is_string($b)) {
63
                return false;
64
            }
65
            
66
            $a = self::getParts(self::strtolower($a));
67
            $b = self::getParts(self::strtolower($b));
68
            foreach ((array)$a as $index=>$word) {
69
                if (!self::haveSameRoot($word, $b[$index])) {
70
                    return false;
71
                }
72
            }
73
            return true;
74
        }
75
        
76
        public static function wordsAddedOrRemoved($a, $b)
77
        {
78
            if (!is_string($a) || !is_string($b)) {
79
                return false;
80
            }
81
            $filter = function ($v) {
82
                return !(ctype_space($v));
83
            };
84
            self::filter($a, $b, $filter, true);
85
            return self::waorDiff($a, $b, count($a), count($b));
86
        }
87
        
88
        private static function filter(&$a, &$b, $filter, $insensitive = true, $captureLength=false)
89
        {
90
            if ($insensitive) {
91
                $a = array_filter(self::getParts(self::strtolower($a), $c, $captureLength), $filter);
92
                if ($c===1) {
93
                    $a=self::strtolower($a);
94
                }
95
                $b = array_filter(self::getParts(self::strtolower($b), $c, $captureLength), $filter);
96
                if ($c===1) {
97
                    $b=self::strtolower($b);
98
                }
99
            } else {
100
                $a = array_filter(self::getParts(self::split($a), $c, $captureLength), $filter);
101
                if ($c===1) {
102
                    $a=self::strtolower($a);
103
                }
104
                $b = array_filter(self::getParts(self::split($b), $c, $captureLength), $filter);
105
                if ($c===1) {
106
                    $b=self::strtolower($b);
107
                }
108
            }
109
        }
110
        
111
        private static function waorDiff($a, $b, $ca, $cb)
112
        {
113
            return (bool) (($ca > $cb) ?array_diff_assoc(array_values($a), array_values($b)) : array_diff_assoc(array_values($b), array_values($a)));
114
        }
115
        
116
        
117
        public static function punctuationChangesOccured($a, $b, $insensitive = true, $considerSpace = true)
118
        {
119
            $filter = function ($v) use ($considerSpace) {
120
                return $considerSpace ? !(ctype_space($v) || ctype_punct($v)) : !ctype_punct($v);
121
            };
122
            if (!is_string($a) || !is_string($b)) {
123
                return false;
124
            }
125
            self::filter($a, $b, $filter, $insensitive);
126
            return empty(array_diff($a, $b));
127
        }
128
        
129
        
130
        public static function acronymOrExpanded($a, $b)
131
        {
132
            if (!is_string($a) || !is_string($b)) {
133
                return false;
134
            }
135
            $filter = function ($v) {
136
                return !(ctype_space($v[0]) || ctype_punct($v[0]));
137
            };
138
            
139
            self::filter($a, $b, $filter, true, true);
140
            return self::aoeStemming($a, $b);
141
        }
142
        
143
        private static function aoeStemming($a, $b)
144
        {
145
            foreach ($a as $index=>$word) {
146
                if (!self::haveSameRoot($word[0], $b[$index][0]) || ($a[$index][1]>2 && $b[$index][1]>2)) {
147
                    return false;
148
                }
149
            }
150
            return true;
151
        }
152
    }
153
}
154