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 |
||
21 | class StockbaseStockManagementTest extends TestCase |
||
22 | { |
||
23 | |||
24 | /** @var StockRegistryInterface|\PHPUnit_Framework_MockObject_MockObject */ |
||
25 | private $stockRegistry; |
||
26 | |||
27 | /** @var StockbaseConfiguration|\PHPUnit_Framework_MockObject_MockObject */ |
||
28 | private $config; |
||
29 | |||
30 | /** @var ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ |
||
31 | private $productRepository; |
||
32 | |||
33 | /** @var StockItemResource|\PHPUnit_Framework_MockObject_MockObject */ |
||
34 | private $stockItemResource; |
||
35 | |||
36 | /** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
||
37 | private $objectManager; |
||
38 | |||
39 | /** @var \Magento\CatalogInventory\Model\Stock\Item|\PHPUnit_Framework_MockObject_MockObject */ |
||
40 | private $stockItem; |
||
41 | |||
42 | /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */ |
||
43 | private $product; |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | 9 | public function setUp() |
|
49 | { |
||
50 | 9 | $this->stockItem = $this->createMock(\Magento\CatalogInventory\Model\Stock\Item::class); |
|
51 | |||
52 | 9 | $this->stockRegistry = $this->createMock(StockRegistryInterface::class); |
|
53 | |||
54 | 9 | $this->config = $this->createMock(StockbaseConfiguration::class); |
|
55 | |||
56 | 9 | $this->product = $this->createMock(\Magento\Catalog\Model\Product::class); |
|
57 | |||
58 | 9 | $this->productRepository = $this->createMock(ProductRepositoryInterface::class); |
|
59 | |||
60 | 9 | $this->stockItemResource = $this->createMock(StockItemResource::class); |
|
61 | |||
62 | 9 | $this->objectManager = $this->createMock(ObjectManagerInterface::class); |
|
63 | 9 | } |
|
64 | |||
65 | /** |
||
66 | * testGetStockbaseStockAmount |
||
67 | */ |
||
68 | 1 | public function testGetStockbaseStockAmount() |
|
69 | { |
||
70 | 1 | $this->configureStockbaseEan(101, '12345'); |
|
71 | |||
72 | 1 | $this->stockItemResource->expects($this->once())->method('getNotReservedStockAmount') |
|
|
|||
73 | 1 | ->with('12345') |
|
74 | 1 | ->willReturn(5.0); |
|
75 | |||
76 | 1 | $model = $this->createModel(); |
|
77 | 1 | $result = $model->getStockbaseStockAmount(101); |
|
78 | |||
79 | 1 | $this->assertEquals(5.0, $result); |
|
80 | 1 | } |
|
81 | |||
82 | /** |
||
83 | * testGetStockbaseEan |
||
84 | */ |
||
85 | 1 | View Code Duplication | public function testGetStockbaseEan() |
86 | { |
||
87 | 1 | $this->configureStockbaseEan(101, '12345'); |
|
88 | |||
89 | 1 | $model = $this->createModel(); |
|
90 | 1 | $result = $model->getStockbaseEan(101); |
|
91 | |||
92 | 1 | $this->assertEquals('12345', $result); |
|
93 | 1 | } |
|
94 | |||
95 | /** |
||
96 | * testIsStockbaseProduct |
||
97 | */ |
||
98 | 1 | View Code Duplication | public function testIsStockbaseProduct() |
99 | { |
||
100 | 1 | $this->configureStockbaseEan(101, '12345'); |
|
101 | |||
102 | 1 | $model = $this->createModel(); |
|
103 | 1 | $result = $model->isStockbaseProduct(101); |
|
104 | |||
105 | 1 | $this->assertEquals(true, $result); |
|
106 | 1 | } |
|
107 | |||
108 | /** |
||
109 | * testUpdateStockAmount |
||
110 | * @dataProvider updateStockAmountProvider |
||
111 | * |
||
112 | * @param mixed $ean |
||
113 | * @param mixed $amount |
||
114 | * @param mixed $operation |
||
115 | */ |
||
116 | 2 | public function testUpdateStockAmount($ean, $amount, $operation) |
|
117 | { |
||
118 | 2 | $this->stockItemResource->expects($this->once())->method('updateStockAmount') |
|
119 | 2 | ->with($ean, $amount, $operation); |
|
120 | |||
121 | 2 | $model = $this->createModel(); |
|
122 | 2 | $model->updateStockAmount($ean, $amount, $operation); |
|
123 | 2 | } |
|
124 | |||
125 | /** |
||
126 | * @return array |
||
127 | */ |
||
128 | public function updateStockAmountProvider() |
||
135 | |||
136 | /** |
||
137 | * testCreateReserve |
||
138 | */ |
||
139 | 1 | public function testCreateReserve() |
|
140 | { |
||
178 | |||
179 | /** |
||
180 | * testReleaseReserve |
||
181 | */ |
||
182 | 1 | public function testReleaseReserve() |
|
190 | |||
191 | /** |
||
192 | * testGetReserveForProduct |
||
193 | */ |
||
194 | 1 | View Code Duplication | public function testGetReserveForProduct() |
215 | |||
216 | /** |
||
217 | * testGetReserveForQuoteItem |
||
218 | */ |
||
219 | 1 | View Code Duplication | public function testGetReserveForQuoteItem() |
240 | |||
241 | 9 | protected function createModel() |
|
251 | |||
252 | 4 | protected function configureStockbaseEan($productId, $ean) |
|
269 | } |
||
270 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: