Skip to content

Commit

Permalink
Object type hint on board
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed May 28, 2020
1 parent 8a2609d commit bcd55b2
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 213 deletions.
15 changes: 2 additions & 13 deletions phpcs.xml.dist
Expand Up @@ -18,25 +18,14 @@
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
<properties>
<property name="enableObjectTypeHint" value="false"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint"/>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingParameterTypeHint">
<exclude-pattern>src/Reflection/Adapter/*</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint"/>
<properties>
<property name="enableObjectTypeHint" value="false"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
<properties>
<property name="enableObjectTypeHint" value="false"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint"/>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint">
<exclude-pattern>src/Reflection/Adapter/*</exclude-pattern>
</rule>
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Adapter/ReflectionClass.php
Expand Up @@ -8,13 +8,13 @@
use OutOfBoundsException;
use ReflectionClass as CoreReflectionClass;
use ReflectionException as CoreReflectionException;
use Roave\BetterReflection\Reflection\Exception\NotAnObject;
use Roave\BetterReflection\Reflection\ReflectionClass as BetterReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionClassConstant as BetterReflectionClassConstant;
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionObject as BetterReflectionObject;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
use Roave\BetterReflection\Util\FileHelper;
use TypeError;
use function array_combine;
use function array_map;
use function array_values;
Expand Down Expand Up @@ -382,7 +382,7 @@ public function isInstance($object)
{
try {
return $this->betterReflectionClass->isInstance($object);
} catch (NotAnObject $e) {
} catch (TypeError $e) {
return null;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Reflection/Adapter/ReflectionMethod.php
Expand Up @@ -9,10 +9,10 @@
use ReflectionMethod as CoreReflectionMethod;
use Roave\BetterReflection\Reflection\Adapter\Exception\NotImplemented;
use Roave\BetterReflection\Reflection\Exception\NoObjectProvided;
use Roave\BetterReflection\Reflection\Exception\NotAnObject;
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Util\FileHelper;
use Throwable;
use TypeError;
use function func_get_args;

class ReflectionMethod extends CoreReflectionMethod
Expand Down Expand Up @@ -318,7 +318,7 @@ public function getClosure($object = null)
{
try {
return $this->betterReflectionMethod->getClosure($object);
} catch (NoObjectProvided | NotAnObject $e) {
} catch (NoObjectProvided | TypeError $e) {
return null;
} catch (Throwable $e) {
throw new CoreReflectionException($e->getMessage(), 0, $e);
Expand All @@ -344,7 +344,7 @@ public function invoke($object = null, $args = null)

try {
return $this->betterReflectionMethod->invoke(...func_get_args());
} catch (NoObjectProvided | NotAnObject $e) {
} catch (NoObjectProvided | TypeError $e) {
return null;
} catch (Throwable $e) {
throw new CoreReflectionException($e->getMessage(), 0, $e);
Expand All @@ -362,7 +362,7 @@ public function invokeArgs($object = null, array $args = [])

try {
return $this->betterReflectionMethod->invokeArgs($object, $args);
} catch (NoObjectProvided | NotAnObject $e) {
} catch (NoObjectProvided | TypeError $e) {
return null;
} catch (Throwable $e) {
throw new CoreReflectionException($e->getMessage(), 0, $e);
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Adapter/ReflectionObject.php
Expand Up @@ -6,12 +6,12 @@

use ReflectionException as CoreReflectionException;
use ReflectionObject as CoreReflectionObject;
use Roave\BetterReflection\Reflection\Exception\NotAnObject;
use Roave\BetterReflection\Reflection\ReflectionClass as BetterReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionObject as BetterReflectionObject;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
use Roave\BetterReflection\Util\FileHelper;
use TypeError;
use function array_combine;
use function array_map;
use function array_values;
Expand Down Expand Up @@ -355,7 +355,7 @@ public function isInstance($object)
{
try {
return $this->betterReflectionObject->isInstance($object);
} catch (NotAnObject $e) {
} catch (TypeError $e) {
return null;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Reflection/Adapter/ReflectionProperty.php
Expand Up @@ -11,6 +11,7 @@
use Roave\BetterReflection\Reflection\Exception\NotAnObject;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
use Throwable;
use TypeError;

class ReflectionProperty extends CoreReflectionProperty
{
Expand Down Expand Up @@ -62,7 +63,7 @@ public function getValue($object = null)

try {
return $this->betterReflectionProperty->getValue($object);
} catch (NoObjectProvided | NotAnObject $e) {
} catch (NoObjectProvided | TypeError $e) {
return null;
} catch (Throwable $e) {
throw new CoreReflectionException($e->getMessage(), 0, $e);
Expand Down
25 changes: 2 additions & 23 deletions src/Reflection/ReflectionClass.php
Expand Up @@ -4,7 +4,6 @@

namespace Roave\BetterReflection\Reflection;

use InvalidArgumentException;
use OutOfBoundsException;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_ as ClassNode;
Expand Down Expand Up @@ -47,7 +46,6 @@
use function assert;
use function implode;
use function in_array;
use function is_object;
use function ltrim;
use function sha1;
use function sprintf;
Expand Down Expand Up @@ -125,20 +123,11 @@ public static function createFromName(string $className) : self
*
* @see ReflectionObject::createFromInstance
*
* @param object $instance
*
* @throws IdentifierNotFound
* @throws ReflectionException
* @throws InvalidArgumentException
*
* @psalm-suppress DocblockTypeContradiction
*/
public static function createFromInstance($instance) : self
public static function createFromInstance(object $instance) : self
{
if (! is_object($instance)) {
throw new InvalidArgumentException('Instance must be an instance of an object');
}

return ReflectionObject::createFromInstance($instance);
}

