An open API service indexing awesome lists of open source software.

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.

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/).

[![Packagist](https://img.shields.io/packagist/v/struggle-for-php/sfp-psalm-typed-local-variable-plugin.svg)](https://packagist.org/packages/struggle-for-php/sfp-psalm-typed-local-variable-plugin)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fstruggle-for-php%2Fsfp-psalm-typed-local-variable-plugin%2F0.2.x)](https://dashboard.stryker-mutator.io/reports/github.com/struggle-for-php/sfp-psalm-typed-local-variable-plugin/0.2.x)
[![Psalm coverage](https://shepherd.dev/github/struggle-for-php/sfp-psalm-typed-local-variable-plugin/coverage.svg?)](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.