Skip to content

Commit

Permalink
Fixed level 9 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 1, 2022
1 parent 28179f2 commit 9f12f0f
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Type/StrictMixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,31 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic

public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
{
return TrinaryLogic::createFromBoolean(
$acceptingType instanceof MixedType && !$acceptingType instanceof TemplateMixedType,
);
if ($acceptingType instanceof self) {
return TrinaryLogic::createYes();
}
if ($acceptingType instanceof MixedType && !$acceptingType instanceof TemplateMixedType) {
return TrinaryLogic::createYes();
}

return TrinaryLogic::createMaybe();
}

public function isSuperTypeOf(Type $type): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($type instanceof self);
return TrinaryLogic::createYes();
}

public function isSubTypeOf(Type $otherType): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($otherType instanceof self);
if ($otherType instanceof self) {
return TrinaryLogic::createYes();
}
if ($otherType instanceof MixedType && !$otherType instanceof TemplateMixedType) {
return TrinaryLogic::createYes();
}

return TrinaryLogic::createMaybe();
}

public function equals(Type $type): bool
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2332,4 +2332,40 @@ public function testReadOnlyPropertyPassedByReference(): void
]);
}

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

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

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

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

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,10 @@ public function testBug4165(): void
$this->analyse([__DIR__ . '/data/bug-4165.php'], []);
}

public function testBug6053(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-6053.php'], []);
}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-6053.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Bug6053;

class HelloWorld
{
/**
* @param ?mixed $items
*
* @return ?array<mixed>
*/
public function processItems($items): ?array
{
if ($items === null || !is_array($items)) {
return null;
}

if ($items === []) {
return [];
}

$result = [];
foreach ($items as $item) {
$result[] = 'something';
}

return $result;
}
}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-6055.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Bug6055;

class HelloWorld
{
public function sayHello(): void
{
$this->check(null);

$this->check(array_merge(
['key1' => true],
['key2' => 'value']
));
}

/**
* @param ?array<mixed> $items
*/
private function check(?array $items): void
{
}
}
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-6081.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bug6081;

class HelloWorld
{
/** @param mixed[]|string $thing */
public function something($thing) : void{}

/** @param int[] $array */
public function sayHello(array $array): void
{
$this->something($array);
if(count($array) !== 0){
$this->something($array);
}
}

}
68 changes: 68 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-6118.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Bug6118;


/**
* @template-covariant T of mixed
*/
class Element {
/** @var T */
public mixed $value;

/**
* @param Element<mixed> $element
*/
function getValue(Element $element) : void {
}

/**
* @param Element<int> $element
*/
function takesValue(Element $element) : void {
$this->getValue($element);
}


/**
* @param Element<int|null> $element
*/
function getValue2(Element $element) : void {
}

/**
* @param Element<int> $element
*/
function takesValue2(Element $element) : void {
getValue2($element);
}
}

/**
* @template-covariant T of string|int|array<mixed>
*/
interface Example {
/**
* @return T|null
*/
public function normalize(): string|int|array|null;
}

/**
* @implements Example<string|int|array<mixed>>
*/
class Foo implements Example {
public function normalize(): int
{
return 0;
}
/**
* @param Example<int|string|array<mixed>> $example
*/
function foo(Example $example): void {
}

function bar(): void {
$this->foo(new Foo());
}
}
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-6236.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bug6236;


class HelloWorld
{
/**
* @param \Traversable<mixed, \DateTime> $t
*/
public static function sayHello(\Traversable $t): void
{
}

/**
* @param \SplObjectStorage<\DateTime, \DateTime> $foo
*/
public function doFoo($foo)
{
$this->sayHello(new \ArrayIterator([new \DateTime()]));

$this->sayHello(new \ArrayIterator(['a' => new \DateTime()]));

$this->sayHello($foo);
}
}

0 comments on commit 9f12f0f

Please sign in to comment.