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 namespace Comodojo\Cookies; |
||
| 24 | class EncryptedCookie extends AbstractCookie { |
||
| 25 | |||
| 26 | /* |
||
| 27 | * AES key |
||
| 28 | * |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | private $key = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Encrypted cookie constructor |
||
| 35 | * |
||
| 36 | * Setup cookie name and key |
||
| 37 | * |
||
| 38 | * @param string $name |
||
| 39 | * |
||
| 40 | * @param string $key |
||
| 41 | * |
||
| 42 | * @throws \Comodojo\Exception\CookieException |
||
| 43 | */ |
||
| 44 | 42 | public function __construct($name, $key, $max_cookie_size = null) { |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Set cookie content |
||
| 54 | * |
||
| 55 | * @param mixed $value Cookie content |
||
| 56 | * @param bool $serialize If true (default) cookie will be serialized first |
||
| 57 | * |
||
| 58 | * @return \Comodojo\Cookies\EncryptedCookie |
||
| 59 | * |
||
| 60 | * @throws \Comodojo\Exception\CookieException |
||
| 61 | */ |
||
| 62 | 24 | public function setValue($value, $serialize = true) { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Get cookie content |
||
| 88 | * |
||
| 89 | * @param bool $unserialize If true (default) cookie will be unserialized first |
||
| 90 | * |
||
| 91 | * @return mixed |
||
| 92 | */ |
||
| 93 | 18 | public function getValue($unserialize = true) { |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Static method to create a cookie quickly |
||
| 117 | * |
||
| 118 | * @param string $name The cookie name |
||
| 119 | * |
||
| 120 | * @param string $key |
||
| 121 | * |
||
| 122 | * @param array $properties Array of properties cookie should have |
||
| 123 | * |
||
| 124 | * @return \Comodojo\Cookies\EncryptedCookie |
||
| 125 | * |
||
| 126 | * @throws \Comodojo\Exception\CookieException |
||
| 127 | */ |
||
| 128 | 6 | public static function create($name, $key, $properties = [], $serialize = true) { |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Static method to get a cookie quickly |
||
| 150 | * |
||
| 151 | * @param string $name The cookie name |
||
| 152 | * |
||
| 153 | * @param string $key |
||
| 154 | * |
||
| 155 | * @return \Comodojo\Cookies\EncryptedCookie |
||
| 156 | * |
||
| 157 | * @throws \Comodojo\Exception\CookieException |
||
| 158 | */ |
||
| 159 | 6 | View Code Duplication | public static function retrieve($name, $key) { |
| 178 | |||
| 179 | /** |
||
| 180 | * Hash the key to generate a valid aes key value |
||
| 181 | * |
||
| 182 | * @param string $key |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | 24 | protected static function encryptKey($key) { |
|
| 191 | |||
| 192 | } |
||
| 193 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.