-
-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Meaning of integer' and
float'
#69
Comments
@ruafozy You created func which not apply it's own interface, declared in php doc. You must check input arguments at least for the type. /**
* @param int $n
* @return int
* @throws \InvalidArgumentExeption
*/
function double($n) {
if (!is_int($n)) {
throw new \InvalidArgumentExeption('Param $n must be int');
}
return 2 * $n;
} Correct call is only |
There is an issue with this interpretation, that I believe @ruafozy was trying to point out, because of PHP's notorious type coercion behavior. First I think we all agree that these calls should throw exceptions (if the function is implemented to enforce it's contract, as @index0h suggested): $x += double(1.5); // call 1
$x += double("1.5"); // call 2
$x += double("abc"); // call 3 As @index0h suggests, we certainly could require the function argument be an actual integer type (with Look, for instance, at this alternate semantic interpretation of /**
* @param int $n
* @return int
* @throws \InvalidArgumentExeption
*/
function double($n) {
if (!preg_match('/^-?\\d+$/', "{$n}")) {
throw new \InvalidArgumentExeption(
sprintf('Param $n (%s) must be int', var_export($n, true)));
}
return 2 * intval($n, 10);
} I'm not saying that either interpretation is "correct", but I do believe that the semantic interpretation is more PHP like. What do you think? |
There is no such type as integer. PHP 7 defines the correct types as |
@GrahamCampbell, if we are discussing literal types, http://php.net/manual/en/language.types.integer.php refers to them pretty pervasively as "integers", but I believe the question is, are we (or should we be) talking about literal types or semantic types. We could, for instance support both literal integers, and semantic integers as distinct types, which would require distinct names. I am rather confident that distinguishing between literal and semantic integers as To clarify, I consider a literal integer to be a value stored as an integer, while a semantic integer would be an integer, a float or a string that represents a number with no fractional part. |
Weak type checks as defined for PHP7: https://wiki.php.net/rfc/scalar_type_hints_v5 The RFC includes a table that explains what is accepted and what is not (this also applies to previous PHP versions). |
Above you wrote that you "believe that the semantic $total = array_sum(file('php://stdin')); This is perfectly good PHP code, and it shows that it Because of PHP's type coercion, I believe that PHPdoc the PHPDoc type is called |
No, the type in php is called INT. |
@ruafozy, I see room in the spec for supporting both type hints. @GrahamCampbell, I welcome your feedback. |
As I see it, the types are to be interpreted in a semantical way as it is a means to communicate with a reader what sort of information is expected. I consider the enforcing of type correctness, or validating whether the contents are truly of that sort, something that should be solved in code and not in PHPDoc. I don't think it adds much for the reader's awareness whether you are talking about a literal integer or not. I think it should be a convention in a project how literal you interpret the documentation. |
It is an alias and that is why both keywords have to have the same meaning in PHPDoc as they have in PHP: they refer to semantically to an integer number.
Exactly and in the future |
@mvriel, I do think there are (rare) times when it does make sense to specify a literal storage type, like @Fleshgrinder, as these situations will probably be quite rare, I see no value in adding a project or file-wide setting. Can you provide an example where this would be useful? (If you're wanting project-wide type strictness, I suspect you're using the wrong language.) |
@loren-osborn Since the current wording does not require you to interpret it literal my thoughts are that if a specific project needs to interpret the types literally (because they added business logic in their project that needs that to be) then they should have a Documentation Standard or Coding Standard that extends PSR-1/2 and PSR-5 that states so. There is nothing in PSR-5 that specifies this is not allowed and to add specific rules for such rare occurrences that are not documentation related adds, in my opinion, unnecessary complexity. |
@loren-osborn and @mvriel the |
/**
* @param int $n <- Here you said that argument MUST be int, not should, not may, but must.
* @return int
* @throws \InvalidArgumentExeption
*/
function double($n) {
if (!preg_match('/^-?\\d+$/', "{$n}")) { // <- Here you cast argument to string, it's wrong flow.
throw new \InvalidArgumentExeption(
sprintf('Param $n (%s) must be int', var_export($n, true)));
}
return 2 * intval($n, 10);
} What will be if $n - Exception instance? So you must check only if argument is int, in any other cases developer uses func incorrectly. This situation could not be resolved automatically so you thrown an exception. |
@index0h, I regret I'm having a little trouble with your English, but I will try my best to answer. I apologize if this is long-winded. 😦 I see you bringing up 3 distinct issues: Role of the caller argument specificationThe first issue is the role of the caller argument specification
RFC 2119 specifies that MUST means:
... so if we look, for instance, at RFC 2045 (the MIME specification) which says compliant messages "MUST include" the "verbatim text" So, if we return to
The meaning of
|
Just found this https://github.com/eloquent/typhax where he's using |
@loren-osborn sorry for my English(( "semantic int" in your case means "anything that php can cast to int". It means that @return int could return anything that could be cast as int, it could be string for example. In your case it'l be correct. But it's way to nowhere. /**
* @param int $n
* @return int
* @throws \InvalidArgumentException
*/
function double($n) {
if (!is_int($n)) {
$trimmedN = trim("{$n}");
if (!is_numeric($trimmedN) || (abs($trimmedN - round($trimmedN)) > 0.001)) {
throw new \InvalidArgumentException(
sprintf('Param $n (%s) must be an integer', var_export($n, true)));
} else {
$n = intval(round($trimmedN), 10);
}
}
return strval(2 * $n);
} This example is disgusting, but it return semantic int. So in most part of methods I dont know what exactly they will work with and must cast every argument. In this case erros count will be grow. I offer to use strong type declaration. If some where in code my func doubleInt called with float or string - it means: my code aroud it is incorrect, it not applies doubleInt interface. But with semantic int I dont even know about the problem. Look, if you admit extra types in your function, like float or string - you should type it in dockblock |
Although I was pro weak types before, I would like to state that I changed my mind. I think that it is easier if the given type is to be interpreted in a strict manner. It is easier for the reader if a string is a string and an integer is an integer. I think it would be best to apply the PHP 7 rules for strict types to the types in the PHPDoc. This means that every type is absolutely strict with one exception: an integer without a fraction part is a valid float. |
Types in this PSR are intended to be semantic. This means that, for example, if a piece of code mentions an int then in reality it could be an integer, float without numeric part or strong containing a valid number. Applying strictness and guarding for such strictness is not the responsibility of the PHPDoc standard or DocBlocks in general. A project may decide to interpret a type more strict than is intended by this PSR but this is a project specific documentation standard item and should not be blocked by this PSR. This information should be clearly mentioned in the PSR and I will as this to my review list |
The PHPDoc draft fails to make it clear whether the types
integer
andfloat
are to be interpreted "semantically" or are defined in terms oftheir corresponding PHP type-test functions (in this case,
is_integer
andis_float
). I will illustrate the problem with the following code.With reference to that code:
$n
whichmakes all the calls correct?
The text was updated successfully, but these errors were encountered: