bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.64k stars 94 forks source link

[Feature Request] Warn when type annotations are violated #694

Open Tarmean opened 5 years ago

Tarmean commented 5 years ago

Example:

/** @var string $str */
 $str = 1;

At the end of this snippet $str is inferred as string. It'd be really useful if violations to user specified types were highlighted.

bmewburn commented 5 years ago

Type checking is currently restricted to where type declarations have been used, so this could be expanded. Though for the example above, when an assignment has been annotated it usually means that the developer knows better than the type inference, so I'd be reluctant to show an error for this case.

KapitanOczywisty commented 5 years ago

If int is valid should be also included in @var. Intelephense shouldn't be weakened because some devs like to write bad code. As I mentioned in #695 this is great place to use declare(strict_types=1) or config option and warn about type misuses.

bmewburn commented 5 years ago

Fair enough. Agree that there should be some way of opting in for more thorough checking.

KapitanOczywisty commented 5 years ago

My reasoning is: If someone is using strict_types he already is trying to follow standards. However I'm expecting cases when strict checks could be desired, when use of declare is not possible (e.g. old projects).

Tarmean commented 5 years ago

It's a fair point that

/** @var int $foo */
$foo = $json['foo']

would give a warning which might not be wanted.

But I think there are always ways around this. In this case: