PHP code example of fkupper / psalm-laravel-collections
1. Go to this page and download the library: Download fkupper/psalm-laravel-collections library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fkupper / psalm-laravel-collections example snippets
/** @var Collection<int,string> */
$c = new Collection(["a", "b", "c"]);
// items type is deinfed as string, cannot add int
$c->add(1);
/**
* @param Collection<string,\Exception>
*/
function(Collection $coll): void {
$exception = $coll->first();
// psalm will report this typo
$exception->getMassage();
}
/**
* @param Collection<int,string>
*/
function(Collection $coll): int {
$value = $coll->first();
// psalm will remind you forgot to cast that $value to int
return $value + 1;
}
/**
* This function is using wrong types in the filter Closure params.
* @param Collection<int,string>
*/
function(Collection $coll): void {
$filteredValues = $coll->filter(
// psalm will tell you that the Closure params are wrong
function (bool $value, float $key): bool {
return true;
}
)
}
/**
* @param Collection<int,string>
*/
function(Collection $coll): void {
$filteredValues = $coll->filter(
function (int $value, string $key): bool {
return true;
}
)
// psalm understands that the result of the filter call
// is a collection of same type as it was before, so the value
// type is still string, therefore array cannot be added
$filteredValues->add(['something']);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.