Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

PHP Docs: int/bool vs integer/boolean #22

Closed
natanfelles opened this issue Feb 14, 2018 · 4 comments
Closed

PHP Docs: int/bool vs integer/boolean #22

natanfelles opened this issue Feb 14, 2018 · 4 comments

Comments

@natanfelles
Copy link

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.

captura de tela de 2018-02-14 12-55-33

@louisl
Copy link
Collaborator

louisl commented Feb 14, 2018

Thanks for the report, I'll have to look into the PHP Codesniffer core for this one I think.

@natanfelles
Copy link
Author

natanfelles commented Feb 16, 2018

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(), 😄 .

@louisl
Copy link
Collaborator

louisl commented Feb 16, 2018

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.

@TomasVotruba
Copy link

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/

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants