Skip to content

Commit

Permalink
ImagickPixel::getColor() normalized param accepts int instead of bool
Browse files Browse the repository at this point in the history
  • Loading branch information
blankse committed Nov 28, 2022
1 parent b2243dc commit 70a87cc
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ parameters:
- ../stubs/ArrayObject.stub
- ../stubs/WeakReference.stub
- ../stubs/ext-ds.stub
- ../stubs/ImagickPixel.stub
- ../stubs/PDOStatement.stub
- ../stubs/date.stub
- ../stubs/mysqli.stub
Expand Down
2 changes: 1 addition & 1 deletion resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5166,7 +5166,7 @@
'ImagickPixel::clear' => ['bool'],
'ImagickPixel::clone' => ['void'],
'ImagickPixel::destroy' => ['bool'],
'ImagickPixel::getColor' => ['array', 'normalized='=>'bool'],
'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'],
Expand Down
9 changes: 9 additions & 0 deletions stubs/ImagickPixel.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class ImagickPixel
{
/**
* @return ($normalized is 0 ? array{r: int<0, 255>, g: int<0, 255>, b: int<0, 255>, a: int<0, 1>} : ($normalized is 1 ? array{r: float, g: float, b: float, a: float} : ($normalized is 2 ? array{r: int<0, 255>, g: int<0, 255>, b: int<0, 255>, a: int<0, 255>} : array{})))
*/
public function getColor(int $normalized = 0): array;
}
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8373.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Functions/data/bug-8389.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8421.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/imagick-pixel.php');
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Analyser/data/imagick-pixel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace ImagickPixelReturn;

use function PHPStan\Testing\assertType;

function (): void {
$imagickPixel = new \ImagickPixel('rgb(0, 0, 0)');

assertType('array{r: int<0, 255>, g: int<0, 255>, b: int<0, 255>, a: int<0, 1>}', $imagickPixel->getColor());
assertType('array{r: int<0, 255>, g: int<0, 255>, b: int<0, 255>, a: int<0, 1>}', $imagickPixel->getColor(0));
assertType('array{r: float, g: float, b: float, a: float}', $imagickPixel->getColor(1));
assertType('array{r: int<0, 255>, g: int<0, 255>, b: int<0, 255>, a: int<0, 255>}', $imagickPixel->getColor(2));
assertType('array{}', $imagickPixel->getColor(3));
};
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2675,4 +2675,13 @@ public function testBug5623(): void
$this->analyse([__DIR__ . '/data/bug-5623.php'], []);
}

public function testImagickPixel(): void
{
$this->checkThisOnly = false;
$this->checkNullables = false;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = false;
$this->analyse([__DIR__ . '/data/imagick-pixel.php'], []);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Methods/data/imagick-pixel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace ImagickPixelParam;

use ImagickPixel;

class HelloWorld
{
public function sayHello(ImagickPixel $pixel): void
{
$pixel->getColor();
$pixel->getColor(0);
$pixel->getColor(1);
$pixel->getColor(2);
}
}

0 comments on commit 70a87cc

Please sign in to comment.