Expand Down Expand Up @@ -1167,19 +1156,9 @@ static function (self $interface) : string {
* Checks whether the given object is an instance.
*
* @link https://php.net/manual/en/reflectionclass.isinstance.php
*
* @param object $object
*
* @throws NotAnObject
*
* @psalm-suppress DocblockTypeContradiction
*/
public function isInstance($object) : bool
public function isInstance(object $object) : bool
{
if (! is_object($object)) {
throw NotAnObject::fromNonObject($object);
}

$className = $this->getName();

// note: since $object was loaded, we can safely assume that $className is available in the current
Expand Down
39 changes: 8 additions & 31 deletions src/Reflection/ReflectionMethod.php
Expand Up @@ -5,7 +5,6 @@
namespace Roave\BetterReflection\Reflection;

use Closure;
use InvalidArgumentException;
use OutOfBoundsException;
use PhpParser\Node\Stmt\ClassMethod as MethodNode;
use PhpParser\Node\Stmt\Namespace_;
Expand All @@ -14,14 +13,12 @@
use Roave\BetterReflection\Reflection\Exception\ClassDoesNotExist;
use Roave\BetterReflection\Reflection\Exception\InvalidAbstractFunctionNodeType;
use Roave\BetterReflection\Reflection\Exception\NoObjectProvided;
use Roave\BetterReflection\Reflection\Exception\NotAnObject;
use Roave\BetterReflection\Reflection\Exception\ObjectNotInstanceOfClass;
use Roave\BetterReflection\Reflection\StringCast\ReflectionMethodStringCast;
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
use Roave\BetterReflection\Reflector\Reflector;
use function class_exists;
use function get_class;
use function is_object;
use function sprintf;
use function strtolower;

Expand Down Expand Up @@ -79,14 +76,11 @@ public static function createFromName(string $className, string $methodName) : s
/**
* Create a reflection of a method by it's name using an instance
*
* @param object $instance
*
* @throws InvalidArgumentException
* @throws ReflectionException
* @throws IdentifierNotFound
* @throws OutOfBoundsException
*/
public static function createFromInstance($instance, string $methodName) : self
public static function createFromInstance(object $instance, string $methodName) : self
{
return ReflectionClass::createFromInstance($instance)->getMethod($methodName);
}
Expand Down Expand Up @@ -277,14 +271,11 @@ public function isInternal() : bool
}

/**
* @param object|null $object
*
* @throws ClassDoesNotExist
* @throws NoObjectProvided
* @throws NotAnObject
* @throws ObjectNotInstanceOfClass
*/
public function getClosure($object = null) : Closure
public function getClosure(?object $object = null) : Closure
{
$declaringClassName = $this->getDeclaringClass()->getName();

Expand All @@ -304,33 +295,29 @@ public function getClosure($object = null) : Closure
}

/**
* @param object|null $object
* @param mixed ...$args
* @param mixed ...$args
*
* @return mixed
*
* @throws ClassDoesNotExist
* @throws NoObjectProvided
* @throws NotAnObject
* @throws ObjectNotInstanceOfClass
*/
public function invoke($object = null, ...$args)
public function invoke(?object $object = null, ...$args)
{
return $this->invokeArgs($object, $args);
}

/**
* @param object|null $object
* @param mixed[] $args
* @param mixed[] $args
*
* @return mixed
*
* @throws ClassDoesNotExist
* @throws NoObjectProvided
* @throws NotAnObject
* @throws ObjectNotInstanceOfClass
*/
public function invokeArgs($object = null, array $args = [])
public function invokeArgs(?object $object = null, array $args = [])
{
$declaringClassName = $this->getDeclaringClass()->getName();

Expand Down Expand Up @@ -358,12 +345,11 @@ private function callStaticMethod(array $args)
}

/**
* @param object $object
* @param mixed[] $args
*
* @return mixed
*/
private function callObjectMethod($object, array $args)
private function callObjectMethod(object $object, array $args)
{
return Closure::bind(function ($object, string $methodName, array $methodArgs) {
return $object->{$methodName}(...$methodArgs);
Expand All @@ -381,24 +367,15 @@ private function assertClassExist(string $className) : void
}

/**
* @param mixed $object
*
* @return object
*
* @throws NoObjectProvided
* @throws NotAnObject
* @throws ObjectNotInstanceOfClass
*/
private function assertObject($object)
private function assertObject(?object $object) : object
{
if ($object === null) {
throw NoObjectProvided::create();
}

if (! is_object($object)) {
throw NotAnObject::fromNonObject($object);
}

$declaringClassName = $this->getDeclaringClass()->getName();

if (get_class($object) !== $declaringClassName) {
Expand Down
21 changes: 3 additions & 18 deletions src/Reflection/ReflectionObject.php
Expand Up @@ -20,7 +20,6 @@
use Roave\BetterReflection\SourceLocator\Type\AnonymousClassObjectSourceLocator;
use function array_merge;
use function get_class;
use function is_object;
use function strpos;

class ReflectionObject extends ReflectionClass
Expand All @@ -34,10 +33,7 @@ class ReflectionObject extends ReflectionClass
/** @var Reflector */
private $reflector;

/**
* @param object $object
*/
private function __construct(Reflector $reflector, ReflectionClass $reflectionClass, $object)
private function __construct(Reflector $reflector, ReflectionClass $reflectionClass, object $object)
{
$this->reflector = $reflector;
$this->reflectionClass = $reflectionClass;
Expand All @@ -47,20 +43,11 @@ private function __construct(Reflector $reflector, ReflectionClass $reflectionCl
/**
* Pass an instance of an object to this method to reflect it
*
* @param object $object
*
* @throws ReflectionException
* @throws InvalidArgumentException
* @throws IdentifierNotFound
*
* @psalm-suppress DocblockTypeContradiction
*/
public static function createFromInstance($object) : ReflectionClass
public static function createFromInstance(object $object) : ReflectionClass
{
if (! is_object($object)) {
throw new InvalidArgumentException('Can only create from an instance of an object');
}

$className = get_class($object);

if (strpos($className, ReflectionClass::ANONYMOUS_CLASS_NAME_PREFIX) === 0) {
Expand Down Expand Up @@ -132,10 +119,8 @@ private function getRuntimeProperties(?int $filter = null) : array
*
* Note that we don't copy across DocBlock, protected, private or static
* because runtime properties can't have these attributes.
*
* @param object $instance
*/
private function createPropertyNodeFromReflection(CoreReflectionProperty $property, $instance) : PropertyNode
private function createPropertyNodeFromReflection(CoreReflectionProperty $property, object $instance) : PropertyNode
{
$builder = new PropertyNodeBuilder($property->getName());
$builder->setDefault($property->getValue($instance));
Expand Down
4 changes: 1 addition & 3 deletions src/Reflection/ReflectionParameter.php
Expand Up @@ -84,12 +84,10 @@ public static function createFromClassNameAndMethod(
/**
* Create a reflection of a parameter using an instance
*
* @param object $instance
*
* @throws OutOfBoundsException
*/
public static function createFromClassInstanceAndMethod(
$instance,
object $instance,
string $methodName,
string $parameterName
) : self {
Expand Down

0 comments on commit bcd55b2

Please sign in to comment.