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

Introduce PHP7.2 object type hint #112

Merged
merged 2 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Generator/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ final class TypeGenerator implements GeneratorInterface
*
* @link http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
*/
private static $internalPhpTypes = ['void', 'int', 'float', 'string', 'bool', 'array', 'callable', 'iterable'];
private static $internalPhpTypes = [
'void',
'int',
'float',
'string',
'bool',
'array',
'callable',
'iterable',
'object'
];

/**
* @var string a regex pattern to match valid class names or types
Expand Down
13 changes: 12 additions & 1 deletion test/Generator/MethodGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ZendTest\Code\TestAsset\EmptyClass;
use ZendTest\Code\TestAsset\InternalHintsClass;
use ZendTest\Code\TestAsset\IterableHintsClass;
use ZendTest\Code\TestAsset\ObjectHintsClass;
use ZendTest\Code\TestAsset\NullableReturnTypeHintedClass;
use ZendTest\Code\TestAsset\ReturnTypeHintedClass;

Expand Down Expand Up @@ -364,9 +365,19 @@ public function returnTypeHintClassesProvider()
[NullableReturnTypeHintedClass::class, 'otherClassReturn', '?\\' . InternalHintsClass::class],
[IterableHintsClass::class, 'iterableReturnValue', 'iterable'],
[IterableHintsClass::class, 'nullableIterableReturnValue', '?iterable'],
[ObjectHintsClass::class, 'objectReturnValue', 'object'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should only be added to the data providrr if the php version is 70200 or greater

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the tests regarding PHP version.

[ObjectHintsClass::class, 'nullableObjectReturnValue', '?object'],
];

return $parameters;
return array_filter(
$parameters,
function (array $parameter) {
return PHP_VERSION_ID >= 70200
|| (
false === strpos($parameter[2], 'object')
);
}
);
}

/**
Expand Down
21 changes: 16 additions & 5 deletions test/Generator/ParameterGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ZendTest\Code\TestAsset\EmptyClass;
use ZendTest\Code\TestAsset\InternalHintsClass;
use ZendTest\Code\TestAsset\IterableHintsClass;
use ZendTest\Code\TestAsset\ObjectHintsClass;
use ZendTest\Code\TestAsset\NullableHintsClass;
use ZendTest\Code\TestAsset\NullNullableDefaultHintsClass;
use ZendTest\Code\TestAsset\VariadicParametersClass;
Expand Down Expand Up @@ -345,9 +346,6 @@ public function validClassNameProvider()
['mixed'],
['Mixed'],
['MIXED'],
['object'],
['Object'],
['OBJECT'],
['resource'],
['Resource'],
['RESOURCE'],
Expand Down Expand Up @@ -464,17 +462,30 @@ public function reflectionHintsProvider()
[IterableHintsClass::class, 'iterableParameter', 'foo', 'iterable'],
[IterableHintsClass::class, 'nullableIterableParameter', 'foo', '?iterable'],
[IterableHintsClass::class, 'nullDefaultIterableParameter', 'foo', '?iterable'],
[ObjectHintsClass::class, 'objectParameter', 'foo', 'object'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should only be added to the data providrr if the php version is 70200 or greater

[ObjectHintsClass::class, 'nullableObjectParameter', 'foo', '?object'],
[ObjectHintsClass::class, 'nullDefaultObjectParameter', 'foo', '?object'],
];

$compatibleParameters = array_filter(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to use this variable in line 487 and 489.

$parameters,
function (array $parameter) {
return PHP_VERSION_ID >= 70200
|| (
false === strpos($parameter[3], 'object')
);
}
);

// just re-organizing the keys so that the phpunit data set makes sense in errors:
return array_combine(
array_map(
function (array $definition) {
return $definition[0] . '#' . $definition[1];
},
$parameters
$compatibleParameters
),
$parameters
$compatibleParameters
);
}

Expand Down
15 changes: 9 additions & 6 deletions test/Generator/TypeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public function validTypeProvider()
['iterable', 'iterable'],
['Iterable', 'iterable'],
['ITERABLE', 'iterable'],
['object', '\\object'],
['Object', '\\Object'],
['OBJECT', '\\OBJECT'],
['object', 'object'],
['Object', 'object'],
['OBJECT', 'object'],
['mixed', '\\mixed'],
['Mixed', '\\Mixed'],
['MIXED', '\\MIXED'],
Expand Down Expand Up @@ -157,9 +157,9 @@ public function validTypeProvider()
['?iterable', '?iterable'],
['?Iterable', '?iterable'],
['?ITERABLE', '?iterable'],
['?object', '?\\object'],
['?Object', '?\\Object'],
['?OBJECT', '?\\OBJECT'],
['?object', '?object'],
['?Object', '?object'],
['?OBJECT', '?object'],
['?mixed', '?\\mixed'],
['?Mixed', '?\\Mixed'],
['?MIXED', '?\\MIXED'],
Expand Down Expand Up @@ -241,6 +241,9 @@ public function invalidTypeProvider()
['\\iterable'],
['\\Iterable'],
['\\ITERABLE'],
['\\object'],
['\\Object'],
['\\OBJECT'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you added these to invalidTypeProvider, don't we need remove 'object' from validTypeProvider?

];

return array_combine(
Expand Down
26 changes: 26 additions & 0 deletions test/TestAsset/ObjectHintsClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ZendTest\Code\TestAsset;

class ObjectHintsClass extends EmptyClass
{
public function objectParameter(object $foo)
{
}

public function nullableObjectParameter(?object $foo)
{
}

public function nullDefaultObjectParameter(object $foo = null)
{
}

public function objectReturnValue() : object
{
}

public function nullableObjectReturnValue() : ?object
{
}
}