https://github.com/struggle-for-php/sfp-psalm-typed-local-variable-plugin
finding mismatch type assignment in function/method scope with psalm.
https://github.com/struggle-for-php/sfp-psalm-typed-local-variable-plugin
php psalm psalm-plugin
Last synced: 5 months ago
JSON representation
finding mismatch type assignment in function/method scope with psalm.
- Host: GitHub
- URL: https://github.com/struggle-for-php/sfp-psalm-typed-local-variable-plugin
- Owner: struggle-for-php
- Created: 2020-07-24T07:32:08.000Z (almost 6 years ago)
- Default Branch: 0.2.x
- Last Pushed: 2023-01-09T12:02:09.000Z (over 3 years ago)
- Last Synced: 2025-08-11T22:34:33.311Z (11 months ago)
- Topics: php, psalm, psalm-plugin
- Language: PHP
- Homepage:
- Size: 67.4 KB
- Stars: 16
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# sfp-psalm-typed-local-variable-plugin
finding mismatch type assignment in function/method scope with [psalm](https://psalm.dev/).
[](https://packagist.org/packages/struggle-for-php/sfp-psalm-typed-local-variable-plugin)
[](https://dashboard.stryker-mutator.io/reports/github.com/struggle-for-php/sfp-psalm-typed-local-variable-plugin/0.2.x)
[](https://shepherd.dev/github/struggle-for-php/sfp-psalm-typed-local-variable-plugin)
## Installation
```
$ composer require --dev struggle-for-php/sfp-psalm-typed-local-variable-plugin
$ vendor/bin/psalm-plugin enable struggle-for-php/sfp-psalm-typed-local-variable-plugin
```
latest version supports psalm `^4`.
## Demo
```php
repository->findOneById(1); // ERROR
}
function works_with_intersection() : void
{
/** @var \DateTimeInterface&Mock $date */
$date = new \DateTime('now'); // ERROR
$date = date_mock(); // success
}
}
```
```bash
$ ./vendor/bin/psalm -c demo.psalm.xml
Scanning files...
Analyzing files...
E
ERROR: InvalidScalarTypedLocalVariableIssue - demo/demo.php:23:28 - Type true should be a subtype of null|string
$nullable_string = true; // ERROR
ERROR: InvalidTypedLocalVariableIssue - demo/demo.php:30:21 - Type DateTime should be a subtype of DateTimeImmutable
$date = new \DateTime('tomorrow'); // ERROR
ERROR: InvalidScalarTypedLocalVariableIssue - demo/demo.php:35:17 - Type 1 should be a subtype of bool
$bool = 1; // ERROR
ERROR: InvalidTypedLocalVariableIssue - demo/demo.php:41:19 - Type Entity|null should be a subtype of Entity
$entity = $this->repository->findOneById(1); // ERROR
ERROR: InvalidTypedLocalVariableIssue - demo/demo.php:47:17 - Type DateTime should be a subtype of DateTimeInterface&Mock
$date = new \DateTime('now'); // ERROR
------------------------------
5 errors found
------------------------------
```
## Plugin Issues
All issue names has `TypedLocalVariableIssue` suffix.
* MixedTypeCoercionTypedLocalVariableIssue
* nearly [MixedArgumentTypeCoercion](https://psalm.dev/docs/running_psalm/issues/MixedArgumentTypeCoercion)
eg.
```php
function foo(array $a) : void {
/** @var string[] $x */
$x = $a;
}
```
* TypeCoercionTypedLocalVariableIssue
* nearly [ArgumentTypeCoercion](https://psalm.dev/docs/running_psalm/issues/ArgumentTypeCoercion)
eg.
```php
class A {}
class B extends A {}
function takesA(A $a) : void {
/** @var B $b */
$b = $a;
}
```
* InvalidScalarTypedLocalVariableIssue
* nearly [InvalidScalarArgument](https://psalm.dev/docs/running_psalm/issues/InvalidScalarArgument/)
* InvalidTypedLocalVariableIssue
* nearly [InvalidArgument](https://psalm.dev/docs/running_psalm/issues/InvalidArgument/)
If you want **suppress** specific issue, please setting `psalm.xml` like below.
```xml
```
## Disclaimer
This is **Experimental** plugin.
## Limitation
* NOT support global variables.
* NOT support variables in namespace.
* NOT support [Variable variables](https://php.net/language.variables.variable)
* Non-each inline VariableReference.
* eg.
```php
/** @var string $var1 */
/** @var bool $var2 */
$var1 = 'string'; // cannot determine type for $var1
// should fix like below
/** @var string $var1 */
$var1 = 'string';
/** @var bool $var2 */
$var2 = true;
```
## Todo
- [ ] optional setting for only from_docblock typed.