Comments (3)
Note to future me: callable(T):void
should be valid in this context, but callable():T
shouldn't.
from psalm.
I found these snippets:
https://psalm.dev/r/2eb66f01fe
<?php
/**
* @template-covariant T
*/
class Example
{
/**
* @param T $value
*/
function __construct(private mixed $value)
{}
/**
* @param callable(T):void $callback
*/
function call(callable $callback): void
{
$callback($this->value);
}
}
Psalm output (using commit ef3b018):
ERROR: InvalidTemplateParam - 15:15 - Template param T of Example is marked covariant and cannot be used here
https://psalm.dev/r/3ebad94817
<?php
/**
* @template-covariant T
*/
class Example
{
/**
* @param T $value
*/
function __construct(private mixed $value)
{}
/**
* @psalm-suppress InvalidTemplateParam
* @param callable(T):void $callback
*/
function call(callable $callback): void
{
$callback($this->value);
}
}
class Animal {}
class Cat extends Animal {}
class Dog extends Animal {}
/** @psalm-suppress UnusedClosureParam */
function exampleWithObjects(): void
{
$example = new Example(new Cat());
$example->call(function (Animal $animal) {});
$example->call(function (Cat $animal) {});
$example->call(function (Dog $animal) {}); // OK (expected error)
}
/**
* @param Cat[] $cats
* @param callable(Animal[]):void $takesAnimals
* @param callable(Cat[]):void $takesCats
* @param callable(Dog[]):void $takesDogs
*/
function exampleWithArrays(
array $cats,
callable $takesAnimals,
callable $takesCats,
callable $takesDogs,
): void {
$example = new Example($cats);
$example->call($takesAnimals);
$example->call($takesCats);
$example->call($takesDogs); // OK (expected error)
}
/**
* @param \ArrayObject<int, Cat> $cats
* @param callable(\ArrayObject<int, Animal>):void $takesAnimals
* @param callable(\ArrayObject<int, Cat>):void $takesCats
* @param callable(\ArrayObject<int, Dog>):void $takesDogs
*/
function exampleWithArrayObjects(
\ArrayObject $cats,
callable $takesAnimals,
callable $takesCats,
callable $takesDogs,
): void {
$example = new Example($cats);
$example->call($takesAnimals); // OK (expected error)
$example->call($takesCats);
$example->call($takesDogs); // OK (expected error)
}
Psalm output (using commit ef3b018):
ERROR: InvalidArgument - 35:20 - Argument 1 of Example::call expects callable(Cat):void, but pure-Closure(Dog):void provided
ERROR: InvalidArgument - 54:20 - Argument 1 of Example::call expects callable(array<array-key, Cat>):void, but callable(array<array-key, Dog>):void provided
ERROR: InvalidArgument - 72:20 - Argument 1 of Example::call expects callable(ArrayObject<int, Cat>):void, but callable(ArrayObject<int, Animal>):void provided
ERROR: InvalidArgument - 74:20 - Argument 1 of Example::call expects callable(ArrayObject<int, Cat>):void, but callable(ArrayObject<int, Dog>):void provided
from psalm.
It would be awesome if Psalm supported this Scala machinery on callables and variance: https://scastie.scala-lang.org/ZbzouFbvRHGPSwwB8OrNoA
+A
stands for @template-covariant A
-A
is @template-contravariant A
Unit
is void
A => Boolean
is callable(A): bool
(A => Boolean) => Boolean
is callable(callable(A): bool): bool
from psalm.
Related Issues (20)
- ensureArrayString/Int/Offset seems to be unsound for non-literal array offsets HOT 7
- properties-of<Class> not compatible with array type HOT 3
- psalm not understanding properties-of<Class> assignment to a ParentClass property, if properties-of<ParentClass> is used HOT 1
- Unbound template type is not narrowed correctly HOT 2
- The "Generator" class is reserved for internal use and cannot be manually instantiated HOT 1
- Psalm adds float to inferred return type when two integer numbers are added together HOT 4
- Plugin exception details HOT 1
- Misplaced variable causes IncorrectDocblockException HOT 1
- Undetected Trust Boundary violation (CWE-501) HOT 1
- UnexpectedValueException $storage should not be null for PhpParser\Parser::gettokens HOT 1
- How to test against class-string<Foo> if the class might not exist yet? HOT 1
- Bug: Internal Psalm Error: Could not get class storage for <interface> HOT 2
- Converting iterable to array with iterator_to_array causes error HOT 1
- Invalid type for SQLite3Result::columnName HOT 2
- Broken @psalm-yield documentation example
- Getting MismatchingDocblockParamType error even when there's no docblock HOT 2
- Unable to infer not-null state HOT 1
- Trouble with switch case in try catch block HOT 1
- Supression is not working in echo shorthand HOT 1
- `@psalm-suppress InternalClass` doesn't work for internal attributes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from psalm.