From 6ed452a00d77a9cff7ea107b46ae04f94002f591 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 10 Oct 2022 12:54:54 +0200 Subject: [PATCH] Fixed further nullability issues around `ReflectionClass#getMethod()` Ref: https://github.com/Roave/BetterReflection/issues/1275 --- .../ParameterDefaultValueChangedTest.php | 20 +++++++++++++++---- .../ParameterTypeChangedTest.php | 20 +++++++++++++++---- ...ParameterTypeContravarianceChangedTest.php | 20 +++++++++++++++---- .../RequiredParameterAmountIncreasedTest.php | 20 +++++++++++++++---- .../ReturnTypeByReferenceChangedTest.php | 20 +++++++++++++++---- .../FunctionBased/ReturnTypeChangedTest.php | 20 +++++++++++++++---- 6 files changed, 96 insertions(+), 24 deletions(-) diff --git a/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterDefaultValueChangedTest.php b/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterDefaultValueChangedTest.php index 992d0d8d..0e332395 100644 --- a/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterDefaultValueChangedTest.php +++ b/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterDefaultValueChangedTest.php @@ -8,6 +8,7 @@ use Roave\BackwardCompatibility\Change; use Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterDefaultValueChanged; use Roave\BetterReflection\BetterReflection; +use Roave\BetterReflection\Reflection\ReflectionClass; use Roave\BetterReflection\Reflection\ReflectionFunction; use Roave\BetterReflection\Reflection\ReflectionMethod; use Roave\BetterReflection\Reflector\DefaultReflector; @@ -17,6 +18,7 @@ use function array_keys; use function array_map; use function array_merge; +use function assert; use function iterator_to_array; /** @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterDefaultValueChanged */ @@ -131,16 +133,26 @@ function changed2($a = 2) {} ), [ 'C::changed1' => [ - $fromReflector->reflectClass('C')->getMethod('changed1'), - $toReflector->reflectClass('C')->getMethod('changed1'), + self::getMethod($fromReflector->reflectClass('C'), 'changed1'), + self::getMethod($toReflector->reflectClass('C'), 'changed1'), ['[BC] CHANGED: Default parameter value for parameter $a of C::changed1() changed from 1 to 2'], ], 'C#changed2' => [ - $fromReflector->reflectClass('C')->getMethod('changed2'), - $toReflector->reflectClass('C')->getMethod('changed2'), + self::getMethod($fromReflector->reflectClass('C'), 'changed2'), + self::getMethod($toReflector->reflectClass('C'), 'changed2'), ['[BC] CHANGED: Default parameter value for parameter $a of C#changed2() changed from 1 to 2'], ], ], ); } + + /** @param non-empty-string $name */ + private static function getMethod(ReflectionClass $class, string $name): ReflectionMethod + { + $method = $class->getMethod($name); + + assert($method !== null); + + return $method; + } } diff --git a/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterTypeChangedTest.php b/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterTypeChangedTest.php index dc825f28..f12dfe33 100644 --- a/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterTypeChangedTest.php +++ b/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterTypeChangedTest.php @@ -8,6 +8,7 @@ use Roave\BackwardCompatibility\Change; use Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterTypeChanged; use Roave\BetterReflection\BetterReflection; +use Roave\BetterReflection\Reflection\ReflectionClass; use Roave\BetterReflection\Reflection\ReflectionFunction; use Roave\BetterReflection\Reflection\ReflectionMethod; use Roave\BetterReflection\Reflector\DefaultReflector; @@ -17,6 +18,7 @@ use function array_keys; use function array_map; use function array_merge; +use function assert; use function iterator_to_array; /** @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterTypeChanged */ @@ -166,8 +168,8 @@ function changed2(int $a, int $b) {} ), [ 'N4\C::changed1' => [ - $fromReflector->reflectClass('N4\C')->getMethod('changed1'), - $toReflector->reflectClass('N4\C')->getMethod('changed1'), + self::getMethod($fromReflector->reflectClass('N4\C'), 'changed1'), + self::getMethod($toReflector->reflectClass('N4\C'), 'changed1'), [ '[BC] CHANGED: The parameter $a of N4\C::changed1() changed from no type to int', '[BC] CHANGED: The parameter $b of N4\C::changed1() changed from no type to int', @@ -175,8 +177,8 @@ function changed2(int $a, int $b) {} ], ], 'N4\C#changed2' => [ - $fromReflector->reflectClass('N4\C')->getMethod('changed2'), - $toReflector->reflectClass('N4\C')->getMethod('changed2'), + self::getMethod($fromReflector->reflectClass('N4\C'), 'changed2'), + self::getMethod($toReflector->reflectClass('N4\C'), 'changed2'), [ '[BC] CHANGED: The parameter $a of N4\C#changed2() changed from no type to int', '[BC] CHANGED: The parameter $b of N4\C#changed2() changed from no type to int', @@ -185,4 +187,14 @@ function changed2(int $a, int $b) {} ], ); } + + /** @param non-empty-string $name */ + private static function getMethod(ReflectionClass $class, string $name): ReflectionMethod + { + $method = $class->getMethod($name); + + assert($method !== null); + + return $method; + } } diff --git a/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterTypeContravarianceChangedTest.php b/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterTypeContravarianceChangedTest.php index 14dcecfb..01e902ff 100644 --- a/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterTypeContravarianceChangedTest.php +++ b/test/unit/DetectChanges/BCBreak/FunctionBased/ParameterTypeContravarianceChangedTest.php @@ -9,6 +9,7 @@ use Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterTypeContravarianceChanged; use Roave\BackwardCompatibility\DetectChanges\Variance\TypeIsContravariant; use Roave\BetterReflection\BetterReflection; +use Roave\BetterReflection\Reflection\ReflectionClass; use Roave\BetterReflection\Reflection\ReflectionFunction; use Roave\BetterReflection\Reflection\ReflectionMethod; use Roave\BetterReflection\Reflector\DefaultReflector; @@ -18,6 +19,7 @@ use function array_keys; use function array_map; use function array_merge; +use function assert; use function iterator_to_array; /** @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterTypeContravarianceChanged */ @@ -167,8 +169,8 @@ function changed2(int $a, int $b) {} ), [ 'N4\C::changed1' => [ - $fromReflector->reflectClass('N4\C')->getMethod('changed1'), - $toReflector->reflectClass('N4\C')->getMethod('changed1'), + self::getMethod($fromReflector->reflectClass('N4\C'), 'changed1'), + self::getMethod($toReflector->reflectClass('N4\C'), 'changed1'), [ '[BC] CHANGED: The parameter $a of N4\C::changed1() changed from no type to a non-contravariant int', '[BC] CHANGED: The parameter $b of N4\C::changed1() changed from no type to a non-contravariant int', @@ -176,8 +178,8 @@ function changed2(int $a, int $b) {} ], ], 'N4\C#changed2' => [ - $fromReflector->reflectClass('N4\C')->getMethod('changed2'), - $toReflector->reflectClass('N4\C')->getMethod('changed2'), + self::getMethod($fromReflector->reflectClass('N4\C'), 'changed2'), + self::getMethod($toReflector->reflectClass('N4\C'), 'changed2'), [ '[BC] CHANGED: The parameter $a of N4\C#changed2() changed from no type to a non-contravariant int', '[BC] CHANGED: The parameter $b of N4\C#changed2() changed from no type to a non-contravariant int', @@ -186,4 +188,14 @@ function changed2(int $a, int $b) {} ], ); } + + /** @param non-empty-string $name */ + private static function getMethod(ReflectionClass $class, string $name): ReflectionMethod + { + $method = $class->getMethod($name); + + assert($method !== null); + + return $method; + } } diff --git a/test/unit/DetectChanges/BCBreak/FunctionBased/RequiredParameterAmountIncreasedTest.php b/test/unit/DetectChanges/BCBreak/FunctionBased/RequiredParameterAmountIncreasedTest.php index 06036c04..43034295 100644 --- a/test/unit/DetectChanges/BCBreak/FunctionBased/RequiredParameterAmountIncreasedTest.php +++ b/test/unit/DetectChanges/BCBreak/FunctionBased/RequiredParameterAmountIncreasedTest.php @@ -8,6 +8,7 @@ use Roave\BackwardCompatibility\Change; use Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\RequiredParameterAmountIncreased; use Roave\BetterReflection\BetterReflection; +use Roave\BetterReflection\Reflection\ReflectionClass; use Roave\BetterReflection\Reflection\ReflectionFunction; use Roave\BetterReflection\Reflection\ReflectionMethod; use Roave\BetterReflection\Reflector\DefaultReflector; @@ -17,6 +18,7 @@ use function array_keys; use function array_map; use function array_merge; +use function assert; use function iterator_to_array; /** @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\RequiredParameterAmountIncreased */ @@ -140,16 +142,26 @@ function changed2($a, $b, $c, $d) {} ), [ 'N1\C::changed1' => [ - $fromReflector->reflectClass('N1\C')->getMethod('changed1'), - $toReflector->reflectClass('N1\C')->getMethod('changed1'), + self::getMethod($fromReflector->reflectClass('N1\C'), 'changed1'), + self::getMethod($toReflector->reflectClass('N1\C'), 'changed1'), ['[BC] CHANGED: The number of required arguments for N1\C::changed1() increased from 3 to 4'], ], 'N1\C#changed2' => [ - $fromReflector->reflectClass('N1\C')->getMethod('changed2'), - $toReflector->reflectClass('N1\C')->getMethod('changed2'), + self::getMethod($fromReflector->reflectClass('N1\C'), 'changed2'), + self::getMethod($toReflector->reflectClass('N1\C'), 'changed2'), ['[BC] CHANGED: The number of required arguments for N1\C#changed2() increased from 3 to 4'], ], ], ); } + + /** @param non-empty-string $name */ + private static function getMethod(ReflectionClass $class, string $name): ReflectionMethod + { + $method = $class->getMethod($name); + + assert($method !== null); + + return $method; + } } diff --git a/test/unit/DetectChanges/BCBreak/FunctionBased/ReturnTypeByReferenceChangedTest.php b/test/unit/DetectChanges/BCBreak/FunctionBased/ReturnTypeByReferenceChangedTest.php index 2dbbcdd9..ee38b00d 100644 --- a/test/unit/DetectChanges/BCBreak/FunctionBased/ReturnTypeByReferenceChangedTest.php +++ b/test/unit/DetectChanges/BCBreak/FunctionBased/ReturnTypeByReferenceChangedTest.php @@ -8,6 +8,7 @@ use Roave\BackwardCompatibility\Change; use Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ReturnTypeByReferenceChanged; use Roave\BetterReflection\BetterReflection; +use Roave\BetterReflection\Reflection\ReflectionClass; use Roave\BetterReflection\Reflection\ReflectionFunction; use Roave\BetterReflection\Reflection\ReflectionMethod; use Roave\BetterReflection\Reflector\DefaultReflector; @@ -17,6 +18,7 @@ use function array_keys; use function array_map; use function array_merge; +use function assert; use function iterator_to_array; /** @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ReturnTypeByReferenceChanged */ @@ -122,16 +124,26 @@ function & changed2() {} ), [ 'N1\C::changed1' => [ - $fromReflector->reflectClass('N1\C')->getMethod('changed1'), - $toReflector->reflectClass('N1\C')->getMethod('changed1'), + self::getMethod($fromReflector->reflectClass('N1\C'), 'changed1'), + self::getMethod($toReflector->reflectClass('N1\C'), 'changed1'), ['[BC] CHANGED: The return value of N1\C::changed1() changed from by-value to by-reference'], ], 'N1\C#changed2' => [ - $fromReflector->reflectClass('N1\C')->getMethod('changed2'), - $toReflector->reflectClass('N1\C')->getMethod('changed2'), + self::getMethod($fromReflector->reflectClass('N1\C'), 'changed2'), + self::getMethod($toReflector->reflectClass('N1\C'), 'changed2'), ['[BC] CHANGED: The return value of N1\C#changed2() changed from by-value to by-reference'], ], ], ); } + + /** @param non-empty-string $name */ + private static function getMethod(ReflectionClass $class, string $name): ReflectionMethod + { + $method = $class->getMethod($name); + + assert($method !== null); + + return $method; + } } diff --git a/test/unit/DetectChanges/BCBreak/FunctionBased/ReturnTypeChangedTest.php b/test/unit/DetectChanges/BCBreak/FunctionBased/ReturnTypeChangedTest.php index c8809c0d..c9ecdb76 100644 --- a/test/unit/DetectChanges/BCBreak/FunctionBased/ReturnTypeChangedTest.php +++ b/test/unit/DetectChanges/BCBreak/FunctionBased/ReturnTypeChangedTest.php @@ -8,6 +8,7 @@ use Roave\BackwardCompatibility\Change; use Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ReturnTypeChanged; use Roave\BetterReflection\BetterReflection; +use Roave\BetterReflection\Reflection\ReflectionClass; use Roave\BetterReflection\Reflection\ReflectionFunction; use Roave\BetterReflection\Reflection\ReflectionMethod; use Roave\BetterReflection\Reflector\DefaultReflector; @@ -17,6 +18,7 @@ use function array_keys; use function array_map; use function array_merge; +use function assert; use function iterator_to_array; /** @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ReturnTypeChanged */ @@ -156,16 +158,26 @@ function changed2() {} ), [ 'N4\C::changed1' => [ - $fromReflector->reflectClass('N4\C')->getMethod('changed1'), - $toReflector->reflectClass('N4\C')->getMethod('changed1'), + self::getMethod($fromReflector->reflectClass('N4\C'), 'changed1'), + self::getMethod($toReflector->reflectClass('N4\C'), 'changed1'), ['[BC] CHANGED: The return type of N4\C::changed1() changed from int to no type'], ], 'N4\C#changed2' => [ - $fromReflector->reflectClass('N4\C')->getMethod('changed2'), - $toReflector->reflectClass('N4\C')->getMethod('changed2'), + self::getMethod($fromReflector->reflectClass('N4\C'), 'changed2'), + self::getMethod($toReflector->reflectClass('N4\C'), 'changed2'), ['[BC] CHANGED: The return type of N4\C#changed2() changed from int to no type'], ], ], ); } + + /** @param non-empty-string $name */ + private static function getMethod(ReflectionClass $class, string $name): ReflectionMethod + { + $method = $class->getMethod($name); + + assert($method !== null); + + return $method; + } }