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 |
||
29 | class SimpleSoapServer |
||
30 | { |
||
31 | /** |
||
32 | * @WebMethod |
||
33 | * @param string $name |
||
34 | * @param int $age |
||
35 | * @return string $nameWithAge |
||
36 | */ |
||
37 | public function getNameWithAge($name, $age) |
||
38 | { |
||
39 | return 'Your name is: ' . $name . ' and you have ' . $age . ' years old'; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @WebMethod |
||
44 | * @param string[] $names |
||
45 | * @return string $userNames |
||
46 | */ |
||
47 | public function getNameForUsers($names) |
||
48 | { |
||
49 | //FIXME correct array of $names |
||
50 | return 'User names: ' . implode(', ', $names); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @WebMethod |
||
55 | * @param int $max |
||
56 | * @return string[] $count |
||
57 | */ |
||
58 | public function countTo($max) |
||
59 | { |
||
60 | //FIXME incorrect structure of response |
||
61 | $array = array(); |
||
62 | for ($i = 0; $i < $max; $i++) { |
||
63 | $array[] = 'Number: ' . ($i + 1); |
||
64 | } |
||
65 | return $array; |
||
66 | } |
||
67 | } |