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 Polygon |
||
|
|
|||
| 10 | { |
||
| 11 | /** @var Point[] */ |
||
| 12 | private $points; |
||
| 13 | |||
| 14 | |||
| 15 | /** |
||
| 16 | * Creates a new polygon defined by the list of its vertices. |
||
| 17 | * |
||
| 18 | * @param Point[]|float[][] $points |
||
| 19 | * @return Polygon |
||
| 20 | */ |
||
| 21 | public static function fromPoints($points): Polygon |
||
| 40 | |||
| 41 | private function __construct($points) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return Point[] the list of vertices determining this polygon |
||
| 48 | */ |
||
| 49 | public function getPoints() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Computes the area of the polygon. |
||
| 56 | * |
||
| 57 | * The algorithm taken from http://alienryderflex.com/polygon_area/ |
||
| 58 | * |
||
| 59 | * @return float area of the polygon |
||
| 60 | */ |
||
| 61 | public function getArea(): float |
||
| 73 | |||
| 74 | public function __toString() |
||
| 78 | } |
||
| 79 |