Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contravariant test which fails due to missing in the from part #702

Draft
wants to merge 2 commits into
base: 8.2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ static function changed1($a, $b) {}
function changed2($a, $b) {}
}
}

namespace N5 {
interface A {}
class C {
public function changed(A $a) {}
}
}
PHP
,
$astLocator,
Expand Down Expand Up @@ -126,6 +133,14 @@ static function changed1(int $a, int $b) {}
function changed2(int $a, int $b) {}
}
}

namespace N5 {
interface A extends B {}
interface B {}
Comment on lines +138 to +139
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming in this test makes things a bit complex to follow.

Let's rename A and B to make the sorting more clear instead.

B used in both examples as "child interface".

class C {
public function changed(B $a) {}
}
}
PHP
,
$astLocator,
Expand Down Expand Up @@ -185,6 +200,11 @@ function changed2(int $a, int $b) {}
'[BC] CHANGED: The parameter $b of N4\C#changed2() changed from no type to a non-contravariant int',
],
],
'N5\C#changed' => [
self::getMethod($fromReflector->reflectClass('N5\C'), 'changed'),
self::getMethod($toReflector->reflectClass('N5\C'), 'changed'),
[],
],
],
);
}
Expand Down
40 changes: 40 additions & 0 deletions test/unit/DetectChanges/Variance/TypeIsContravariantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,46 @@ public function existingNullableTypeStrings(): array
];
}

public function testContravarianceWhereClassWasMissingBefore(): void
{
$astLocator = (new BetterReflection())->astLocator();

$fromLocator = new StringSourceLocator(
<<<'PHP'
<?php
namespace {
interface ChildInterface {}
class Service {
public function __invoke(ChildInterface $a) {}
}
}
PHP
,
$astLocator,
);

$toLocator = new StringSourceLocator(
<<<'PHP'
<?php
namespace {
interface ChildInterface extends ParentInterface {}
interface ParentInterface {}
class Service {
public function __invoke(ParentInterface $a) {}
}
}
PHP
,
$astLocator,
);

$isContravariant = new TypeIsContravariant();
self::assertTrue($isContravariant(
(new DefaultReflector($fromLocator))->reflectClass('Service')->getMethod('__invoke')->getParameter('a')->getType(),
(new DefaultReflector($toLocator))->reflectClass('Service')->getMethod('__invoke')->getParameter('a')->getType()
));
}

private static function identifierType(
Reflector $reflector,
ReflectionProperty $owner,
Expand Down