Skip to content

Instantly share code, notes, and snippets.

@elazar
Created January 29, 2021 19:17
Show Gist options
  • Save elazar/be33d27ff623b6335b10b892d777dd46 to your computer and use it in GitHub Desktop.
Save elazar/be33d27ff623b6335b10b892d777dd46 to your computer and use it in GitHub Desktop.
Psalm Error Example
<?php
// This version replaces the redundant code with a recursive call, but emits these errors:
//
// ERROR: InvalidReturnType - src/Filter/FilterTransformer.php:111:16 - The declared return type 'Kiosk\Filter\PriceRange|array<array-key, Kiosk\Filter\PriceRange|float>|float' for Kiosk\Filter\FilterTransformer::getPrice is incorrect, got 'Kiosk\Filter\PriceRange|array<int|string(max)|string(min), Kiosk\Filter\PriceRange|array<array-key, Kiosk\Filter\PriceRange|float>|float>|float' (see https://psalm.dev/011)
// * @return float|float[]|PriceRange|PriceRange[]
//
// ERROR: InvalidReturnStatement - src/Filter/FilterTransformer.php:130:16 - The inferred type 'array<int|string(max)|string(min), Kiosk\Filter\PriceRange|array<array-key, Kiosk\Filter\PriceRange|float>|float>' does not match the declared return type 'Kiosk\Filter\PriceRange|array<array-key, Kiosk\Filter\PriceRange|float>|float' for Kiosk\Filter\FilterTransformer::getPrice (see https://psalm.dev/128)
// return array_map(
// [$this, 'getPrice'],
// $price
// );
/**
* @param string|float|array{min?: float|string, max?:float|string}|list<array{min?: float|string, max?:float|string}> $price
* @return float|float[]|PriceRange|PriceRange[]
*/
private function getPrice($price)
{
if (is_string($price) || is_float($price)) {
return (float) $price;
}
if (isset($price['min']) || isset($price['max'])) {
$range = new PriceRange;
if (isset($price['min'])) {
$range->setMin((float) $price['min']);
}
if (isset($price['max'])) {
$range->setMax((float) $price['max']);
}
return $range;
}
return array_map(
[$this, 'getPrice'],
$price
);
}
<?php
// This version produces no errors, but has repeated code in the array_map() call.
/**
* @param string|float|array{min?: float|string, max?:float|string}|list<array{min?: float|string, max?:float|string}> $price
* @return float|float[]|PriceRange|PriceRange[]
*/
private function getPrice($price)
{
if (is_string($price) || is_float($price)) {
return (float) $price;
}
if (isset($price['min']) || isset($price['max'])) {
$range = new PriceRange;
if (isset($price['min'])) {
$range->setMin((float) $price['min']);
}
if (isset($price['max'])) {
$range->setMax((float) $price['max']);
}
return $range;
}
return array_map(
/**
* @param string|float|array{min?: float|string, max?:float|string} $price
* @return float|PriceRange
*/
function ($price) {
if (is_string($price) || is_float($price)) {
return (float) $price;
}
$range = new PriceRange;
if (isset($price['min'])) {
$range->setMin((float) $price['min']);
}
if (isset($price['max'])) {
$range->setMax((float) $price['max']);
}
return $range;
},
$price
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment