Crell / enum-comparison

A comparison of enumerations and similar features in different languages
82 stars 7 forks source link

Override getDeclaringClass() to return ReflectionEnum instead of getEnum() #65

Closed iluuu1994 closed 3 years ago

iluuu1994 commented 3 years ago

I'm thinking about ways to make reflection simpler. This is one such thing. Instead of adding a redundant ReflectionEnumCase::getEnum() method we could simply override ReflectionClassConst::getDeclaringClass() and return a ReflectionEnum instance instead.

Crell commented 3 years ago

So... You'd end up with this?

$r = new ReflectionEnumCase(Suit::Hearts);
$re = $r->getDeclaringClass();
$re instanceof ReflectionEnum; // true

$r2 = new ReflectionEnumCase(Suit::ConstThatReferencesHearts)->getDeclaringClass();
$r2 instanceof ReflectionEnum; // false
$r2 instanceof ReflectionClass; // true

I suppose that's Liskov safe, but still feels a little odd. The code seems less self-documenting to me.

bwoebi commented 3 years ago

$r2 would be still ReflectionEnum, as it's operating on the value. (and not the source of the const)

iluuu1994 commented 3 years ago

What @bwoebi said :slightly_smiling_face: If you tried new ReflectionEnumCase(Suit::class, 'ConstThatReferencesHearts') then yes, that should fail.

Crell commented 3 years ago

Is there anything to do here? Ilija?

iluuu1994 commented 3 years ago

I just tried it and php was giving me some trouble. Let's forget about it ^^