coding-standard
coding-standard copied to clipboard
PHP Docs: int/bool vs integer/boolean
Currently the int params and returns are being updated to integer. Same from bool to boolean.
The following doc (right):
/**
* @param int $n
*
* @return int
*/
public function a(int $n):int
{
return 2 * $n;
}
is updated to it (wrong):
/**
* @param integer $n
*
* @return integer
*/
public function a(int $n):int
{
return 2 * $n;
}
The issue is that @return integer is not int (a number)... On returns it must be int because updating to integer we say that we are returning an instance of the class integer... Then the type hint :int would have to be changed to :integer, but the PHP will show a fatal error. Because the correct would be:
/**
* @param integer $n
*
* @return integer
*/
public function a(int $n):integer
{
return new integer(); // An instanceof integer
}
The same applies to boolean's, while the short syntax is the correct.
See:
Test:
<?php
class Test
{
public function a(int $n):int
{
return 2 * $n;
}
/**
* [b description]
*
* @param integer $n
*
* @return integer
*/
public function b(integer $n):integer
{
return 2 * $n;
}
}
$test = new Test;
var_dump($test->a(5));
// int(10)
var_dump($test->b(5));
// PHP Fatal error: Uncaught TypeError: Argument 1 passed to Test::b() must be an instance of integer, integer given, called in...
The CodeIgniter4 Standard is not updating the method type hint, but the docs. What instructs wrong params and return types.
Detected with PHP Integrator.

Thanks for the report, I'll have to look into the PHP Codesniffer core for this one I think.
The conversion is done here. I do not know yet if is possible change it, or if php-integrator is that conflicts with that... since int is integer but not integer(), 😄 .
Thanks for tracking that down @natanfelles, I can definitely fix that. I've a deadline for Tuesday 20th so I'll be able to look at after that if I cant squeeze it in before.
Just dropping an approach I use on huge projects. Coding standard can break typehints in many ways, since they don't know the context of parent/child files:
<?php
final class SuperCoolSniff implements Sniff
{
/**
* @param int $position
*/
- public function process(File $file, $position)
+ public function process(File $file, int $position)
{
// ...
}
}
It's better to use AST for this in tools like Retor instead of token_get_all(): https://www.tomasvotruba.cz/blog/2018/12/10/rocket-science-behind-migration-of-docblock-types-to-php-typehints/