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 |
||
| 27 | class ProductDetailPage extends AbstractFrontPage |
||
| 28 | { |
||
| 29 | public function __construct(\AcceptanceTester $I) |
||
| 30 | { |
||
| 31 | parent::__construct($I); |
||
| 32 | } |
||
| 33 | |||
| 34 | public static function go($I, $id) |
||
| 35 | { |
||
| 36 | $page = new self($I); |
||
| 37 | return $page->goPage('products/detail/'.$id); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function カテゴリ選択($categories) |
||
| 41 | { |
||
| 42 | $xpath = "//*[@class='ec-layoutRole__header']/"; |
||
| 43 | foreach ($categories as $i=>$category) { |
||
| 44 | $xpath .= "/ul/li/a[contains(text(), '$category')]/parent::node()"; |
||
| 45 | $this->tester->waitForElement(['xpath' => $xpath]); |
||
| 46 | $this->tester->moveMouseOver(['xpath' => $xpath]); |
||
| 47 | } |
||
| 48 | $this->tester->click(['xpath' => $xpath]); |
||
| 49 | return $this; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function サムネイル切替($num) |
||
| 53 | { |
||
| 54 | $this->tester->click("div.item_nav div.slick-list div.slick-track div.slideThumb:nth-child(${num})"); |
||
| 55 | return $this; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function サムネイル画像URL() |
||
| 59 | { |
||
| 60 | return $this->tester->grabAttributeFrom('div.item.slick-slide.slick-current.slick-active img', 'src'); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function 規格選択($array) |
||
| 64 | { |
||
| 65 | foreach ($array as $index=>$option) { |
||
| 66 | $this->tester->selectOption(['id' => 'classcategory_id'.($index+1)], $option); |
||
| 67 | } |
||
| 68 | return $this; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param $num|int |
||
| 73 | * @return ProductDetailPage |
||
| 74 | */ |
||
| 75 | public function カートに入れる($num) |
||
| 76 | { |
||
| 77 | $this->tester->fillField(['id' => 'quantity'], $num); |
||
| 78 | $this->tester->click(['id' => 'add-cart']); |
||
| 79 | $this->tester->wait(1); |
||
| 80 | return $this; |
||
| 81 | } |
||
| 82 | |||
| 83 | public function お気に入りに追加() |
||
| 84 | { |
||
| 85 | $this->tester->click('#favorite'); |
||
| 86 | return $this; |
||
| 87 | } |
||
| 88 | } |