PrinsFrank / standards

A collection of standards as PHP Enums: ISO3166, ISO4217, ISO639...
MIT License
393 stars 10 forks source link

Danger is my middle name! #183

Closed szepeviktor closed 8 months ago

szepeviktor commented 8 months ago

💥 Allow duplicated keys.

For #182

codecov[bot] commented 8 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (524d1a7) 100.00% compared to head (972fbf7) 100.00%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #183 +/- ## =========================================== Coverage 100.00% 100.00% Complexity 231 231 =========================================== Files 33 33 Lines 3611 3611 =========================================== Hits 3611 3611 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

PrinsFrank commented 8 months ago

Hi, I think you found quite the bug here. As far as I can see, the __toString method on the EnumCase class is only there to filter out duplicates, but that is not done directly. IMHO we just add the duplicate cases instead of silently dropping them and let CI pick those up for manual review. I think the best fix in that case would be this:

dev/DataTarget/EnumCase.php

public function __toString(): string
{
-    return NameNormalizer::normalize($this->name);
+    return NameNormalizer::normalize($this->name) . NameNormalizer::normalize($this->value);
}

EnumCaseTest.php

<?php
declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use PrinsFrank\Standards\Dev\DataTarget\EnumCase;

/** @coversDefaultClass \PrinsFrank\Standards\Dev\DataTarget\EnumCase */
class EnumCaseTest extends TestCase
{
    /** @covers ::__toString */
    public function testToStringCanBeUsedToDetectIdenticalEnumNameValuePairs(): void
    {
        static::assertEquals(
            [
                new EnumCase('', '')
            ],
            array_unique(
                [
                    new EnumCase('', '')
                ]
            )
        );
        static::assertEquals(
            [
                new EnumCase('foo', 'bar')
            ],
            array_unique(
                [
                    new EnumCase('foo', 'bar')
                ]
            )
        );
        static::assertEquals(
            [
                new EnumCase('foo', 'bar')
            ],
            array_unique(
                [
                    new EnumCase('foo', 'bar'),
                    new EnumCase('foo', 'bar')
                ]
            )
        );
        static::assertEquals(
            [
                new EnumCase('foo', 'bar'),
                new EnumCase('foo', 'bop'),
            ],
            array_unique(
                [
                    new EnumCase('foo', 'bar'),
                    new EnumCase('foo', 'bop')
                ]
            )
        );
    }
}

What do you think?

PrinsFrank commented 8 months ago

FYI: there's currently no location the EnumTestCase would make sense in, so you can move the files from tests/Unit/ to tests/src/Unit and add a new tests/dev folder I guess?

PrinsFrank commented 8 months ago

This looks like a change we'll need anyway, so I'll merge this. Thanks!!

szepeviktor commented 8 months ago

You know what you do!