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 |
||
| 33 | class ResultIterator implements Result, \ArrayAccess, \JsonSerializable |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var Statement |
||
| 37 | */ |
||
| 38 | protected $statement; |
||
| 39 | |||
| 40 | private $objectStorage; |
||
| 41 | private $className; |
||
| 42 | |||
| 43 | private $tdbmService; |
||
| 44 | private $parameters; |
||
| 45 | private $magicQuery; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var QueryFactory |
||
| 49 | */ |
||
| 50 | private $queryFactory; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var InnerResultIterator |
||
| 54 | */ |
||
| 55 | private $innerResultIterator; |
||
| 56 | |||
| 57 | private $databasePlatform; |
||
| 58 | |||
| 59 | private $totalCount; |
||
| 60 | |||
| 61 | private $mode; |
||
| 62 | |||
| 63 | private $logger; |
||
| 64 | |||
| 65 | public function __construct(QueryFactory $queryFactory, array $parameters, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger) |
||
| 81 | |||
| 82 | protected function executeCountQuery() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings). |
||
| 91 | * |
||
| 92 | * @return int |
||
| 93 | */ |
||
| 94 | public function count() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Casts the result set to a PHP array. |
||
| 105 | * |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | public function toArray() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Returns a new iterator mapping any call using the $callable function. |
||
| 115 | * |
||
| 116 | * @param callable $callable |
||
| 117 | * |
||
| 118 | * @return MapIterator |
||
| 119 | */ |
||
| 120 | public function map(callable $callable) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Retrieve an external iterator. |
||
| 127 | * |
||
| 128 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 129 | * |
||
| 130 | * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or |
||
| 131 | * <b>Traversable</b> |
||
| 132 | * |
||
| 133 | * @since 5.0.0 |
||
| 134 | */ |
||
| 135 | public function getIterator() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param int $offset |
||
| 150 | * @param int $limit |
||
| 151 | * |
||
| 152 | * @return PageIterator |
||
| 153 | */ |
||
| 154 | public function take($offset, $limit) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Whether a offset exists. |
||
| 161 | * |
||
| 162 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 163 | * |
||
| 164 | * @param mixed $offset <p> |
||
| 165 | * An offset to check for. |
||
| 166 | * </p> |
||
| 167 | * |
||
| 168 | * @return bool true on success or false on failure. |
||
| 169 | * </p> |
||
| 170 | * <p> |
||
| 171 | * The return value will be casted to boolean if non-boolean was returned |
||
| 172 | * |
||
| 173 | * @since 5.0.0 |
||
| 174 | */ |
||
| 175 | public function offsetExists($offset) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Offset to retrieve. |
||
| 182 | * |
||
| 183 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 184 | * |
||
| 185 | * @param mixed $offset <p> |
||
| 186 | * The offset to retrieve. |
||
| 187 | * </p> |
||
| 188 | * |
||
| 189 | * @return mixed Can return all value types |
||
| 190 | * |
||
| 191 | * @since 5.0.0 |
||
| 192 | */ |
||
| 193 | public function offsetGet($offset) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Offset to set. |
||
| 200 | * |
||
| 201 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 202 | * |
||
| 203 | * @param mixed $offset <p> |
||
| 204 | * The offset to assign the value to. |
||
| 205 | * </p> |
||
| 206 | * @param mixed $value <p> |
||
| 207 | * The value to set. |
||
| 208 | * </p> |
||
| 209 | * |
||
| 210 | * @since 5.0.0 |
||
| 211 | */ |
||
| 212 | public function offsetSet($offset, $value) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Offset to unset. |
||
| 219 | * |
||
| 220 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 221 | * |
||
| 222 | * @param mixed $offset <p> |
||
| 223 | * The offset to unset. |
||
| 224 | * </p> |
||
| 225 | * |
||
| 226 | * @since 5.0.0 |
||
| 227 | */ |
||
| 228 | public function offsetUnset($offset) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Specify data which should be serialized to JSON. |
||
| 235 | * |
||
| 236 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
| 237 | * |
||
| 238 | * @param bool $stopRecursion Parameter used internally by TDBM to |
||
| 239 | * stop embedded objects from embedding |
||
| 240 | * other objects |
||
| 241 | * |
||
| 242 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
| 243 | * which is a value of any type other than a resource |
||
| 244 | * |
||
| 245 | * @since 5.4.0 |
||
| 246 | */ |
||
| 247 | public function jsonSerialize($stopRecursion = false) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns only one value (the first) of the result set. |
||
| 256 | * Returns null if no value exists. |
||
| 257 | * |
||
| 258 | * @return mixed|null |
||
| 259 | */ |
||
| 260 | public function first() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Sets the ORDER BY directive executed in SQL and returns a NEW ResultIterator. |
||
| 272 | * |
||
| 273 | * For instance: |
||
| 274 | * |
||
| 275 | * $resultSet = $resultSet->withOrder('label ASC, status DESC'); |
||
| 276 | * |
||
| 277 | * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter. |
||
| 278 | * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter: |
||
| 279 | * |
||
| 280 | * $resultSet->withOrder(new UncheckedOrderBy('RAND()')) |
||
| 281 | * |
||
| 282 | * @param string|UncheckedOrderBy|null $orderBy |
||
| 283 | * |
||
| 284 | * @return ResultIterator |
||
| 285 | */ |
||
| 286 | View Code Duplication | public function withOrder($orderBy) : ResultIterator |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Sets new parameters for the SQL query and returns a NEW ResultIterator. |
||
| 298 | * |
||
| 299 | * For instance: |
||
| 300 | * |
||
| 301 | * $resultSet = $resultSet->withParameters('label ASC, status DESC'); |
||
| 302 | * |
||
| 303 | * @param string|UncheckedOrderBy|null $orderBy |
||
| 304 | * |
||
| 305 | * @return ResultIterator |
||
| 306 | */ |
||
| 307 | View Code Duplication | public function withParameters(array $parameters) : ResultIterator |
|
| 316 | } |
||
| 317 |
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.