| Total Complexity | 40 |
| Total Lines | 411 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PrimitiveTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PrimitiveTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class PrimitiveTest extends TestCase |
||
| 18 | { |
||
| 19 | public function testInterface() |
||
| 20 | { |
||
| 21 | $this->assertInstanceOf( |
||
| 22 | Implementation::class, |
||
| 23 | new Primitive, |
||
| 24 | ); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function testSize() |
||
| 28 | { |
||
| 29 | $this->assertSame(2, (new Primitive([1, 1]))->size()); |
||
| 30 | $this->assertSame(2, (new Primitive([1, 1]))->count()); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function testIterator() |
||
| 34 | { |
||
| 35 | $this->assertSame( |
||
| 36 | [1, 2, 3], |
||
| 37 | \iterator_to_array((new Primitive([1, 2, 3]))->iterator()), |
||
| 38 | ); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testGet() |
||
| 42 | { |
||
| 43 | $this->assertSame(42, $this->get(new Primitive([1, 42, 3]), 1)); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function testReturnNothingWhenIndexNotFound() |
||
| 47 | { |
||
| 48 | $this->assertNull($this->get(new Primitive, 0)); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function testDiff() |
||
| 52 | { |
||
| 53 | $a = new Primitive([1, 2]); |
||
| 54 | $b = new Primitive([2, 3]); |
||
| 55 | $c = $a->diff($b); |
||
| 56 | |||
| 57 | $this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
||
| 58 | $this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
||
| 59 | $this->assertInstanceOf(Primitive::class, $c); |
||
| 60 | $this->assertSame([1], \iterator_to_array($c->iterator())); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function testDistinct() |
||
| 64 | { |
||
| 65 | $a = new Primitive([1, 2, 1]); |
||
| 66 | $b = $a->distinct(); |
||
| 67 | |||
| 68 | $this->assertSame([1, 2, 1], \iterator_to_array($a->iterator())); |
||
| 69 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 70 | $this->assertSame([1, 2], \iterator_to_array($b->iterator())); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function testDrop() |
||
| 74 | { |
||
| 75 | $a = new Primitive([1, 2, 3, 4]); |
||
| 76 | $b = $a->drop(2); |
||
| 77 | |||
| 78 | $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
||
| 79 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 80 | $this->assertSame([3, 4], \iterator_to_array($b->iterator())); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function testDropEnd() |
||
| 84 | { |
||
| 85 | $a = new Primitive([1, 2, 3, 4]); |
||
| 86 | $b = $a->dropEnd(2); |
||
| 87 | |||
| 88 | $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
||
| 89 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 90 | $this->assertSame([1, 2], \iterator_to_array($b->iterator())); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function testEquals() |
||
| 94 | { |
||
| 95 | $this->assertTrue((new Primitive([1, 2]))->equals(new Primitive([1, 2]))); |
||
| 96 | $this->assertFalse((new Primitive([1, 2]))->equals(new Primitive([2]))); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function testFilter() |
||
| 100 | { |
||
| 101 | $a = new Primitive([1, 2, 3, 4]); |
||
| 102 | $b = $a->filter(static fn($i) => $i % 2 === 0); |
||
| 103 | |||
| 104 | $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
||
| 105 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 106 | $this->assertSame([2, 4], \iterator_to_array($b->iterator())); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testForeach() |
||
| 110 | { |
||
| 111 | $sequence = new Primitive([1, 2, 3, 4]); |
||
| 112 | $calls = 0; |
||
| 113 | $sum = 0; |
||
| 114 | |||
| 115 | $this->assertInstanceOf( |
||
| 116 | SideEffect::class, |
||
| 117 | $sequence->foreach(static function($i) use (&$calls, &$sum) { |
||
| 118 | ++$calls; |
||
| 119 | $sum += $i; |
||
| 120 | }), |
||
| 121 | ); |
||
| 122 | $this->assertSame(4, $calls); |
||
| 123 | $this->assertSame(10, $sum); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function testGroupEmptySequence() |
||
| 127 | { |
||
| 128 | $this->assertTrue( |
||
| 129 | (new Primitive) |
||
| 130 | ->groupBy(static fn($i) => $i) |
||
| 131 | ->equals(Map::of()), |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function testGroupBy() |
||
| 136 | { |
||
| 137 | $sequence = new Primitive([1, 2, 3, 4]); |
||
| 138 | $groups = $sequence->groupBy(static fn($i) => $i % 2); |
||
| 139 | |||
| 140 | $this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator())); |
||
| 141 | $this->assertInstanceOf(Map::class, $groups); |
||
| 142 | $this->assertCount(2, $groups); |
||
| 143 | $this->assertSame([2, 4], $this->get($groups, 0)->toList()); |
||
| 144 | $this->assertSame([1, 3], $this->get($groups, 1)->toList()); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function testReturnNothingWhenTryingToAccessFirstElementOnEmptySequence() |
||
| 148 | { |
||
| 149 | $this->assertNull( |
||
| 150 | (new Primitive)->first()->match( |
||
| 151 | static fn($value) => $value, |
||
| 152 | static fn() => null, |
||
| 153 | ), |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | public function testReturnNothingWhenTryingToAccessLastElementOnEmptySequence() |
||
| 158 | { |
||
| 159 | $this->assertNull( |
||
| 160 | (new Primitive)->last()->match( |
||
| 161 | static fn($value) => $value, |
||
| 162 | static fn() => null, |
||
| 163 | ), |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function testFirst() |
||
| 168 | { |
||
| 169 | $this->assertSame( |
||
| 170 | 2, |
||
| 171 | (new Primitive([2, 3, 4]))->first()->match( |
||
| 172 | static fn($value) => $value, |
||
| 173 | static fn() => null, |
||
| 174 | ), |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function testLast() |
||
| 179 | { |
||
| 180 | $this->assertSame( |
||
| 181 | 4, |
||
| 182 | (new Primitive([2, 3, 4]))->last()->match( |
||
| 183 | static fn($value) => $value, |
||
| 184 | static fn() => null, |
||
| 185 | ), |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function testContains() |
||
| 190 | { |
||
| 191 | $sequence = new Primitive([1, 2, 3]); |
||
| 192 | |||
| 193 | $this->assertTrue($sequence->contains(2)); |
||
|
|
|||
| 194 | $this->assertFalse($sequence->contains(4)); |
||
| 195 | } |
||
| 196 | |||
| 197 | public function testIndexOf() |
||
| 198 | { |
||
| 199 | $sequence = new Primitive([1, 2, 4]); |
||
| 200 | |||
| 201 | $this->assertSame( |
||
| 202 | 1, |
||
| 203 | $sequence->indexOf(2)->match( |
||
| 204 | static fn($value) => $value, |
||
| 205 | static fn() => null, |
||
| 206 | ), |
||
| 207 | ); |
||
| 208 | $this->assertSame( |
||
| 209 | 2, |
||
| 210 | $sequence->indexOf(4)->match( |
||
| 211 | static fn($value) => $value, |
||
| 212 | static fn() => null, |
||
| 213 | ), |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | |||
| 217 | public function testReturnNothingWhenTryingToAccessIndexOfUnknownValue() |
||
| 218 | { |
||
| 219 | $this->assertNull( |
||
| 220 | (new Primitive)->indexOf(1)->match( |
||
| 221 | static fn($value) => $value, |
||
| 222 | static fn() => null, |
||
| 223 | ), |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | public function testIndices() |
||
| 228 | { |
||
| 229 | $a = new Primitive(['1', '2']); |
||
| 230 | $b = $a->indices(); |
||
| 231 | |||
| 232 | $this->assertSame(['1', '2'], \iterator_to_array($a->iterator())); |
||
| 233 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 234 | $this->assertSame([0, 1], \iterator_to_array($b->iterator())); |
||
| 235 | } |
||
| 236 | |||
| 237 | public function testIndicesOnEmptySequence() |
||
| 238 | { |
||
| 239 | $a = new Primitive; |
||
| 240 | $b = $a->indices(); |
||
| 241 | |||
| 242 | $this->assertSame([], \iterator_to_array($a->iterator())); |
||
| 243 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 244 | $this->assertSame([], \iterator_to_array($b->iterator())); |
||
| 245 | } |
||
| 246 | |||
| 247 | public function testMap() |
||
| 248 | { |
||
| 249 | $a = new Primitive([1, 2, 3]); |
||
| 250 | $b = $a->map(static fn($i) => $i * 2); |
||
| 251 | |||
| 252 | $this->assertSame([1, 2, 3], \iterator_to_array($a->iterator())); |
||
| 253 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 254 | $this->assertSame([2, 4, 6], \iterator_to_array($b->iterator())); |
||
| 255 | } |
||
| 256 | |||
| 257 | public function testPad() |
||
| 258 | { |
||
| 259 | $a = new Primitive([1, 2]); |
||
| 260 | $b = $a->pad(4, 0); |
||
| 261 | $c = $a->pad(1, 0); |
||
| 262 | |||
| 263 | $this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
||
| 264 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 265 | $this->assertInstanceOf(Primitive::class, $c); |
||
| 266 | $this->assertSame([1, 2, 0, 0], \iterator_to_array($b->iterator())); |
||
| 267 | $this->assertSame([1, 2], \iterator_to_array($c->iterator())); |
||
| 268 | } |
||
| 269 | |||
| 270 | public function testPartition() |
||
| 271 | { |
||
| 272 | $sequence = new Primitive([1, 2, 3, 4]); |
||
| 273 | $partition = $sequence->partition(static fn($i) => $i % 2 === 0); |
||
| 274 | |||
| 275 | $this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator())); |
||
| 276 | $this->assertInstanceOf(Map::class, $partition); |
||
| 277 | $this->assertCount(2, $partition); |
||
| 278 | $this->assertSame([2, 4], $this->get($partition, true)->toList()); |
||
| 279 | $this->assertSame([1, 3], $this->get($partition, false)->toList()); |
||
| 280 | } |
||
| 281 | |||
| 282 | public function testSlice() |
||
| 283 | { |
||
| 284 | $a = new Primitive([2, 3, 4, 5]); |
||
| 285 | $b = $a->slice(1, 3); |
||
| 286 | |||
| 287 | $this->assertSame([2, 3, 4, 5], \iterator_to_array($a->iterator())); |
||
| 288 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 289 | $this->assertSame([3, 4], \iterator_to_array($b->iterator())); |
||
| 290 | } |
||
| 291 | |||
| 292 | public function testTake() |
||
| 293 | { |
||
| 294 | $a = new Primitive([2, 3, 4]); |
||
| 295 | $b = $a->take(2); |
||
| 296 | |||
| 297 | $this->assertSame([2, 3, 4], \iterator_to_array($a->iterator())); |
||
| 298 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 299 | $this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
||
| 300 | } |
||
| 301 | |||
| 302 | public function testTakeEnd() |
||
| 303 | { |
||
| 304 | $a = new Primitive([2, 3, 4]); |
||
| 305 | $b = $a->takeEnd(2); |
||
| 306 | |||
| 307 | $this->assertSame([2, 3, 4], \iterator_to_array($a->iterator())); |
||
| 308 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 309 | $this->assertSame([3, 4], \iterator_to_array($b->iterator())); |
||
| 310 | } |
||
| 311 | |||
| 312 | public function testAppend() |
||
| 313 | { |
||
| 314 | $a = new Primitive([1, 2]); |
||
| 315 | $b = new Primitive([3, 4]); |
||
| 316 | $c = $a->append($b); |
||
| 317 | |||
| 318 | $this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
||
| 319 | $this->assertSame([3, 4], \iterator_to_array($b->iterator())); |
||
| 320 | $this->assertInstanceOf(Primitive::class, $c); |
||
| 321 | $this->assertSame([1, 2, 3, 4], \iterator_to_array($c->iterator())); |
||
| 322 | } |
||
| 323 | |||
| 324 | public function testIntersect() |
||
| 325 | { |
||
| 326 | $a = new Primitive([1, 2]); |
||
| 327 | $b = new Primitive([2, 3]); |
||
| 328 | $c = $a->intersect($b); |
||
| 329 | |||
| 330 | $this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
||
| 331 | $this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
||
| 332 | $this->assertInstanceOf(Primitive::class, $c); |
||
| 333 | $this->assertSame([2], \iterator_to_array($c->iterator())); |
||
| 334 | } |
||
| 335 | |||
| 336 | public function testAdd() |
||
| 337 | { |
||
| 338 | $a = new Primitive([1]); |
||
| 339 | $b = ($a)(2); |
||
| 340 | |||
| 341 | $this->assertSame([1], \iterator_to_array($a->iterator())); |
||
| 342 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 343 | $this->assertSame([1, 2], \iterator_to_array($b->iterator())); |
||
| 344 | } |
||
| 345 | |||
| 346 | public function testSort() |
||
| 347 | { |
||
| 348 | $a = new Primitive([1, 4, 3, 2]); |
||
| 349 | $b = $a->sort(static fn($a, $b) => $a > $b ? 1 : -1); |
||
| 350 | |||
| 351 | $this->assertSame([1, 4, 3, 2], \iterator_to_array($a->iterator())); |
||
| 352 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 353 | $this->assertSame([1, 2, 3, 4], \iterator_to_array($b->iterator())); |
||
| 354 | } |
||
| 355 | |||
| 356 | public function testReduce() |
||
| 357 | { |
||
| 358 | $sequence = new Primitive([1, 2, 3, 4]); |
||
| 359 | |||
| 360 | $this->assertSame(10, $sequence->reduce(0, static fn($sum, $i) => $sum + $i)); |
||
| 361 | } |
||
| 362 | |||
| 363 | public function testClear() |
||
| 371 | } |
||
| 372 | |||
| 373 | public function testReverse() |
||
| 374 | { |
||
| 375 | $a = new Primitive([1, 2, 3, 4]); |
||
| 376 | $b = $a->reverse(); |
||
| 377 | |||
| 378 | $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
||
| 379 | $this->assertInstanceOf(Primitive::class, $b); |
||
| 380 | $this->assertSame([4, 3, 2, 1], \iterator_to_array($b->iterator())); |
||
| 381 | } |
||
| 382 | |||
| 383 | public function testEmpty() |
||
| 384 | { |
||
| 385 | $this->assertTrue((new Primitive)->empty()); |
||
| 386 | $this->assertFalse((new Primitive([1]))->empty()); |
||
| 387 | } |
||
| 388 | |||
| 389 | public function testFind() |
||
| 390 | { |
||
| 391 | $sequence = new Primitive([1, 2, 3]); |
||
| 392 | |||
| 393 | $this->assertSame( |
||
| 394 | 1, |
||
| 395 | $sequence->find(static fn($i) => $i === 1)->match( |
||
| 396 | static fn($i) => $i, |
||
| 397 | static fn() => null, |
||
| 398 | ), |
||
| 399 | ); |
||
| 400 | $this->assertSame( |
||
| 401 | 2, |
||
| 402 | $sequence->find(static fn($i) => $i === 2)->match( |
||
| 403 | static fn($i) => $i, |
||
| 404 | static fn() => null, |
||
| 405 | ), |
||
| 406 | ); |
||
| 407 | $this->assertSame( |
||
| 408 | 3, |
||
| 409 | $sequence->find(static fn($i) => $i === 3)->match( |
||
| 410 | static fn($i) => $i, |
||
| 411 | static fn() => null, |
||
| 412 | ), |
||
| 413 | ); |
||
| 414 | |||
| 415 | $this->assertNull( |
||
| 416 | $sequence->find(static fn($i) => $i === 0)->match( |
||
| 417 | static fn($i) => $i, |
||
| 418 | static fn() => null, |
||
| 419 | ), |
||
| 420 | ); |
||
| 421 | } |
||
| 422 | |||
| 423 | public function get($map, $index) |
||
| 428 | ); |
||
| 429 | } |
||
| 430 | } |
||
| 431 |