diff --git a/ChangeLog.md b/ChangeLog.md index b00d488..93c0bf0 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -2,6 +2,12 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. +## [1.1.2] - 2019-MM-DD + +### Fixed + +* Fixed handling of `object` type + ## [1.1.1] - 2019-06-08 ### Fixed @@ -19,6 +25,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt * Initial release based on [code contributed by Michel Hartmann to PHPUnit](https://github.com/sebastianbergmann/phpunit/pull/3673) +[1.1.2]: https://github.com/sebastianbergmann/type/compare/1.1.1...1.1.2 [1.1.1]: https://github.com/sebastianbergmann/type/compare/1.1.0...1.1.1 [1.1.0]: https://github.com/sebastianbergmann/type/compare/1.0.0...1.1.0 [1.0.0]: https://github.com/sebastianbergmann/type/compare/ff74aa41746bd8d10e931843ebf37d42da513ede...1.0.0 diff --git a/src/GenericObjectType.php b/src/GenericObjectType.php new file mode 100644 index 0000000..7e0a6d2 --- /dev/null +++ b/src/GenericObjectType.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace SebastianBergmann\Type; + +final class GenericObjectType extends Type +{ + /** + * @var bool + */ + private $allowsNull; + + public function __construct(bool $nullable) + { + $this->allowsNull = $nullable; + } + + public function isAssignable(Type $other): bool + { + if ($this->allowsNull && $other instanceof NullType) { + return true; + } + + if (!$other instanceof ObjectType) { + return false; + } + + return true; + } + + public function getReturnTypeDeclaration(): string + { + return ': ' . ($this->allowsNull ? '?' : '') . 'object'; + } + + public function allowsNull(): bool + { + return $this->allowsNull; + } +} diff --git a/src/Type.php b/src/Type.php index 1f1c4e8..df7dce6 100644 --- a/src/Type.php +++ b/src/Type.php @@ -40,6 +40,9 @@ public static function fromName(string $typeName, bool $allowsNull): self case 'null': return new NullType; + case 'object': + return new GenericObjectType($allowsNull); + case 'unknown type': return new UnknownType; @@ -57,7 +60,6 @@ public static function fromName(string $typeName, bool $allowsNull): self case 'resource': case 'resource (closed)': case 'string': - case 'object': return new SimpleType($typeName, $allowsNull); default: diff --git a/tests/unit/TypeTest.php b/tests/unit/TypeTest.php index 1619617..5bc4b7d 100644 --- a/tests/unit/TypeTest.php +++ b/tests/unit/TypeTest.php @@ -15,6 +15,7 @@ * @covers \SebastianBergmann\Type\Type * * @uses \SebastianBergmann\Type\SimpleType + * @uses \SebastianBergmann\Type\GenericObjectType * @uses \SebastianBergmann\Type\ObjectType * @uses \SebastianBergmann\Type\TypeName * @uses \SebastianBergmann\Type\CallableType @@ -64,7 +65,7 @@ public function namesToTypes(): array 'int' => ['int', false, new SimpleType('int', false)], 'bool' => ['bool', false, new SimpleType('bool', false)], 'boolean' => ['boolean', false, new SimpleType('bool', false)], - 'object' => ['object', false, new SimpleType('object', false)], + 'object' => ['object', false, new GenericObjectType(false)], 'real' => ['real', false, new SimpleType('float', false)], 'double' => ['double', false, new SimpleType('float', false)], 'float' => ['float', false, new SimpleType('float', false)],