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 |
||
| 11 | class Header implements |
||
| 12 | \Countable, |
||
|
|
|||
| 13 | \IteratorAggregate |
||
| 14 | { |
||
| 15 | use TypedHeader; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Parameters. |
||
| 19 | * |
||
| 20 | * @var JWTParameter[] $_parameters |
||
| 21 | */ |
||
| 22 | protected $_parameters; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructor |
||
| 26 | * |
||
| 27 | * @param JWTParameter ...$params Parameters |
||
| 28 | */ |
||
| 29 | 131 | public function __construct(JWTParameter ...$params) { |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Initialize from an array representing a JSON object. |
||
| 38 | * |
||
| 39 | * @param array $members |
||
| 40 | * @return self |
||
| 41 | */ |
||
| 42 | 45 | public static function fromArray(array $members) { |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Initialize from a JSON. |
||
| 52 | * |
||
| 53 | * @param string $json |
||
| 54 | * @throws \UnexpectedValueException |
||
| 55 | * @return self |
||
| 56 | */ |
||
| 57 | 33 | View Code Duplication | public static function fromJSON($json) { |
| 64 | |||
| 65 | /** |
||
| 66 | * Get self with parameters added. |
||
| 67 | * |
||
| 68 | * @param JWTParameter ...$param |
||
| 69 | * @return self |
||
| 70 | */ |
||
| 71 | 32 | public function withParameters(JWTParameter ...$params) { |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Get all parameters. |
||
| 81 | * |
||
| 82 | * @return JWTParameter[] |
||
| 83 | */ |
||
| 84 | 28 | public function parameters() { |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Whether parameters are present. |
||
| 90 | * |
||
| 91 | * Returns false if any of the given parameters is not set. |
||
| 92 | * |
||
| 93 | * @param string ...$names Parameter names |
||
| 94 | * @return boolean |
||
| 95 | */ |
||
| 96 | 70 | View Code Duplication | public function has(...$names) { |
| 104 | |||
| 105 | /** |
||
| 106 | * Get a parameter. |
||
| 107 | * |
||
| 108 | * @param string $name Parameter name |
||
| 109 | * @throws \LogicException |
||
| 110 | * @return JWTParameter |
||
| 111 | */ |
||
| 112 | 37 | public function get($name) { |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Convert to a JSON. |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | 70 | View Code Duplication | public function toJSON() { |
| 134 | |||
| 135 | /** |
||
| 136 | * Get the number of parameters. |
||
| 137 | * |
||
| 138 | * @see Countable::count() |
||
| 139 | * @return int |
||
| 140 | */ |
||
| 141 | 3 | public function count() { |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Get iterator for the parameters. |
||
| 147 | * |
||
| 148 | * @see IteratorAggregate::getIterator() |
||
| 149 | * @return \ArrayIterator |
||
| 150 | */ |
||
| 151 | 1 | public function getIterator() { |
|
| 154 | } |
||
| 155 |