Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
30 | class RequestRemoteAddress implements ICheck { |
||
31 | |||
32 | /** @var IL10N */ |
||
33 | protected $l; |
||
34 | |||
35 | /** @var IRequest */ |
||
36 | protected $request; |
||
37 | |||
38 | /** |
||
39 | * @param IL10N $l |
||
40 | * @param IRequest $request |
||
41 | */ |
||
42 | public function __construct(IL10N $l, IRequest $request) { |
||
46 | |||
47 | /** |
||
48 | * @param IStorage $storage |
||
49 | * @param string $path |
||
50 | */ |
||
51 | public function setFileInfo(IStorage $storage, $path) { |
||
54 | |||
55 | /** |
||
56 | * @param string $operator |
||
57 | * @param string $value |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function executeCheck($operator, $value) { |
||
74 | |||
75 | /** |
||
76 | * @param string $operator |
||
77 | * @param string $value |
||
78 | * @throws \UnexpectedValueException |
||
79 | */ |
||
80 | public function validateCheck($operator, $value) { |
||
81 | if (!in_array($operator, ['matchesIPv4', '!matchesIPv4', 'matchesIPv6', '!matchesIPv6'])) { |
||
82 | throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
||
83 | } |
||
84 | |||
85 | $decodedValue = explode('/', $value); |
||
86 | if (sizeof($decodedValue) !== 2) { |
||
87 | throw new \UnexpectedValueException($this->l->t('The given IP range is invalid'), 2); |
||
88 | } |
||
89 | |||
90 | if (in_array($operator, ['matchesIPv4', '!matchesIPv4'])) { |
||
91 | View Code Duplication | if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
|
|||
92 | throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 3); |
||
93 | } |
||
94 | View Code Duplication | if ($decodedValue[1] > 32 || $decodedValue[1] <= 0) { |
|
95 | throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv4'), 4); |
||
96 | } |
||
97 | } else { |
||
98 | View Code Duplication | if (!filter_var($decodedValue[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
99 | throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 3); |
||
100 | } |
||
101 | View Code Duplication | if ($decodedValue[1] > 128 || $decodedValue[1] <= 0) { |
|
102 | throw new \UnexpectedValueException($this->l->t('The given IP range is not valid for IPv6'), 4); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Based on http://stackoverflow.com/a/594134 |
||
109 | * @param string $ip |
||
110 | * @param string $rangeIp |
||
111 | * @param int $bits |
||
112 | * @return bool |
||
113 | */ |
||
114 | protected function matchIPv4($ip, $rangeIp, $bits) { |
||
120 | |||
121 | /** |
||
122 | * Based on http://stackoverflow.com/a/7951507 |
||
123 | * @param string $ip |
||
124 | * @param string $rangeIp |
||
125 | * @param int $bits |
||
126 | * @return bool |
||
127 | */ |
||
128 | protected function matchIPv6($ip, $rangeIp, $bits) { |
||
139 | |||
140 | /** |
||
141 | * Based on http://stackoverflow.com/a/7951507 |
||
142 | * @param string $packedIp |
||
143 | * @return string |
||
144 | */ |
||
145 | protected function ipv6ToBits($packedIp) { |
||
154 | } |
||
155 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.