Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #30 from webimpress/hotfix/reflection-serializatio…
Browse files Browse the repository at this point in the history
…n-php-7.4

Fix (un)serialization for Reflection* objects - PHP 7.4
  • Loading branch information
weierophinney committed Oct 16, 2019
2 parents 7621172 + 70eb7e6 commit 28cbe9f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
41 changes: 34 additions & 7 deletions src/Reflection/AbstractFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ abstract class AbstractFunction
*/
protected $class;

/**
* Function name (needed for serialization)
* @var string
*/
protected $name;

/**
* Function/method description
* @var string
Expand All @@ -75,11 +81,11 @@ abstract class AbstractFunction
*/
protected $docComment = '';

private $return;
private $returnDesc;
private $paramDesc;
private $sigParams;
private $sigParamsDepth;
protected $return;
protected $returnDesc;
protected $paramDesc;
protected $sigParams;
protected $sigParamsDepth;

/**
* Constructor
Expand Down Expand Up @@ -109,6 +115,8 @@ public function __construct(ReflectionFunctionAbstract $r, $namespace = null, $a
$this->class = $r->getDeclaringClass()->getName();
}

$this->name = $r->getName();

// Perform some introspection
$this->reflect();
}
Expand Down Expand Up @@ -438,6 +446,25 @@ public function getInvokeArguments()
return $this->argv;
}

/**
* @return string[]
*/
public function __sleep()
{
$serializable = [];
foreach ($this as $name => $value) {
if ($value instanceof PhpReflectionFunction
|| $value instanceof PhpReflectionMethod
) {
continue;
}

$serializable[] = $name;
}

return $serializable;
}

/**
* Wakeup from serialization
*
Expand All @@ -450,9 +477,9 @@ public function __wakeup()
{
if ($this->reflection instanceof PhpReflectionMethod) {
$class = new PhpReflectionClass($this->class);
$this->reflection = new PhpReflectionMethod($class->newInstance(), $this->getName());
$this->reflection = new PhpReflectionMethod($class->newInstance(), $this->name);
} else {
$this->reflection = new PhpReflectionFunction($this->getName());
$this->reflection = new PhpReflectionFunction($this->name);
}
}
}
17 changes: 16 additions & 1 deletion src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class ReflectionClass
*/
protected $reflection;

/**
* Reflection class name (needed for serialization)
* @var string
*/
protected $name;

/**
* Constructor
*
Expand All @@ -55,6 +61,7 @@ class ReflectionClass
public function __construct(PhpReflectionClass $reflection, $namespace = null, $argv = false)
{
$this->reflection = $reflection;
$this->name = $reflection->getName();
$this->setNamespace($namespace);

foreach ($reflection->getMethods() as $method) {
Expand Down Expand Up @@ -171,6 +178,14 @@ public function setNamespace($namespace)
*/
public function __wakeup()
{
$this->reflection = new PhpReflectionClass($this->getName());
$this->reflection = new PhpReflectionClass($this->name);
}

/**
* @return string[]
*/
public function __sleep()
{
return ['config', 'methods', 'namespace', 'name'];
}
}
3 changes: 2 additions & 1 deletion src/Reflection/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function __construct(ReflectionClass $class, \ReflectionMethod $r, $names

// If method call, need to store some info on the class
$this->class = $class->getName();
$this->name = $r->getName();

// Perform some introspection
$this->reflect();
Expand Down Expand Up @@ -88,7 +89,7 @@ public function __wakeup()
$this->getNamespace(),
$this->getInvokeArguments()
);
$this->reflection = new \ReflectionMethod($this->classReflection->getName(), $this->getName());
$this->reflection = new \ReflectionMethod($this->classReflection->getName(), $this->name);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/Reflection/ReflectionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ class ReflectionParameter
*/
protected $description;

/**
* Parameter name (needed for serialization)
* @var string
*/
protected $name;

/**
* Declaring function name (needed for serialization)
* @var string
*/
protected $functionName;

/**
* Constructor
*
Expand All @@ -47,6 +59,13 @@ class ReflectionParameter
public function __construct(\ReflectionParameter $r, $type = 'mixed', $description = '')
{
$this->reflection = $r;

// Store parameters needed for (un)serialization
$this->name = $r->getName();
$this->functionName = $r->getDeclaringClass()
? [$r->getDeclaringClass()->getName(), $r->getDeclaringFunction()->getName()]
: $r->getDeclaringFunction()->getName();

$this->setType($type);
$this->setDescription($description);
}
Expand Down Expand Up @@ -140,4 +159,17 @@ public function getPosition()
{
return $this->position;
}

/**
* @return string[]
*/
public function __sleep()
{
return ['position', 'type', 'description', 'name', 'functionName'];
}

public function __wakeup()
{
$this->reflection = new \ReflectionParameter($this->functionName, $this->name);
}
}

0 comments on commit 28cbe9f

Please sign in to comment.