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 |
||
| 12 | class Header implements |
||
| 13 | \Countable, \IteratorAggregate |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Parameters. |
||
| 17 | * |
||
| 18 | * @var JWTParameter[] $_parameters |
||
| 19 | */ |
||
| 20 | protected $_parameters; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Constructor |
||
| 24 | * |
||
| 25 | * @param JWTParameter ...$params Parameters |
||
| 26 | */ |
||
| 27 | 131 | public function __construct(JWTParameter ...$params) { |
|
| 33 | |||
| 34 | /** |
||
| 35 | * Initialize from an array representing a JSON object. |
||
| 36 | * |
||
| 37 | * @param array $members |
||
| 38 | * @return self |
||
| 39 | */ |
||
| 40 | 45 | public static function fromArray(array $members) { |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Initialize from a JSON. |
||
| 50 | * |
||
| 51 | * @param string $json |
||
| 52 | * @throws \UnexpectedValueException |
||
| 53 | * @return self |
||
| 54 | */ |
||
| 55 | 33 | View Code Duplication | public static function fromJSON($json) { |
| 62 | |||
| 63 | /** |
||
| 64 | * Get self with parameters added. |
||
| 65 | * |
||
| 66 | * @param JWTParameter ...$param |
||
| 67 | * @return self |
||
| 68 | */ |
||
| 69 | 32 | public function withParameters(JWTParameter ...$params) { |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Get all parameters. |
||
| 79 | * |
||
| 80 | * @return JWTParameter[] |
||
| 81 | */ |
||
| 82 | 28 | public function parameters() { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Whether parameters are present. |
||
| 88 | * |
||
| 89 | * Returns false if any of the given parameters is not set. |
||
| 90 | * |
||
| 91 | * @param string ...$names Parameter names |
||
| 92 | * @return boolean |
||
| 93 | */ |
||
| 94 | 70 | View Code Duplication | public function has(...$names) { |
| 102 | |||
| 103 | /** |
||
| 104 | * Get a parameter. |
||
| 105 | * |
||
| 106 | * @param string $name Parameter name |
||
| 107 | * @throws \LogicException |
||
| 108 | * @return JWTParameter |
||
| 109 | */ |
||
| 110 | 37 | public function get($name) { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Get a parameter and check that it's an instance of the expected type. |
||
| 119 | * |
||
| 120 | * @param string $name Parameter name |
||
| 121 | * @throws \RuntimeException |
||
| 122 | * @return JWTParameter |
||
| 123 | */ |
||
| 124 | 4 | public function getTyped($name) { |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Convert to a JSON. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 70 | View Code Duplication | public function toJSON() { |
| 151 | |||
| 152 | /** |
||
| 153 | * Get the number of parameters. |
||
| 154 | * |
||
| 155 | * @see Countable::count() |
||
| 156 | * @return int |
||
| 157 | */ |
||
| 158 | 3 | public function count() { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get iterator for the parameters. |
||
| 164 | * |
||
| 165 | * @see IteratorAggregate::getIterator() |
||
| 166 | * @return \ArrayIterator |
||
| 167 | */ |
||
| 168 | 1 | public function getIterator() { |
|
| 171 | } |
||
| 172 |