Skip to content

Commit

Permalink
MagicCallPatch: prevent deprecation notice
Browse files Browse the repository at this point in the history
Reflection Docblock has made some internal changes, including deprecating the `getArguments()` method in favour of `getParameters()`.

As Reflection Docblock updated the minimum PHP requirement to PHP 7.4 with the 5.4.0 release, to maintain the minimum supported version of PHP 7.2 of Prophecy, a toggle is needed to prevent the deprecation notice from Reflection Docblock.

Notes:
I did consider the alternative of raising the minimum PHP requirement to PHP 7..4.
In my opinion, this would be problematic for the following reason:
- PHPUnit 8 has a minimum PHP version of PHP 7.2.
- PHPUnit 9 has a minimum PHP version of PHP 7.3.
So, raising the minimum PHP requirement to PHP 7.4 means dropping support for PHPUnit 8. It also means that the PHPUnit 9 Phar file would need to include an outdated version of Prophecy as dependencies included in the PHAR file have to comply with the minimum supported PHP version of PHPUnit.

As PHPUnit 10 (and 11) brought huge changes, a lot of larger packages have not upgraded to PHPUnit 10/11 yet, so these will be testing their PHP 8.4 readiness with PHPUnit 9, which means that continuing to support PHPUnit 9 is important for the time being.

Refs:
* https://github.com/phpDocumentor/ReflectionDocBlock/releases/
  • Loading branch information
jrfnl committed Aug 20, 2024
1 parent 0160eb2 commit d9d1858
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/Prophecy/Doubler/ClassPatch/MagicCallPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,18 @@ public function apply(ClassNode $node)

// only magic methods can have a contract that needs to be enforced
if (in_array($methodName, self::MAGIC_METHODS_WITH_ARGUMENTS)) {
foreach($tag->getArguments() as $argument) {
$argumentNode = new ArgumentNode($argument['name']);
$methodNode->addArgument($argumentNode);
if (method_exists($tag, 'getParameters')) {
// Reflection Docblock 5.4.0+.
foreach($tag->getParameters() as $argument) {
$argumentNode = new ArgumentNode($argument->getName());
$methodNode->addArgument($argumentNode);
}
} else {
// Reflection Docblock < 5.4.0.
foreach($tag->getArguments() as $argument) {
$argumentNode = new ArgumentNode($argument['name']);
$methodNode->addArgument($argumentNode);
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion tests/Doubler/ClassPatch/MagicCallPatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ public function it_supports_arguments_for_magic_methods()

$classNode = $this->applyPatchTo($class);

$this->assertEquals([new ArgumentNode('data')], $classNode->getMethod('__unserialize')->getArguments());
$method = $classNode->getMethod('__unserialize');
if (method_exists($method, 'getParameters')) {
// Reflection Docblock 5.4.0+.
$args = $method->getParameters();
} else {
// Reflection Docblock < 5.4.0.
$args = $method->getArguments();
}

$this->assertEquals([new ArgumentNode('data')], $args);
}

private function applyPatchTo(\ReflectionClass $class): ClassNode
Expand Down

0 comments on commit d9d1858

Please sign in to comment.