Skip to content
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
30 changes: 28 additions & 2 deletions src/DetectChanges/Variance/TypeIsContravariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

use Psl\Iter;
use Psl\Str;
use ReflectionProperty;
use Roave\BetterReflection\Reflection\ReflectionIntersectionType;
use Roave\BetterReflection\Reflection\ReflectionNamedType;
use Roave\BetterReflection\Reflection\ReflectionUnionType;
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
use Roave\BetterReflection\Reflector\Reflector;

use function assert;

/**
* This is a simplistic contravariant type check. A more appropriate approach would be to
Expand Down Expand Up @@ -101,13 +106,34 @@ private function compareNamedTypes(ReflectionNamedType $type, ReflectionNamedTyp
return false;
}

$typeReflectionClass = $type->getClass();
$comparedTypeClass = $comparedType->getClass();
$comparedTypeClass = $comparedType->getClass();

try {
// the old type has to be resolved in the new codebase, as its inheritance may have changed
$typeReflectionClass = $this->reflectorOf($comparedType)
->reflectClass($type->getClass()->getName());
} catch (IdentifierNotFound) {
// the old type no longer exists, so it cannot be a subtype of the new one
return false;
}

if ($comparedTypeClass->isInterface()) {
return $typeReflectionClass->implementsInterface($comparedTypeClass->getName());
}

return Iter\contains($typeReflectionClass->getParentClassNames(), $comparedTypeClass->getName());
}

/**
* @todo better-reflection does not expose the reflector a type was created with, therefore
* it is read from its private state here
*/
private function reflectorOf(ReflectionNamedType $type): Reflector
{
$reflector = (new ReflectionProperty($type::class, 'reflector'))->getValue($type);

assert($reflector instanceof Reflector);

return $reflector;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,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 @@ -125,6 +132,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 +137 to +138

Copy link
Copy Markdown
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 @@ -184,6 +199,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 @@ -564,6 +564,46 @@
];
}

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(),

Check failure on line 602 in test/unit/DetectChanges/Variance/TypeIsContravariantTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (Psalm [8.4, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyNullReference

test/unit/DetectChanges/Variance/TypeIsContravariantTest.php:602:118: PossiblyNullReference: Cannot call method getType on possibly null value (see https://psalm.dev/083)

Check failure on line 602 in test/unit/DetectChanges/Variance/TypeIsContravariantTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (Psalm [8.4, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyNullReference

test/unit/DetectChanges/Variance/TypeIsContravariantTest.php:602:99: PossiblyNullReference: Cannot call method getParameter on possibly null value (see https://psalm.dev/083)
(new DefaultReflector($toLocator))->reflectClass('Service')->getMethod('__invoke')->getParameter('a')->getType()

Check failure on line 603 in test/unit/DetectChanges/Variance/TypeIsContravariantTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.4, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Multi-line function calls must have a trailing comma after the last parameter.

Check failure on line 603 in test/unit/DetectChanges/Variance/TypeIsContravariantTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (Psalm [8.4, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyNullReference

test/unit/DetectChanges/Variance/TypeIsContravariantTest.php:603:116: PossiblyNullReference: Cannot call method getType on possibly null value (see https://psalm.dev/083)

Check failure on line 603 in test/unit/DetectChanges/Variance/TypeIsContravariantTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (Psalm [8.4, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyNullReference

test/unit/DetectChanges/Variance/TypeIsContravariantTest.php:603:97: PossiblyNullReference: Cannot call method getParameter on possibly null value (see https://psalm.dev/083)
));
}

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