4 Commits

Author SHA1 Message Date
Grégoire Paris 1b33070b49 Merge pull request #33 from DieterHolvoet/issue-32
Fix Trying to access array offset on value of type null
2020-04-26 13:02:52 +02:00
Dieter Holvoet 41a2f0f445 Add type check to prevent PHP 7.4 warnings
PHP 7.4 introduces a "Trying to access array offset on value of type ..." warning for accessing null/bool/int/float/resource (everything but array, string and object) as if it were an array. This commit fixes an instance where this warning is emitted.

Fixes #32.
2020-04-26 12:57:05 +02:00
Grégoire Paris d11bac73db Merge pull request #40 from pgrimaud/master
Fix typo in lib/Doctrine/Common/Reflection/TypedNoDefaultReflectionProperty.php
2020-04-18 23:41:10 +02:00
Pierre Grimaud 1c7272c98a Fix typos 2020-04-18 23:34:26 +02:00
8 changed files with 45 additions and 65 deletions
+1 -13
View File
@@ -5,26 +5,14 @@
"docsSlug": "doctrine-reflection",
"versions": [
{
"name": "1.2",
"name": "1.0",
"branchName": "master",
"slug": "latest",
"upcoming": true
},
{
"name": "1.1",
"branchName": "1.1.x",
"slug": "1.1",
"current": true,
"aliases": [
"current",
"stable"
]
},
{
"name": "1.0",
"branchName": "1.0",
"slug": "1.0",
"maintained": false
}
]
}
-1
View File
@@ -1,4 +1,3 @@
/docs export-ignore
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
+1 -1
View File
@@ -44,7 +44,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.2.x-dev"
"dev-master": "1.0.x-dev"
}
}
}
@@ -19,6 +19,7 @@ use const T_VAR;
use const T_VARIABLE;
use function array_merge;
use function file_get_contents;
use function is_array;
use function ltrim;
use function preg_match;
use function sprintf;
@@ -189,6 +190,9 @@ class StaticReflectionParser implements ReflectionProviderInterface
while (($token = $tokenParser->next()) && $token[0] !== T_STRING) {
continue;
}
if ($token === null) {
break;
}
$methodName = $token[1];
$this->docComment['method'][$methodName] = $docComment;
$docComment = '';
@@ -222,7 +226,7 @@ class StaticReflectionParser implements ReflectionProviderInterface
break;
}
$last_token = $token[0];
$last_token = is_array($token) ? $token[0] : false;
}
}
@@ -13,36 +13,11 @@ class TypedNoDefaultReflectionProperty extends ReflectionProperty
* {@inheritDoc}
*
* Checks that a typed property is initialized before accessing its value.
* This is neccessary to avoid PHP error "Error: Typed property must not be accessed before initialization".
* This is necessary to avoid PHP error "Error: Typed property must not be accessed before initialization".
* Should be used only for reflecting typed properties without a default value.
*/
public function getValue($object = null)
{
return $object !== null && $this->isInitialized($object) ? parent::getValue($object) : null;
}
/**
* {@inheritDoc}
*
* Works around the problem with setting typed no default properties to
* NULL which is not supported, instead unset() to uninitialize.
*
* @link https://github.com/doctrine/orm/issues/7999
*/
public function setValue($object, $value = null)
{
if ($value === null) {
$propertyName = $this->getName();
$unsetter = function () use ($propertyName) {
unset($this->$propertyName);
};
$unsetter = $unsetter->bindTo($object, $this->getDeclaringClass()->getName());
$unsetter();
return;
}
parent::setValue($object, $value);
}
}
@@ -0,0 +1,13 @@
<?php
namespace Doctrine\Tests_PHP74\Common\Reflection\Dummies;
trait TokenParserAnonymousFunctionTestClass
{
protected function method()
{
return static function ($value) {
return $value;
};
}
}
@@ -0,0 +1,24 @@
<?php
namespace Doctrine\Tests_PHP74\Common\Reflection;
use Doctrine\Common\Reflection\Psr0FindFile;
use Doctrine\Common\Reflection\StaticReflectionParser;
use Doctrine\Tests_PHP74\Common\Reflection\Dummies\TokenParserAnonymousFunctionTestClass;
use PHPUnit\Framework\TestCase;
use function strlen;
use function substr;
class TokenParserAnonymousFunctionTest extends TestCase
{
public function testGetValue() : void
{
$testsRoot = substr(__DIR__, 0, -strlen(__NAMESPACE__) - 1);
$paths = [
'Doctrine\\Tests_PHP74' => [$testsRoot],
];
$staticReflectionParser = new StaticReflectionParser(TokenParserAnonymousFunctionTestClass::class, new Psr0FindFile($paths));
self::assertEquals('', $staticReflectionParser->getDocComment());
}
}
@@ -23,32 +23,9 @@ class TypedNoDefaultReflectionPropertyTest extends TestCase
self::assertNull($reflProperty->getValue($object));
}
public function testSetValueNull() : void
{
$reflection = new TypedNoDefaultReflectionProperty(TypedFoo::class, 'id');
$reflection->setAccessible(true);
$object = new TypedFoo();
$object->setId(1);
$reflection->setValue($object, null);
self::assertNull($reflection->getValue($object));
}
}
class TypedNoDefaultReflectionPropertyTestClass
{
public string $test;
}
class TypedFoo
{
private int $id;
public function setId($id)
{
$this->id = $id;
}
}