Skip to content

Commit 5124588

Browse files
committed
Added deprecated methods EnumSet::[union|intersect|diff|symDiff]
1 parent 984603d commit 5124588

File tree

2 files changed

+131
-86
lines changed

2 files changed

+131
-86
lines changed

src/EnumSet.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,19 @@ public function withUnion(EnumSet $other): self
530530
return $clone;
531531
}
532532

533+
/**
534+
* Create a new set with enumerators from both this and other (this | other)
535+
*
536+
* @param EnumSet $other EnumSet of the same enumeration to produce the union
537+
* @return static
538+
* @throws InvalidArgumentException If $other doesn't match the enumeration
539+
* @deprecated Will trigger deprecation warning in last 4.x and removed in 5.x
540+
*/
541+
public function union(EnumSet $other): self
542+
{
543+
return $this->withUnion($other);
544+
}
545+
533546
/**
534547
* Create a new set with enumerators common to both this and other (this & other)
535548
*
@@ -545,7 +558,20 @@ public function withIntersect(EnumSet $other): self
545558
}
546559

547560
/**
548-
* Modify this set with enumerators in this but not in other (this - other)
561+
* Create a new set with enumerators common to both this and other (this & other)
562+
*
563+
* @param EnumSet $other EnumSet of the same enumeration to produce the intersect
564+
* @return static
565+
* @throws InvalidArgumentException If $other doesn't match the enumeration
566+
* @deprecated Will trigger deprecation warning in last 4.x and removed in 5.x
567+
*/
568+
public function intersect(EnumSet $other): self
569+
{
570+
return $this->withIntersect($other);
571+
}
572+
573+
/**
574+
* Create a new set with enumerators in this but not in other (this - other)
549575
*
550576
* @param EnumSet $other EnumSet of the same enumeration to produce the diff
551577
* @return static
@@ -558,6 +584,19 @@ public function withDiff(EnumSet $other): self
558584
return $clone;
559585
}
560586

587+
/**
588+
* Create a new set with enumerators in this but not in other (this - other)
589+
*
590+
* @param EnumSet $other EnumSet of the same enumeration to produce the diff
591+
* @return static
592+
* @throws InvalidArgumentException If $other doesn't match the enumeration
593+
* @deprecated Will trigger deprecation warning in last 4.x and removed in 5.x
594+
*/
595+
public function diff(EnumSet $other): self
596+
{
597+
return $this->withDiff($other);
598+
}
599+
561600
/**
562601
* Create a new set with enumerators in either this and other but not in both (this ^ other)
563602
*
@@ -572,6 +611,19 @@ public function withSymDiff(EnumSet $other): self
572611
return $clone;
573612
}
574613

614+
/**
615+
* Create a new set with enumerators in either this and other but not in both (this ^ other)
616+
*
617+
* @param EnumSet $other EnumSet of the same enumeration to produce the symmetric difference
618+
* @return static
619+
* @throws InvalidArgumentException If $other doesn't match the enumeration
620+
* @deprecated Will trigger deprecation warning in last 4.x and removed in 5.x
621+
*/
622+
public function symDiff(EnumSet $other): self
623+
{
624+
return $this->withSymDiff($other);
625+
}
626+
575627
/**
576628
* Create a new set with the given binary bitset in little-endian order
577629
*

tests/MabeEnumTest/EnumSetTest.php

Lines changed: 78 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
class EnumSetTest extends TestCase
2525
{
26-
public function testBasicMutable()
26+
public function testBasicImmutable()
2727
{
2828
$set = new EnumSet(EnumBasic::class);
2929
$this->assertSame(EnumBasic::class, $set->getEnumeration());
@@ -47,7 +47,7 @@ public function testBasicMutable()
4747
$this->assertFalse($set->has($enum2));
4848
}
4949

50-
public function testBasicImmutable()
50+
public function testBasicMutable()
5151
{
5252
$set = new EnumSet(EnumBasic::class);
5353
$this->assertSame(EnumBasic::class, $set->getEnumeration());
@@ -170,7 +170,7 @@ public function testInitEnumThrowsInvalidArgumentExceptionOnInvalidEnum()
170170
* @param string $enumeration
171171
* @dataProvider getIntegerEnumerations
172172
*/
173-
public function testAttachAllEnumerators(string $enumeration)
173+
public function testAddRemove(string $enumeration)
174174
{
175175
$set = new EnumSet($enumeration);
176176

@@ -198,7 +198,7 @@ public function testAttachAllEnumerators(string $enumeration)
198198
* @param string $enumeration
199199
* @dataProvider getIntegerEnumerations
200200
*/
201-
public function testAttachAllEnumeratorsAtOnce(string $enumeration)
201+
public function testAddRemoveIterable(string $enumeration)
202202
{
203203
$set = new EnumSet($enumeration);
204204

@@ -215,7 +215,7 @@ public function testAttachAllEnumeratorsAtOnce(string $enumeration)
215215
* @param string $enumeration
216216
* @dataProvider getIntegerEnumerations
217217
*/
218-
public function testWithAllEnumerators(string $enumeration)
218+
public function testWithWithout(string $enumeration)
219219
{
220220
$set = new EnumSet($enumeration);
221221

@@ -242,7 +242,7 @@ public function testWithAllEnumerators(string $enumeration)
242242
* @param string $enumeration
243243
* @dataProvider getIntegerEnumerations
244244
*/
245-
public function testWithAllEnumeratorsAtOnce(string $enumeration)
245+
public function testWithWithoutIterable(string $enumeration)
246246
{
247247
$set = new EnumSet($enumeration);
248248

@@ -270,10 +270,9 @@ public function getIntegerEnumerations()
270270
];
271271
}
272272

273-
public function testAttachAtOnceAllOrNone()
273+
public function testAddIterableAtomic()
274274
{
275-
$set = new EnumSet(EnumBasic::class);
276-
$set->add(EnumBasic::ONE);
275+
$set = new EnumSet(EnumBasic::class, [EnumBasic::ONE]);
277276

278277
try {
279278
$set->addIterable([EnumBasic::TWO, 'unknown']);
@@ -285,10 +284,9 @@ public function testAttachAtOnceAllOrNone()
285284
}
286285
}
287286

288-
public function testDetachAtOnceAllOrNone()
287+
public function testRemoveIterableAtomic()
289288
{
290-
$set = new EnumSet(EnumBasic::class);
291-
$set->addIterable([EnumBasic::ONE]);
289+
$set = new EnumSet(EnumBasic::class, [EnumBasic::ONE]);
292290

293291
try {
294292
$set->removeIterable([EnumBasic::ONE, EnumBasic::TWO, 'unknown']);
@@ -302,8 +300,7 @@ public function testDetachAtOnceAllOrNone()
302300

303301
public function testGetBit()
304302
{
305-
$set = new EnumSet(EnumBasic::class);
306-
$set = $set->with(EnumBasic::TWO);
303+
$set = new EnumSet(EnumBasic::class, [EnumBasic::TWO]);
307304

308305
$this->assertFalse($set->getBit(EnumBasic::ONE()->getOrdinal()));
309306
$this->assertTrue($set->getBit(EnumBasic::TWO()->getOrdinal()));
@@ -459,7 +456,7 @@ public function testWithBinaryBitsetLeBinOutOfRangeBitsOfExtendedBytes1()
459456
$set = new EnumSet(Enum65::class);
460457

461458
$this->expectException(InvalidArgumentException::class, 'Out-Of-Range');
462-
$set = $set->withBinaryBitsetLe("\xff\xff\xff\xff\xff\xff\xff\xff\x00\x02");
459+
$set->withBinaryBitsetLe("\xff\xff\xff\xff\xff\xff\xff\xff\x00\x02");
463460
}
464461

465462
public function testWithBinaryBitsetLeBinOutOfRangeBitsOfExtendedBytes2()
@@ -785,14 +782,8 @@ public function testGetNames()
785782

786783
public function testSetUnion()
787784
{
788-
$set1 = new EnumSet(EnumBasic::class);
789-
$set1->add(EnumBasic::ONE);
790-
$set1->add(EnumBasic::TWO);
791-
792-
$set2 = new EnumSet(EnumBasic::class);
793-
$set2->add(EnumBasic::TWO);
794-
$set2->add(EnumBasic::THREE);
795-
$set2->add(EnumBasic::FOUR);
785+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO]);
786+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
796787

797788
$set1->setUnion($set2);
798789
$this->assertSame([
@@ -805,14 +796,8 @@ public function testSetUnion()
805796

806797
public function testWithUnion()
807798
{
808-
$set1 = new EnumSet(EnumBasic::class);
809-
$set1 = $set1->with(EnumBasic::ONE);
810-
$set1 = $set1->with(EnumBasic::TWO);
811-
812-
$set2 = new EnumSet(EnumBasic::class);
813-
$set2 = $set2->with(EnumBasic::TWO);
814-
$set2 = $set2->with(EnumBasic::THREE);
815-
$set2 = $set2->with(EnumBasic::FOUR);
799+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO]);
800+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
816801

817802
$rs = $set1->withUnion($set2);
818803
$this->assertSame([
@@ -834,31 +819,17 @@ public function testSetUnionThrowsInvalidArgumentException()
834819

835820
public function testSetIntersect()
836821
{
837-
$set1 = new EnumSet(EnumBasic::class);
838-
$set1->add(EnumBasic::ONE);
839-
$set1->add(EnumBasic::TWO);
840-
$set1->add(EnumBasic::THREE);
841-
842-
$set2 = new EnumSet(EnumBasic::class);
843-
$set2->add(EnumBasic::TWO);
844-
$set2->add(EnumBasic::THREE);
845-
$set2->add(EnumBasic::FOUR);
822+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO, EnumBasic::THREE]);
823+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
846824

847825
$set1->setIntersect($set2);
848826
$this->assertSame([EnumBasic::TWO, EnumBasic::THREE], $set1->getValues());
849827
}
850828

851829
public function testWithIntersect()
852830
{
853-
$set1 = new EnumSet(EnumBasic::class);
854-
$set1 = $set1->with(EnumBasic::ONE);
855-
$set1 = $set1->with(EnumBasic::TWO);
856-
$set1 = $set1->with(EnumBasic::THREE);
857-
858-
$set2 = new EnumSet(EnumBasic::class);
859-
$set2 = $set2->with(EnumBasic::TWO);
860-
$set2 = $set2->with(EnumBasic::THREE);
861-
$set2 = $set2->with(EnumBasic::FOUR);
831+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO, EnumBasic::THREE]);
832+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
862833

863834
$rs = $set1->withIntersect($set2);
864835
$this->assertSame([EnumBasic::TWO, EnumBasic::THREE], $rs->getValues());
@@ -875,31 +846,17 @@ public function testSetIntersectThrowsInvalidArgumentException()
875846

876847
public function testSetDiff()
877848
{
878-
$set1 = new EnumSet(EnumBasic::class);
879-
$set1->add(EnumBasic::ONE);
880-
$set1->add(EnumBasic::TWO);
881-
$set1->add(EnumBasic::THREE);
882-
883-
$set2 = new EnumSet(EnumBasic::class);
884-
$set2->add(EnumBasic::TWO);
885-
$set2->add(EnumBasic::THREE);
886-
$set2->add(EnumBasic::FOUR);
849+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO, EnumBasic::THREE]);
850+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
887851

888852
$set1->setDiff($set2);
889853
$this->assertSame([EnumBasic::ONE], $set1->getValues());
890854
}
891855

892856
public function testWithDiff()
893857
{
894-
$set1 = new EnumSet(EnumBasic::class);
895-
$set1 = $set1->with(EnumBasic::ONE);
896-
$set1 = $set1->with(EnumBasic::TWO);
897-
$set1 = $set1->with(EnumBasic::THREE);
898-
899-
$set2 = new EnumSet(EnumBasic::class);
900-
$set2 = $set2->with(EnumBasic::TWO);
901-
$set2 = $set2->with(EnumBasic::THREE);
902-
$set2 = $set2->with(EnumBasic::FOUR);
858+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO, EnumBasic::THREE]);
859+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
903860

