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 |
||
| 35 | class Github |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var array Options for the GitHub object. |
||
| 39 | * @since 1.0 |
||
| 40 | */ |
||
| 41 | protected $options; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Http The HTTP client object to use in sending HTTP requests. |
||
| 45 | * @since 1.0 |
||
| 46 | */ |
||
| 47 | protected $client; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructor. |
||
| 51 | * |
||
| 52 | * @param Registry $options GitHub options object. |
||
| 53 | * @param Http $client The HTTP client object. |
||
| 54 | * |
||
| 55 | * @since 1.0 |
||
| 56 | */ |
||
| 57 | public function __construct(Registry $options = null, Http $client = null) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Magic method to lazily create API objects |
||
| 71 | * |
||
| 72 | * @param string $name Name of property to retrieve |
||
| 73 | * |
||
| 74 | * @return AbstractGithubObject GitHub API object (gists, issues, pulls, etc). |
||
| 75 | * |
||
| 76 | * @since 1.0 |
||
| 77 | * @throws \InvalidArgumentException If $name is not a valid sub class. |
||
| 78 | */ |
||
| 79 | public function __get($name) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get an option from the GitHub instance. |
||
| 98 | * |
||
| 99 | * @param string $key The name of the option to get. |
||
| 100 | * |
||
| 101 | * @return mixed The option value. |
||
| 102 | * |
||
| 103 | * @since 1.0 |
||
| 104 | */ |
||
| 105 | public function getOption($key) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set an option for the GitHub instance. |
||
| 112 | * |
||
| 113 | * @param string $key The name of the option to set. |
||
| 114 | * @param mixed $value The option value to set. |
||
| 115 | * |
||
| 116 | * @return GitHub This object for method chaining. |
||
| 117 | * |
||
| 118 | * @since 1.0 |
||
| 119 | */ |
||
| 120 | public function setOption($key, $value) |
||
| 126 | } |
||
| 127 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..