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 |
||
9 | class MiddleNamesInflection extends \morphos\NamesInflection implements Cases |
||
10 | { |
||
11 | use RussianLanguage, CasesHelper; |
||
12 | |||
13 | /** |
||
14 | * @param $name |
||
15 | * @return null|string |
||
16 | */ |
||
17 | 5 | public static function detectGender($name) |
|
28 | |||
29 | /** |
||
30 | * @param $name |
||
31 | * @param null $gender |
||
32 | * @return bool |
||
33 | */ |
||
34 | 2 | public static function isMutable($name, $gender = null) |
|
44 | |||
45 | /** |
||
46 | * @param $name |
||
47 | * @param $case |
||
48 | * @param null $gender |
||
49 | * @return mixed |
||
50 | * @throws \Exception |
||
51 | */ |
||
52 | 9 | public static function getCase($name, $case, $gender = null) |
|
58 | |||
59 | /** |
||
60 | * @param $name |
||
61 | * @param null $gender |
||
62 | * @return array |
||
63 | */ |
||
64 | 21 | public static function getCases($name, $gender = null) |
|
93 | } |
||
94 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.