Reflection icon indicating copy to clipboard operation
Reflection copied to clipboard

Constants - No way to determine type of value.

Open donatj opened this issue 6 years ago • 7 comments

Working with the newest develop branch, calling getValue on a PHP Constant always returns the value as a string regardless of the constants given type.

This is a problem for me because I need to note the exact types of my given constants.

It would be very helpful for me if either getValue() gave the value in it's defined type or if there were some way to check it on the Constant object.

donatj avatar Oct 28 '19 22:10 donatj

As a workaround in the meantime I'm doing constant( (string)$constant->getFqsen() ) but it feels pretty gross.

donatj avatar Oct 29 '19 20:10 donatj

Additionally this is a somewhat dangerous work around for my purposes as it requires me to include the parsed files, which may have side effects or just generally execute things. It's not a long term solution by any means.

donatj avatar Oct 29 '19 20:10 donatj

Hey Donatj,

The value is returned as a string to prevent accidental conversions and this is how it is received from the Reflected information of the constant. Constants do expose their type when provided using a DocBlock, but it is not inferred.

Can you describe your use-case in more detail so I can make an assessment on how this impacts you?

mvriel avatar Jan 04 '20 19:01 mvriel

We have a doc service built on this library which has headings on classes that outputs something like:

Class Settings\SettingsTypes

class SettingsTypes {
	const BOOK_QUIZZES = 1;
	const BOOK_REVIEWS = 2;
	// ...
}

Not being able to determine the type, I can't safely know whether to document that as const BOOK_QUIZZES = 1 or const BOOK_QUIZZES = "1".

Not being able to tell a NULL from "" also is a major hindrance.

Constants do expose their type when provided using a DocBlock, but it is not inferred.

Outside of the examples on this project, I can't find a single instance of a const 's type being documented in the wild. I believe that's because they have never needed to be, because they are completely safely inferable as they are constant.

Presumably at the time you parsed the value you had to know the type being parsed. I think providing that data, as the previous version of this library did, is a win for everyone.

donatj avatar Jan 06 '20 16:01 donatj

I also can't find any information on documenting of a constant's type in https://docs.phpdoc.org/

donatj avatar Jan 06 '20 16:01 donatj

I have looked at this, and it is not that easy as it looks like. The main issue is that the definition of constants can be relative complex in newer php versions where nested arrays with all kind of mixed types are allowed. So I need to have a better look at this before we can accutally implement something that would work and that does represent the type of a constant correctly.

For now it could help you to define a docblock above your constants.

/**
 * @var int
 */
const MY_CONST=1

But as a see it now it should be possible to calculate the type from the nodes php-parser is reporting but I want to solve that is a good way. Not just by adding support for scalar constants.

jaapio avatar Feb 07 '20 15:02 jaapio

I have recently made a change that may have improved this but I haven't verified this issue yet; you can try or wait until I am able to verify it

mvriel avatar Feb 07 '20 16:02 mvriel