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 |
||
14 | class Visit extends Model |
||
15 | { |
||
16 | use Timestamp; |
||
17 | |||
18 | /** |
||
19 | * The id of the visiting user |
||
20 | * @var int |
||
21 | */ |
||
22 | protected $player; |
||
23 | |||
24 | /** |
||
25 | * The ip of the visiting user |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $ip; |
||
29 | |||
30 | /** |
||
31 | * The host of the visiting user |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $host; |
||
35 | |||
36 | /** |
||
37 | * The user agent of the visiting user |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $user_agent; |
||
41 | |||
42 | /** |
||
43 | * The HTTP_REFERER of the visiting user |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $referer; |
||
47 | |||
48 | /** |
||
49 | * The name of the database table used for queries |
||
50 | */ |
||
51 | const TABLE = "visits"; |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | View Code Duplication | protected function assignResult($visit) |
|
|
|||
57 | { |
||
58 | $this->player = $visit['player']; |
||
59 | $this->ip = $visit['ip']; |
||
60 | $this->host = $visit['host']; |
||
61 | $this->user_agent = $visit['user_agent']; |
||
62 | $this->referer = $visit['referer']; |
||
63 | $this->timestamp = TimeDate::fromMysql($visit['timestamp']); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Enter a new visit into the database |
||
68 | * @param int $visitor The visitor's id |
||
69 | * @param string $ip The visitor's ip address |
||
70 | * @param string $host The visitor's host |
||
71 | * @param string $user_agent The visitor's user agent |
||
72 | * @param string $referrer The HTTP_REFERRER of the visit |
||
73 | * @param string $timestamp The timestamp of the visit |
||
74 | * @return Visit An object representing the visit that was just entered |
||
75 | */ |
||
76 | public static function enterVisit($visitor, $ip, $host, $user_agent, $referrer, $timestamp = "now") |
||
89 | |||
90 | /** |
||
91 | * Get a query builder for players |
||
92 | * @return QueryBuilder |
||
93 | */ |
||
94 | public static function getQueryBuilder() |
||
104 | |||
105 | /** |
||
106 | * Get the visiting player |
||
107 | * @return Player |
||
108 | */ |
||
109 | public function getPlayer() |
||
113 | |||
114 | /** |
||
115 | * Get the IP address of the player |
||
116 | * @return string |
||
117 | */ |
||
118 | public function getIpAddress() |
||
122 | |||
123 | /** |
||
124 | * Get the visiting host |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getHost() |
||
131 | |||
132 | /** |
||
133 | * Get the visitor's user agent |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getUserAgent() |
||
140 | |||
141 | /** |
||
142 | * Get the visitor's referer or referrer |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getReferrer() |
||
149 | } |
||
150 |
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.