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 |
||
| 9 | class Method extends FunctionLike |
||
| 10 | { |
||
| 11 | protected $name; |
||
| 12 | protected $type = 0; |
||
| 13 | protected $stmts = array(); |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Creates a method builder. |
||
| 17 | * |
||
| 18 | * @param string $name Name of the method |
||
| 19 | */ |
||
| 20 | public function __construct($name) { |
||
| 21 | $this->name = $name; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Makes the method public. |
||
| 26 | * |
||
| 27 | * @return $this The builder instance (for fluid interface) |
||
| 28 | */ |
||
| 29 | public function makePublic() { |
||
| 30 | $this->setModifier(Stmt\Class_::MODIFIER_PUBLIC); |
||
| 31 | |||
| 32 | return $this; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Makes the method protected. |
||
| 37 | * |
||
| 38 | * @return $this The builder instance (for fluid interface) |
||
| 39 | */ |
||
| 40 | public function makeProtected() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Makes the method private. |
||
| 48 | * |
||
| 49 | * @return $this The builder instance (for fluid interface) |
||
| 50 | */ |
||
| 51 | public function makePrivate() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Makes the method static. |
||
| 59 | * |
||
| 60 | * @return $this The builder instance (for fluid interface) |
||
| 61 | */ |
||
| 62 | public function makeStatic() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Makes the method abstract. |
||
| 70 | * |
||
| 71 | * @return $this The builder instance (for fluid interface) |
||
| 72 | */ |
||
| 73 | public function makeAbstract() { |
||
| 74 | if (!empty($this->stmts)) { |
||
| 75 | throw new \LogicException('Cannot make method with statements abstract'); |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->setModifier(Stmt\Class_::MODIFIER_ABSTRACT); |
||
| 79 | $this->stmts = null; // abstract methods don't have statements |
||
|
|
|||
| 80 | |||
| 81 | return $this; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Makes the method final. |
||
| 86 | * |
||
| 87 | * @return $this The builder instance (for fluid interface) |
||
| 88 | */ |
||
| 89 | public function makeFinal() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Adds a statement. |
||
| 97 | * |
||
| 98 | * @param Node|PhpParser\Builder $stmt The statement to add |
||
| 99 | * |
||
| 100 | * @return $this The builder instance (for fluid interface) |
||
| 101 | */ |
||
| 102 | public function addStmt($stmt) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns the built method node. |
||
| 114 | * |
||
| 115 | * @return Stmt\ClassMethod The built method node |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function getNode() { |
|
| 125 | } |
||
| 126 |
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..