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 |
||
| 8 | abstract class Validator implements ValidatorInterface{ |
||
| 9 | protected $modifiedMessage; |
||
| 10 | protected $message; |
||
| 11 | protected $member; |
||
| 12 | protected $value; |
||
| 13 | protected $notNull; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param mixed $value |
||
| 17 | * @param string $member |
||
| 18 | * @param array $params |
||
| 19 | * @param string $severity |
||
| 20 | * @param string $message |
||
| 21 | * @return \Ubiquity\contents\validation\validators\ConstraintViolation|boolean |
||
| 22 | */ |
||
| 23 | public function validate_($value,$member,$params,$severity='warning',$message=null){ |
||
| 32 | |||
| 33 | protected function setParams(array $params){ |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return mixed |
||
| 41 | */ |
||
| 42 | public function getMember() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param mixed $member |
||
| 48 | */ |
||
| 49 | public function setMember($member) { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritDoc} |
||
| 55 | * @see \Ubiquity\contents\validation\validators\ValidatorInterface::getParameters() |
||
| 56 | */ |
||
| 57 | public function getParameters(): array { |
||
| 61 | |||
| 62 | public function validate($value) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @return array|string |
||
| 73 | */ |
||
| 74 | protected function mergeMessages(){ |
||
| 81 | |||
| 82 | protected function _getMessage(){ |
||
| 90 | |||
| 91 | } |
||
| 92 | |||
| 93 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.