Skip to content

Commit

Permalink
Merge pull request #76 from glensc/reflection
Browse files Browse the repository at this point in the history
Fixed reflection deprecations for php 8.0
  • Loading branch information
falkenhawk authored Mar 15, 2021
2 parents 79f89fc + 6f3a244 commit 23154d8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
6 changes: 4 additions & 2 deletions packages/zend-amf/library/Zend/Amf/Adobe/Introspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ protected function _addService(Zend_Server_Reflection_Class $refclass, DOMElemen
$arg->setAttribute('name', $param->getName());

$type = $param->getType();
if ($type == 'mixed' && ($pclass = $param->getClass())) {
$type = $pclass->getName();
if (PHP_VERSION_ID < 80000) {
if ($type == 'mixed' && ($pclass = $param->getClass())) {
$type = $pclass->getName();
}
}

$ptype = $this->_registerType($type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public static function fromReflection(Zend_Reflection_Parameter $reflectionParam
$param = new Zend_CodeGenerator_Php_Parameter();
$param->setName($reflectionParameter->getName());

if($reflectionParameter->isArray()) {
if (PHP_VERSION_ID >= 80000) {
$param->setType($reflectionParameter->getType());
} elseif ($reflectionParameter->isArray()) {
$param->setType('array');
} else {
$typeClass = $reflectionParameter->getClass();
Expand Down
36 changes: 27 additions & 9 deletions packages/zend-reflection/library/Zend/Reflection/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,22 @@ public function getDeclaringClass($reflectionClass = 'Zend_Reflection_Class')
*/
public function getClass($reflectionClass = 'Zend_Reflection_Class')
{
$phpReflection = parent::getClass();
if($phpReflection == null) {
return null;
if (PHP_VERSION_ID < 80000) {
$phpReflection = parent::getClass();
if ($phpReflection == null) {
return null;
}

$phpReflectionClassName = $phpReflection->getName();
} else {
if (!parent::hasType()) {
return null;
}

$phpReflectionClassName = parent::getType();
}

$zendReflection = new $reflectionClass($phpReflection->getName());
$zendReflection = new $reflectionClass($phpReflectionClassName);
if (!$zendReflection instanceof Zend_Reflection_Class) {
// require_once 'Zend/Reflection/Exception.php';
throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Class');
Expand Down Expand Up @@ -109,13 +119,21 @@ public function getDeclaringFunction($reflectionClass = null)
*/
public function getType()
{
if ($docblock = $this->getDeclaringFunction()->getDocblock()) {
$params = $docblock->getTags('param');
try {
if ($docblock = $this->getDeclaringFunction()->getDocblock()) {
$params = $docblock->getTags('param');

if (isset($params[$this->getPosition()])) {
return $params[$this->getPosition()]->getType();
}
if (isset($params[$this->getPosition()])) {
return $params[$this->getPosition()]->getType();
}

}
} catch (Zend_Reflection_Exception $e) {
if (PHP_VERSION_ID >= 80000) {
return parent::getType();
} else {
throw $e;
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,13 @@ protected function _reflect()
// Try and auto-determine type, based on reflection
$paramTypesTmp = array();
foreach ($parameters as $i => $param) {
$paramType = 'mixed';
if ($param->isArray()) {
$paramType = 'array';
if (PHP_VERSION_ID < 80000) {
$paramType = 'mixed';
if ($param->isArray()) {
$paramType = 'array';
}
} else {
$paramType = $param->hasType() ? $param->getType() : 'mixed';
}
$paramTypesTmp[$i] = $paramType;
}
Expand Down
5 changes: 0 additions & 5 deletions tests/Zend/CodeGenerator/Php/ClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ public function testNameAccessors()

}

public function testClassDocblockAccessors()
{
$this->markTestSkipped();
}

public function testAbstractAccessors()
{
$codeGenClass = new Zend_CodeGenerator_Php_Class();
Expand Down
12 changes: 8 additions & 4 deletions tests/Zend/CodeGenerator/Php/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,15 @@ public function testFromReflection_GetWithNativeType()
$reflParam = $this->getFirstReflectionParameter('hasNativeDocTypes');
$codeGenParam = Zend_CodeGenerator_Php_Parameter::fromReflection($reflParam);

$this->assertNotEquals('int', $codeGenParam->getType());
$this->assertEquals('', $codeGenParam->getType());
if (PHP_VERSION_ID < 80000) {
$this->assertEquals('', $codeGenParam->getType());
$this->assertNotEquals('int', $codeGenParam->getType());
} else {
$this->assertEquals('int', $codeGenParam->getType());
}
}

static public function dataFromReflection_Generate()
public static function dataFromReflection_Generate()
{
return array(
array('name', '$param'),
Expand All @@ -147,7 +151,7 @@ static public function dataFromReflection_Generate()
array('defaultValue', '$value = \'foo\''),
array('defaultNull', '$value = null'),
array('fromArray', 'array $array'),
array('hasNativeDocTypes', '$integer'),
array('hasNativeDocTypes', PHP_VERSION_ID >= 80000 ? 'int $integer' : '$integer'),
array('defaultArray', '$array = array ()'),
array('defaultArrayWithValues', '$array = array ( 0 => 1, 1 => 2, 2 => 3,)'),
array('defaultFalse', '$val = false'),
Expand Down

0 comments on commit 23154d8

Please sign in to comment.