904861
$rs = $set1->withDiff($set2);
905862
$this->assertSame([EnumBasic::ONE], $rs->getValues());
@@ -916,15 +873,8 @@ public function testSetDiffThrowsInvalidArgumentException()
916873

917874
public function testSetSymDiff()
918875
{
919-
$set1 = new EnumSet(EnumBasic::class);
920-
$set1->add(EnumBasic::ONE);
921-
$set1->add(EnumBasic::TWO);
922-
$set1->add(EnumBasic::THREE);
923-
924-
$set2 = new EnumSet(EnumBasic::class);
925-
$set2->add(EnumBasic::TWO);
926-
$set2->add(EnumBasic::THREE);
927-
$set2->add(EnumBasic::FOUR);
876+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO, EnumBasic::THREE]);
877+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
928878

929879
$set1->setSymDiff($set2);
930880
$this->assertSame([
@@ -935,15 +885,8 @@ public function testSetSymDiff()
935885

936886
public function testWithSymDiff()
937887
{
938-
$set1 = new EnumSet(EnumBasic::class);
939-
$set1 = $set1->with(EnumBasic::ONE);
940-
$set1 = $set1->with(EnumBasic::TWO);
941-
$set1 = $set1->with(EnumBasic::THREE);
942-
943-
$set2 = new EnumSet(EnumBasic::class);
944-
$set2 = $set2->with(EnumBasic::TWO);
945-
$set2 = $set2->with(EnumBasic::THREE);
946-
$set2 = $set2->with(EnumBasic::FOUR);
888+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO, EnumBasic::THREE]);
889+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
947890

948891
$rs = $set1->withSymDiff($set2);
949892
$this->assertSame([
@@ -963,6 +906,55 @@ public function testSetSymDiffThrowsInvalidArgumentException()
963906

964907
/* deprecated */
965908

909+
/** @deprecated */
910+
public function testUnion()
911+
{
912+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO]);
913+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
914+
915+
$rs = $set1->union($set2);
916+
$this->assertSame([
917+
EnumBasic::ONE,
918+
EnumBasic::TWO,
919+
EnumBasic::THREE,
920+
EnumBasic::FOUR,
921+
], $rs->getValues());
922+
}
923+
924+
/** @deprecated */
925+
public function testIntersect()
926+
{
927+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO, EnumBasic::THREE]);
928+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
929+
930+
$rs = $set1->intersect($set2);
931+
$this->assertSame([EnumBasic::TWO, EnumBasic::THREE], $rs->getValues());
932+
}
933+
934+
/** @deprecated */
935+
public function testDiff()
936+
{
937+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO, EnumBasic::THREE]);
938+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
939+
940+
$rs = $set1->diff($set2);
941+
$this->assertSame([EnumBasic::ONE], $rs->getValues());
942+
}
943+
944+
/** @deprecated */
945+
public function testSymDiff()
946+
{
947+
$set1 = new EnumSet(EnumBasic::class, [EnumBasic::ONE, EnumBasic::TWO, EnumBasic::THREE]);
948+
$set2 = new EnumSet(EnumBasic::class, [EnumBasic::TWO, EnumBasic::THREE, EnumBasic::FOUR]);
949+
950+
$rs = $set1->symDiff($set2);
951+
$this->assertSame([
952+
EnumBasic::ONE,
953+
EnumBasic::FOUR,
954+
], $rs->getValues());
955+
}
956+
957+
/** @deprecated */
966958
public function testAttachDetach()
967959
{
968960
$set = new EnumSet(EnumBasic::class);
@@ -974,6 +966,7 @@ public function testAttachDetach()
974966
$this->assertSame([], $set->getEnumerators());
975967
}
976968

969+
/** @deprecated */
977970
public function testContains()
978971
{
979972
$set = new EnumSet(EnumBasic::class, [EnumBasic::ONE]);

0 commit comments

Comments
 (0)