Skip to content

Commit

Permalink
Improve types for places dealing with class or interface names
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Jun 20, 2023
1 parent 8168610 commit a65b93a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 62 deletions.
36 changes: 3 additions & 33 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,11 @@ parameters:
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\\.$#"
message: "#^Method Prophecy\\\\Doubler\\\\CachedDoubler\\:\\:createDoubleClass\\(\\) should return class\\-string\\<Prophecy\\\\Doubler\\\\DoubleInterface&T of object\\> 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\\<object\\>, string given\\.$#"
count: 1
path: src/Prophecy/Doubler/Doubler.php

-
message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|T of object, string given\\.$#"
message: "#^Method Prophecy\\\\Doubler\\\\Doubler\\:\\:createDoubleClass\\(\\) should return class\\-string\\<Prophecy\\\\Doubler\\\\DoubleInterface&T of object\\> but returns class\\-string\\.$#"
count: 1
path: src/Prophecy/Doubler/Doubler.php

-
message: "#^Unable to resolve the template type T in call to method Doctrine\\\\Instantiator\\\\Instantiator\\:\\:instantiate\\(\\)$#"
count: 1
path: src/Prophecy/Doubler/Doubler.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\\<T of object\\> but returns Prophecy\\\\Prophecy\\\\ObjectProphecy\\<object\\>\\.$#"
count: 3
path: src/Prophecy/Prophet.php
6 changes: 3 additions & 3 deletions src/Prophecy/Doubler/CachedDoubler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class CachedDoubler extends Doubler
{
/**
* @var array<string, string>
* @var array<string, class-string>
*/
private static $classes = array();

Expand All @@ -37,8 +37,8 @@ protected function createDoubleClass(ReflectionClass $class = null, array $inter
}

/**
* @param ReflectionClass<object> $class
* @param ReflectionClass<object>[] $interfaces
* @param ReflectionClass<object>|null $class
* @param ReflectionClass<object>[] $interfaces
*
* @return string
*/
Expand Down
19 changes: 12 additions & 7 deletions src/Prophecy/Doubler/Doubler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ public function registerClassPatch(ClassPatchInterface $patch)
/**
* Creates double from specific class or/and list of interfaces.
*
* @param ReflectionClass<object>|null $class
* @param ReflectionClass<object>[] $interfaces Array of ReflectionClass instances
* @param array<mixed>|null $args Constructor arguments
* @template T of object
*
* @return DoubleInterface
* @param ReflectionClass<T>|null $class
* @param ReflectionClass<object>[] $interfaces Array of ReflectionClass instances
* @param array<mixed>|null $args Constructor arguments
*
* @return T&DoubleInterface
*
* @throws \Prophecy\Exception\InvalidArgumentException
*/
Expand Down Expand Up @@ -118,10 +120,12 @@ public function double(ReflectionClass $class = null, array $interfaces, array $
/**
* Creates double class and returns its FQN.
*
* @param ReflectionClass<object>|null $class
* @param ReflectionClass<object>[] $interfaces
* @template T of object
*
* @param ReflectionClass<T>|null $class
* @param ReflectionClass<object>[] $interfaces
*
* @return string
* @return class-string<T&DoubleInterface>
*/
protected function createDoubleClass(ReflectionClass $class = null, array $interfaces)
{
Expand All @@ -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;
}
Expand Down
14 changes: 7 additions & 7 deletions src/Prophecy/Doubler/Generator/Node/ClassNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
class ClassNode
{
/**
* @var string
* @var class-string
*/
private $parentClass = 'stdClass';
/**
* @var list<string>
* @var list<class-string>
*/
private $interfaces = array();

Expand All @@ -53,15 +53,15 @@ class ClassNode
private $methods = array();

/**
* @return string
* @return class-string
*/
public function getParentClass()
{
return $this->parentClass;
}

/**
* @param string $class
* @param class-string|null $class
*
* @return void
*/
Expand All @@ -71,15 +71,15 @@ public function setParentClass($class)
}

/**
* @return list<string>
* @return list<class-string>
*/
public function getInterfaces()
{
return $this->interfaces;
}

/**
* @param string $interface
* @param class-string $interface
*
* @return void
*/
Expand All @@ -93,7 +93,7 @@ public function addInterface($interface)
}

/**
* @param string $interface
* @param class-string $interface
*
* @return bool
*/
Expand Down
30 changes: 21 additions & 9 deletions src/Prophecy/Doubler/LazyDouble.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
* Lazy double.
* Gives simple interface to describe double before creating it.
*
* @template T of object
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class LazyDouble
{
private $doubler;
/**
* @var ReflectionClass<object>|null
* @var ReflectionClass<T>|null
*/
private $class;
/**
Expand All @@ -38,7 +40,7 @@ class LazyDouble
*/
private $arguments = null;
/**
* @var DoubleInterface|null
* @var (T&DoubleInterface)|null
*/
private $double;

Expand All @@ -50,12 +52,16 @@ public function __construct(Doubler $doubler)
/**
* Tells doubler to use specific class as parent one for double.
*
* @param string|ReflectionClass<object> $class
* @param class-string|ReflectionClass<object> $class
*
* @return void
*
* @throws \Prophecy\Exception\Doubler\ClassNotFoundException
* @throws \Prophecy\Exception\Doubler\DoubleException
* @template U of object
* @phpstan-param class-string<U>|ReflectionClass<U> $class
* @phpstan-this-out static<U>
*
* @throws ClassNotFoundException
* @throws DoubleException
*/
public function setParentClass($class)
{
Expand All @@ -71,18 +77,24 @@ public function setParentClass($class)
$class = new ReflectionClass($class);
}

/** @var static<U> $this */

$this->class = $class;
}

/**
* Tells doubler to implement specific interface with double.
*
* @param string|ReflectionClass<object> $interface
* @param class-string|ReflectionClass<object> $interface
*
* @return void
*
* @throws \Prophecy\Exception\Doubler\InterfaceNotFoundException
* @throws \Prophecy\Exception\Doubler\DoubleException
* @template U of object
* @phpstan-param class-string<U>|ReflectionClass<U> $interface
* @phpstan-this-out static<T&U>
*
* @throws InterfaceNotFoundException
* @throws DoubleException
*/
public function addInterface($interface)
{
Expand Down Expand Up @@ -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()
{
Expand Down
10 changes: 8 additions & 2 deletions src/Prophecy/Prophecy/ObjectProphecy.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
*/
class ObjectProphecy implements ProphecyInterface
{
/**
* @var LazyDouble<T>
*/
private $lazyDouble;
private $callCenter;
private $revealer;
Expand All @@ -41,6 +44,9 @@ class ObjectProphecy implements ProphecyInterface
*/
private $methodProphecies = array();

/**
* @param LazyDouble<T> $lazyDouble
*/
public function __construct(
LazyDouble $lazyDouble,
CallCenter $callCenter = null,
Expand All @@ -61,7 +67,7 @@ public function __construct(
*
* @return $this
*
* @template U
* @template U of object
* @phpstan-param class-string<U> $class
* @phpstan-this-out static<T&U>
*/
Expand All @@ -79,7 +85,7 @@ public function willExtend($class)
*
* @return $this
*
* @template U
* @template U of object
* @phpstan-param class-string<U> $interface
* @phpstan-this-out static<T&U>
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Prophecy/Prophet.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct(
*
* @template T of object
* @phpstan-param class-string<T>|null $classOrInterface
* @phpstan-return ObjectProphecy<T>
* @phpstan-return ($classOrInterface is null ? ObjectProphecy<object> : ObjectProphecy<T>)
*/
public function prophesize($classOrInterface = null)
{
Expand Down

0 comments on commit a65b93a

Please sign in to comment.