Skip to content

Commit

Permalink
checkImplicitMixed - test
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMystikJonas authored and ondrejmirtes committed Aug 24, 2022
1 parent 21ca601 commit c98cf80
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 1 deletion.
48 changes: 47 additions & 1 deletion tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ class CallMethodsRuleTest extends RuleTestCase

private bool $checkExplicitMixed = false;

private bool $checkImplicitMixed = false;

private int $phpVersion = PHP_VERSION_ID;

protected function getRule(): Rule
{
$reflectionProvider = $this->createReflectionProvider();
$ruleLevelHelper = new RuleLevelHelper($reflectionProvider, $this->checkNullables, $this->checkThisOnly, $this->checkUnionTypes, $this->checkExplicitMixed, false);
$ruleLevelHelper = new RuleLevelHelper($reflectionProvider, $this->checkNullables, $this->checkThisOnly, $this->checkUnionTypes, $this->checkExplicitMixed, $this->checkImplicitMixed);
return new CallMethodsRule(
new MethodCallCheck($reflectionProvider, $ruleLevelHelper, true, true),
new FunctionCallParametersCheck($ruleLevelHelper, new NullsafeCheck(), new PhpVersion($this->phpVersion), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true, true),
Expand Down Expand Up @@ -1552,6 +1554,50 @@ public function testExplicitMixed(bool $checkExplicitMixed, array $errors): void
$this->analyse([__DIR__ . '/data/check-explicit-mixed.php'], $errors);
}

public function dataImplicitMixed(): array
{
return [
[
true,
[
[
'Cannot call method foo() on mixed.',
16,
],
[
'Parameter #1 $i of method CheckImplicitMixedMethodCall\Bar::doBar() expects int, mixed given.',
42,
],
[
'Parameter #1 $i of method CheckImplicitMixedMethodCall\Bar::doBar() expects int, T given.',
65,
],
[
'Parameter #1 $cb of method CheckImplicitMixedMethodCall\CallableMixed::doBar2() expects callable(): int, Closure(): mixed given.',
139,
],
],
],
[
false,
[],
],
];
}

/**
* @dataProvider dataImplicitMixed
* @param mixed[] $errors
*/
public function testImplicitMixed(bool $checkImplicitMixed, array $errors): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkImplicitMixed = $checkImplicitMixed;
$this->analyse([__DIR__ . '/data/check-implicit-mixed.php'], $errors);
}

public function testBug3409(): void
{
$this->checkThisOnly = false;
Expand Down
142 changes: 142 additions & 0 deletions tests/PHPStan/Rules/Methods/data/check-implicit-mixed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php // lint >= 8.0

namespace CheckImplicitMixedMethodCall;

class Foo
{

/**
* @param mixed $explicit
*/
public function doFoo(
$implicit,
$explicit
): void
{
$implicit->foo();
$explicit->foo();
}

/**
* @template T
* @param T $t
*/
public function doBar($t): void
{
$t->foo();
}

}

class Bar
{

/**
* @param mixed $explicit
*/
public function doFoo(
$implicit,
$explicit
): void
{
$this->doBar($implicit);
$this->doBar($explicit);

$this->doBaz($implicit);
$this->doBaz($explicit);
}

public function doBar(int $i): void
{

}

public function doBaz($mixed): void
{

}

/**
* @template T
* @param T $t
*/
public function doLorem($t): void
{
$this->doBar($t);
$this->doBaz($t);
}

}

class TemplateMixed
{

/**
* @template T
* @param T $t
*/
public function doFoo($t): void
{
$this->doBar($t);
}

public function doBar($mixed): void
{
$this->doFoo($mixed);
}

}

class CallableMixed
{

/**
* @param callable(int): void $cb
*/
public function doBar(callable $cb): void
{

}

/**
* @param callable() $cb
*/
public function doFoo2(callable $cb): void
{

}

/**
* @param callable(): int $cb
*/
public function doBar2(callable $cb): void
{

}

public function doLorem(int $i, $m): void
{
$acceptsInt = function (int $i): void {

};
$this->doBar($acceptsInt);

$acceptsMixed = function ($m): void {

};
$this->doBar($acceptsMixed);

$returnsInt = function () use ($i): int {
return $i;
};
$this->doFoo2($returnsInt);
$this->doBar2($returnsInt);

$returnsMixed = function () use ($m) {
return $m;
};
$this->doFoo2($returnsMixed);
$this->doBar2($returnsMixed);
}

}

0 comments on commit c98cf80

Please sign in to comment.