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 AbstractStatisticsLoader extends AbstractLoader  | 
            ||
| 9 | { | 
            ||
| 10 | /**  | 
            ||
| 11 | * Flags set for workarounds  | 
            ||
| 12 | *  | 
            ||
| 13 | * @var string  | 
            ||
| 14 | */  | 
            ||
| 15 | protected $flags;  | 
            ||
| 16 | |||
| 17 | /**  | 
            ||
| 18 | * Allows setting of workaround flags  | 
            ||
| 19 | *  | 
            ||
| 20 | * @param string $flag  | 
            ||
| 21 | */  | 
            ||
| 22 | public function setFlags($flag)  | 
            ||
| 26 | |||
| 27 | /**  | 
            ||
| 28 | * Retrieves workaround flags  | 
            ||
| 29 | *  | 
            ||
| 30 | * @return string  | 
            ||
| 31 | */  | 
            ||
| 32 | public function getFlags()  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * @var string  | 
            ||
| 39 | */  | 
            ||
| 40 | protected $type;  | 
            ||
| 41 | |||
| 42 | /**  | 
            ||
| 43 | * Returns the top X of a particular statistic  | 
            ||
| 44 | *  | 
            ||
| 45 | * @param array $post POST variables from the request  | 
            ||
| 46 | *  | 
            ||
| 47 | * @return array  | 
            ||
| 48 | */  | 
            ||
| 49 | public function readStatistics($post)  | 
            ||
| 67 | |||
| 68 | /**  | 
            ||
| 69 | * Build a redis key based off inputs provided by the POST request  | 
            ||
| 70 | *  | 
            ||
| 71 | * @param array $post  | 
            ||
| 72 | * @param string $redisKey Redis Key to append to  | 
            ||
| 73 | *  | 
            ||
| 74 | * @return string  | 
            ||
| 75 | */  | 
            ||
| 76 | public function appendRedisKey($post, $redisKey)  | 
            ||
| 112 | |||
| 113 | /**  | 
            ||
| 114 | * De-encode the POST vars for use  | 
            ||
| 115 | *  | 
            ||
| 116 | * @param array $post  | 
            ||
| 117 | *  | 
            ||
| 118 | * @return array  | 
            ||
| 119 | */  | 
            ||
| 120 | public function processPostVars($post)  | 
            ||
| 147 | |||
| 148 | /**  | 
            ||
| 149 | * Takes common requests and appends them to the query object. Any other  | 
            ||
| 150 | * special requirements will be handled after  | 
            ||
| 151 | *  | 
            ||
| 152 | * @param Ps2alerts\Api\QueryObjects\QueryObject $queryObject  | 
            ||
| 153 | * @param array $post  | 
            ||
| 154 | *  | 
            ||
| 155 | * @return Ps2alerts\Api\QueryObjects\QueryObject  | 
            ||
| 156 | */  | 
            ||
| 157 | public function setupQueryObject($queryObject, $post)  | 
            ||
| 206 | }  | 
            ||
| 207 | 
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.