Skip to content

Commit

Permalink
Fix handling of object type
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jun 19, 2019
1 parent a46f5b0 commit 26a5e76
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
46 changes: 46 additions & 0 deletions src/GenericObjectType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/type.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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;
}
}
4 changes: 3 additions & 1 deletion src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)],
Expand Down

0 comments on commit 26a5e76

Please sign in to comment.