mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 12:55:26 +01:00
Merge pull request #6890 from zoonru/imagickpixel_color
Add ImagickPixel::getColor return type provider
This commit is contained in:
commit
f71273c171
@ -5980,7 +5980,7 @@ return [
|
||||
'ImagickPixel::clear' => ['bool'],
|
||||
'ImagickPixel::clone' => ['void'],
|
||||
'ImagickPixel::destroy' => ['bool'],
|
||||
'ImagickPixel::getColor' => ['array{r: int|float, g: int|float, b: int|float, a: int|float}', 'normalized='=>'int'],
|
||||
'ImagickPixel::getColor' => ['array{r: int|float, g: int|float, b: int|float, a: int|float}', 'normalized='=>'0|1|2'],
|
||||
'ImagickPixel::getColorAsString' => ['string'],
|
||||
'ImagickPixel::getColorCount' => ['int'],
|
||||
'ImagickPixel::getColorQuantum' => ['mixed'],
|
||||
|
@ -3161,7 +3161,7 @@ return [
|
||||
'ImagickPixel::clear' => ['bool'],
|
||||
'ImagickPixel::clone' => ['void'],
|
||||
'ImagickPixel::destroy' => ['bool'],
|
||||
'ImagickPixel::getColor' => ['array{r: int|float, g: int|float, b: int|float, a: int|float}', 'normalized='=>'int'],
|
||||
'ImagickPixel::getColor' => ['array{r: int|float, g: int|float, b: int|float, a: int|float}', 'normalized='=>'0|1|2'],
|
||||
'ImagickPixel::getColorAsString' => ['string'],
|
||||
'ImagickPixel::getColorCount' => ['int'],
|
||||
'ImagickPixel::getColorQuantum' => ['mixed'],
|
||||
|
@ -47,6 +47,7 @@ class MethodReturnTypeProvider
|
||||
self::$legacy_handlers = [];
|
||||
|
||||
$this->registerClass(ReturnTypeProvider\DomNodeAppendChild::class);
|
||||
$this->registerClass(ReturnTypeProvider\ImagickPixelColorReturnTypeProvider::class);
|
||||
$this->registerClass(ReturnTypeProvider\SimpleXmlElementAsXml::class);
|
||||
$this->registerClass(ReturnTypeProvider\PdoStatementReturnTypeProvider::class);
|
||||
$this->registerClass(ReturnTypeProvider\ClosureFromCallableReturnTypeProvider::class);
|
||||
|
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
namespace Psalm\Internal\Provider\ReturnTypeProvider;
|
||||
|
||||
use Psalm\Plugin\EventHandler\Event\MethodReturnTypeProviderEvent;
|
||||
use Psalm\Type;
|
||||
use Psalm\Type\Atomic\TIntRange;
|
||||
use Psalm\Type\Atomic\TKeyedArray;
|
||||
use Psalm\Type\Atomic\TLiteralInt;
|
||||
use Psalm\Type\Union;
|
||||
|
||||
use function assert;
|
||||
use function in_array;
|
||||
|
||||
class ImagickPixelColorReturnTypeProvider implements \Psalm\Plugin\EventHandler\MethodReturnTypeProviderInterface
|
||||
{
|
||||
public static function getClassLikeNames() : array
|
||||
{
|
||||
return ['imagickpixel'];
|
||||
}
|
||||
|
||||
public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Type\Union
|
||||
{
|
||||
$source = $event->getSource();
|
||||
$call_args = $event->getCallArgs();
|
||||
$method_name_lowercase = $event->getMethodNameLowercase();
|
||||
|
||||
if ($method_name_lowercase !== 'getcolor') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$source instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$call_args) {
|
||||
$formats = [0 => true];
|
||||
} else {
|
||||
$normalized = $source->node_data->getType($call_args[0]->value) ?? Type::getMixed();
|
||||
$formats = [];
|
||||
foreach ($normalized->getAtomicTypes() as $t) {
|
||||
if ($t instanceof TLiteralInt && in_array($t->value, [0, 1, 2], true)) {
|
||||
$formats[$t->value] = true;
|
||||
} else {
|
||||
$formats[0] = true;
|
||||
$formats[1] = true;
|
||||
$formats[2] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
$types = [];
|
||||
if (isset($formats[0])) {
|
||||
$types []= new Union([
|
||||
new TKeyedArray([
|
||||
'r' => new Union([new TIntRange(0, 255)]),
|
||||
'g' => new Union([new TIntRange(0, 255)]),
|
||||
'b' => new Union([new TIntRange(0, 255)]),
|
||||
'a' => new Union([new TIntRange(0, 1)])
|
||||
])
|
||||
]);
|
||||
}
|
||||
if (isset($formats[1])) {
|
||||
$types []= new Union([
|
||||
new TKeyedArray([
|
||||
'r' => Type::getFloat(),
|
||||
'g' => Type::getFloat(),
|
||||
'b' => Type::getFloat(),
|
||||
'a' => Type::getFloat()
|
||||
])
|
||||
]);
|
||||
}
|
||||
if (isset($formats[2])) {
|
||||
$types []= new Union([
|
||||
new TKeyedArray([
|
||||
'r' => new Union([new TIntRange(0, 255)]),
|
||||
'g' => new Union([new TIntRange(0, 255)]),
|
||||
'b' => new Union([new TIntRange(0, 255)]),
|
||||
'a' => new Union([new TIntRange(0, 255)])
|
||||
])
|
||||
]);
|
||||
}
|
||||
|
||||
assert($types !== []);
|
||||
return Type::combineUnionTypeArray($types, $event->getSource()->getCodebase());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user