Skip to content

Commit

Permalink
Fix phpstan level 8 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Dec 1, 2022
1 parent 6556b94 commit aa9e1de
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 21 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
parameters:
ignoreErrors:
-
message: "#^Method Prophecy\\\\Call\\\\CallCenter\\:\\:indentArguments\\(\\) should return array\\<string\\> but returns array\\<string\\>\\|null\\.$#"
count: 1
path: src/Prophecy/Call/CallCenter.php

-
message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|T of object, string given\\.$#"
count: 1
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 7
level: 8
inferPrivatePropertyTypeFromConstructor: true
treatPhpDocTypesAsCertain: false
paths:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function apply(ClassNode $node)
}

$constructor = $node->getMethod('__construct');
\assert($constructor !== null);
foreach ($constructor->getArguments() as $argument) {
$argument->setDefault(null);
}
Expand Down
1 change: 1 addition & 0 deletions src/Prophecy/Doubler/ClassPatch/ProphecySubjectPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function apply(ClassNode $node)

if ($node->hasMethod('__call')) {
$__call = $node->getMethod('__call');
\assert($__call !== null);
} else {
$__call = new MethodNode('__call');
$__call->addArgument(new ArgumentNode('name'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function supports(ClassNode $node)
*/
public function apply(ClassNode $node)
{
foreach ($node->getMethod('newInstance')->getArguments() as $argument) {
$method = $node->getMethod('newInstance');
\assert($method !== null);
foreach ($method->getArguments() as $argument) {
$argument->setDefault(null);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Prophecy/Doubler/ClassPatch/SplFileInfoPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function apply(ClassNode $node)
{
if ($node->hasMethod('__construct')) {
$constructor = $node->getMethod('__construct');
\assert($constructor !== null);
} else {
$constructor = new MethodNode('__construct');
$node->addMethod($constructor);
Expand Down
17 changes: 12 additions & 5 deletions src/Prophecy/Doubler/Generator/ClassMirror.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,29 +159,36 @@ private function reflectMethodToNode(ReflectionMethod $method, Node\ClassNode $c
}

if ($method->hasReturnType()) {
\assert($method->getReturnType() !== null);
$returnTypes = $this->getTypeHints($method->getReturnType(), $method->getDeclaringClass(), $method->getReturnType()->allowsNull());
$node->setReturnTypeNode(new ReturnTypeNode(...$returnTypes));
}
elseif (method_exists($method, 'hasTentativeReturnType') && $method->hasTentativeReturnType()) {
\assert($method->getTentativeReturnType() !== null);
$returnTypes = $this->getTypeHints($method->getTentativeReturnType(), $method->getDeclaringClass(), $method->getTentativeReturnType()->allowsNull());
$node->setReturnTypeNode(new ReturnTypeNode(...$returnTypes));
}

if (is_array($params = $method->getParameters()) && count($params)) {
foreach ($params as $param) {
$this->reflectArgumentToNode($param, $node);
$this->reflectArgumentToNode($param, $method->getDeclaringClass(), $node);
}
}

$classNode->addMethod($node);
}

private function reflectArgumentToNode(ReflectionParameter $parameter, Node\MethodNode $methodNode): void
/**
* @param ReflectionClass<object> $declaringClass
*
* @return void
*/
private function reflectArgumentToNode(ReflectionParameter $parameter, ReflectionClass $declaringClass, Node\MethodNode $methodNode): void
{
$name = $parameter->getName() == '...' ? '__dot_dot_dot__' : $parameter->getName();
$node = new Node\ArgumentNode($name);

$typeHints = $this->getTypeHints($parameter->getType(), $parameter->getDeclaringClass(), $parameter->allowsNull());
$typeHints = $this->getTypeHints($parameter->getType(), $declaringClass, $parameter->allowsNull());

$node->setTypeNode(new ArgumentTypeNode(...$typeHints));

Expand Down Expand Up @@ -227,11 +234,11 @@ private function getDefaultValue(ReflectionParameter $parameter)
}

/**
* @param ReflectionClass<object>|null $class
* @param ReflectionClass<object> $class
*
* @return list<string>
*/
private function getTypeHints(?ReflectionType $type, ?ReflectionClass $class, bool $allowsNull) : array
private function getTypeHints(?ReflectionType $type, ReflectionClass $class, bool $allowsNull) : array
{
$types = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Prophecy/Prediction/CallbackPrediction.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function check(array $calls, ObjectProphecy $object, MethodProphecy $meth
$callback = $this->callback;

if ($callback instanceof Closure && method_exists('Closure', 'bind') && (new ReflectionFunction($callback))->getClosureThis() !== null) {
$callback = Closure::bind($callback, $object);
$callback = Closure::bind($callback, $object) ?? $this->callback;
}

call_user_func($callback, $calls, $object, $method);
Expand Down
2 changes: 1 addition & 1 deletion src/Prophecy/Promise/CallbackPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function execute(array $args, ObjectProphecy $object, MethodProphecy $met
$callback = $this->callback;

if ($callback instanceof Closure && method_exists('Closure', 'bind') && (new ReflectionFunction($callback))->getClosureThis() !== null) {
$callback = Closure::bind($callback, $object);
$callback = Closure::bind($callback, $object) ?? $this->callback;
}

return call_user_func($callback, $args, $object, $method);
Expand Down
2 changes: 1 addition & 1 deletion src/Prophecy/Promise/ThrowPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function execute(array $args, ObjectProphecy $object, MethodProphecy $met
$reflection = new ReflectionClass($classname);
$constructor = $reflection->getConstructor();

if ($constructor->isPublic() && 0 == $constructor->getNumberOfRequiredParameters()) {
if ($constructor === null || $constructor->isPublic() && 0 == $constructor->getNumberOfRequiredParameters()) {
throw $reflection->newInstance();
}

Expand Down
10 changes: 3 additions & 7 deletions src/Prophecy/Prophet.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
*/
class Prophet
{
/**
* @var Doubler
*/
private $doubler;
private $revealer;
private $util;
Expand All @@ -39,13 +42,6 @@ class Prophet
*/
private $prophecies = array();

/**
* Initializes Prophet.
*
* @param null|Doubler $doubler
* @param null|RevealerInterface $revealer
* @param null|StringUtil $util
*/
public function __construct(
Doubler $doubler = null,
RevealerInterface $revealer = null,
Expand Down
8 changes: 4 additions & 4 deletions tests/Doubler/Generator/ClassMirrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ function it_changes_argument_names_if_they_are_varying()
$method->isStatic()->willReturn(false);
$method->returnsReference()->willReturn(false);
$method->hasReturnType()->willReturn(false);
$method->getDeclaringClass()->willReturn($class);

if (\PHP_VERSION_ID >= 80100) {
$method->hasTentativeReturnType()->willReturn(false);
Expand All @@ -502,7 +503,6 @@ function it_changes_argument_names_if_they_are_varying()
$parameter->getType()->willReturn(null);
$parameter->hasType()->willReturn(false);
$parameter->isVariadic()->willReturn(false);
$parameter->getDeclaringClass()->willReturn($class);

$mirror = new ClassMirror();

Expand Down Expand Up @@ -744,7 +744,7 @@ public function it_can_not_double_dnf_intersection_argument_types()

$classNode = (new ClassMirror())->reflect(new \ReflectionClass('Fixtures\Prophecy\DnfArgumentType'), []);
}

/**
* @test
*/
Expand Down Expand Up @@ -773,7 +773,7 @@ public function it_can_double_a_standalone_return_type_of_true()

$this->assertEquals(new ReturnTypeNode('true'), $methodNode->getReturnTypeNode());
}

/**
* @test
*/
Expand Down Expand Up @@ -864,7 +864,7 @@ public function it_can_double_a_standalone_parameter_type_of_null()

$this->assertEquals(new ArgumentTypeNode('null'), $arguments[0]->getTypeNode());
}

/**
* @test
*/
Expand Down

0 comments on commit aa9e1de

Please sign in to comment.