diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 52470f1fa..1c5cfb728 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -11,27 +11,12 @@ parameters: path: src/Prophecy/Call/CallCenter.php - - message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\\\|T of object, string given\\.$#" + message: "#^Method Prophecy\\\\Doubler\\\\CachedDoubler\\:\\:createDoubleClass\\(\\) should return class\\-string\\ but returns class\\-string\\.$#" count: 1 - path: src/Prophecy/Doubler/ClassPatch/MagicCallPatch.php + path: src/Prophecy/Doubler/CachedDoubler.php - - message: "#^Method Prophecy\\\\Doubler\\\\Doubler\\:\\:double\\(\\) should return Prophecy\\\\Doubler\\\\DoubleInterface but returns object\\.$#" - count: 3 - path: src/Prophecy/Doubler/Doubler.php - - - - message: "#^Parameter \\#1 \\$className of method Doctrine\\\\Instantiator\\\\Instantiator\\:\\:instantiate\\(\\) expects class\\-string\\, string given\\.$#" - count: 1 - path: src/Prophecy/Doubler/Doubler.php - - - - message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\\\|T of object, string given\\.$#" - count: 1 - path: src/Prophecy/Doubler/Doubler.php - - - - message: "#^Unable to resolve the template type T in call to method Doctrine\\\\Instantiator\\\\Instantiator\\:\\:instantiate\\(\\)$#" + message: "#^Method Prophecy\\\\Doubler\\\\Doubler\\:\\:createDoubleClass\\(\\) should return class\\-string\\ but returns class\\-string\\.$#" count: 1 path: src/Prophecy/Doubler/Doubler.php @@ -44,18 +29,3 @@ parameters: message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(ReflectionIntersectionType\\|ReflectionNamedType\\)\\: mixed\\)\\|null, Closure\\(ReflectionNamedType\\)\\: string given\\.$#" count: 1 path: src/Prophecy/Prophecy/MethodProphecy.php - - - - message: "#^Unable to resolve the template type T in call to method Prophecy\\\\Prophet\\:\\:prophesize\\(\\)$#" - count: 1 - path: src/Prophecy/Prophecy/MethodProphecy.php - - - - message: "#^Method Prophecy\\\\Prophecy\\\\ObjectProphecy\\:\\:reveal\\(\\) should return T of object but returns Prophecy\\\\Doubler\\\\DoubleInterface&Prophecy\\\\Prophecy\\\\ProphecySubjectInterface\\.$#" - count: 1 - path: src/Prophecy/Prophecy/ObjectProphecy.php - - - - message: "#^Method Prophecy\\\\Prophet\\:\\:prophesize\\(\\) should return Prophecy\\\\Prophecy\\\\ObjectProphecy\\ but returns Prophecy\\\\Prophecy\\\\ObjectProphecy\\\\.$#" - count: 3 - path: src/Prophecy/Prophet.php diff --git a/src/Prophecy/Doubler/CachedDoubler.php b/src/Prophecy/Doubler/CachedDoubler.php index 6e93ea79f..f6f2ce66a 100644 --- a/src/Prophecy/Doubler/CachedDoubler.php +++ b/src/Prophecy/Doubler/CachedDoubler.php @@ -22,7 +22,7 @@ class CachedDoubler extends Doubler { /** - * @var array + * @var array */ private static $classes = array(); @@ -37,8 +37,8 @@ protected function createDoubleClass(ReflectionClass $class = null, array $inter } /** - * @param ReflectionClass $class - * @param ReflectionClass[] $interfaces + * @param ReflectionClass|null $class + * @param ReflectionClass[] $interfaces * * @return string */ diff --git a/src/Prophecy/Doubler/Doubler.php b/src/Prophecy/Doubler/Doubler.php index 682e56356..88e153093 100644 --- a/src/Prophecy/Doubler/Doubler.php +++ b/src/Prophecy/Doubler/Doubler.php @@ -77,11 +77,13 @@ public function registerClassPatch(ClassPatchInterface $patch) /** * Creates double from specific class or/and list of interfaces. * - * @param ReflectionClass|null $class - * @param ReflectionClass[] $interfaces Array of ReflectionClass instances - * @param array|null $args Constructor arguments + * @template T of object * - * @return DoubleInterface + * @param ReflectionClass|null $class + * @param ReflectionClass[] $interfaces Array of ReflectionClass instances + * @param array|null $args Constructor arguments + * + * @return T&DoubleInterface * * @throws \Prophecy\Exception\InvalidArgumentException */ @@ -118,10 +120,12 @@ public function double(ReflectionClass $class = null, array $interfaces, array $ /** * Creates double class and returns its FQN. * - * @param ReflectionClass|null $class - * @param ReflectionClass[] $interfaces + * @template T of object + * + * @param ReflectionClass|null $class + * @param ReflectionClass[] $interfaces * - * @return string + * @return class-string */ protected function createDoubleClass(ReflectionClass $class = null, array $interfaces) { @@ -136,6 +140,7 @@ protected function createDoubleClass(ReflectionClass $class = null, array $inter $node->addInterface(DoubleInterface::class); $this->creator->create($name, $node); + \assert(class_exists($name, false)); return $name; } diff --git a/src/Prophecy/Doubler/Generator/Node/ClassNode.php b/src/Prophecy/Doubler/Generator/Node/ClassNode.php index c8effe6d3..b90b0cc98 100644 --- a/src/Prophecy/Doubler/Generator/Node/ClassNode.php +++ b/src/Prophecy/Doubler/Generator/Node/ClassNode.php @@ -22,11 +22,11 @@ class ClassNode { /** - * @var string + * @var class-string */ private $parentClass = 'stdClass'; /** - * @var list + * @var list */ private $interfaces = array(); @@ -53,7 +53,7 @@ class ClassNode private $methods = array(); /** - * @return string + * @return class-string */ public function getParentClass() { @@ -61,7 +61,7 @@ public function getParentClass() } /** - * @param string $class + * @param class-string|null $class * * @return void */ @@ -71,7 +71,7 @@ public function setParentClass($class) } /** - * @return list + * @return list */ public function getInterfaces() { @@ -79,7 +79,7 @@ public function getInterfaces() } /** - * @param string $interface + * @param class-string $interface * * @return void */ @@ -93,7 +93,7 @@ public function addInterface($interface) } /** - * @param string $interface + * @param class-string $interface * * @return bool */ diff --git a/src/Prophecy/Doubler/LazyDouble.php b/src/Prophecy/Doubler/LazyDouble.php index 058001864..96ca25bb7 100644 --- a/src/Prophecy/Doubler/LazyDouble.php +++ b/src/Prophecy/Doubler/LazyDouble.php @@ -20,13 +20,15 @@ * Lazy double. * Gives simple interface to describe double before creating it. * + * @template T of object + * * @author Konstantin Kudryashov */ class LazyDouble { private $doubler; /** - * @var ReflectionClass|null + * @var ReflectionClass|null */ private $class; /** @@ -38,7 +40,7 @@ class LazyDouble */ private $arguments = null; /** - * @var DoubleInterface|null + * @var (T&DoubleInterface)|null */ private $double; @@ -50,12 +52,16 @@ public function __construct(Doubler $doubler) /** * Tells doubler to use specific class as parent one for double. * - * @param string|ReflectionClass $class + * @param class-string|ReflectionClass $class * * @return void * - * @throws \Prophecy\Exception\Doubler\ClassNotFoundException - * @throws \Prophecy\Exception\Doubler\DoubleException + * @template U of object + * @phpstan-param class-string|ReflectionClass $class + * @phpstan-this-out static + * + * @throws ClassNotFoundException + * @throws DoubleException */ public function setParentClass($class) { @@ -71,18 +77,24 @@ public function setParentClass($class) $class = new ReflectionClass($class); } + /** @var static $this */ + $this->class = $class; } /** * Tells doubler to implement specific interface with double. * - * @param string|ReflectionClass $interface + * @param class-string|ReflectionClass $interface * * @return void * - * @throws \Prophecy\Exception\Doubler\InterfaceNotFoundException - * @throws \Prophecy\Exception\Doubler\DoubleException + * @template U of object + * @phpstan-param class-string|ReflectionClass $interface + * @phpstan-this-out static + * + * @throws InterfaceNotFoundException + * @throws DoubleException */ public function addInterface($interface) { @@ -121,7 +133,7 @@ public function setArguments(array $arguments = null) /** * Creates double instance or returns already created one. * - * @return DoubleInterface + * @return T&DoubleInterface */ public function getInstance() { diff --git a/src/Prophecy/Prophecy/ObjectProphecy.php b/src/Prophecy/Prophecy/ObjectProphecy.php index 6008e5d3c..d10332f71 100644 --- a/src/Prophecy/Prophecy/ObjectProphecy.php +++ b/src/Prophecy/Prophecy/ObjectProphecy.php @@ -31,6 +31,9 @@ */ class ObjectProphecy implements ProphecyInterface { + /** + * @var LazyDouble + */ private $lazyDouble; private $callCenter; private $revealer; @@ -41,6 +44,9 @@ class ObjectProphecy implements ProphecyInterface */ private $methodProphecies = array(); + /** + * @param LazyDouble $lazyDouble + */ public function __construct( LazyDouble $lazyDouble, CallCenter $callCenter = null, @@ -61,7 +67,7 @@ public function __construct( * * @return $this * - * @template U + * @template U of object * @phpstan-param class-string $class * @phpstan-this-out static */ @@ -79,7 +85,7 @@ public function willExtend($class) * * @return $this * - * @template U + * @template U of object * @phpstan-param class-string $interface * @phpstan-this-out static */ diff --git a/src/Prophecy/Prophet.php b/src/Prophecy/Prophet.php index 7752fcf02..16c781347 100644 --- a/src/Prophecy/Prophet.php +++ b/src/Prophecy/Prophet.php @@ -74,7 +74,7 @@ public function __construct( * * @template T of object * @phpstan-param class-string|null $classOrInterface - * @phpstan-return ObjectProphecy + * @phpstan-return ($classOrInterface is null ? ObjectProphecy : ObjectProphecy) */ public function prophesize($classOrInterface = null) {