Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial import of code from @MichelHartmann that was contributed to P…
…HPUnit in sebastianbergmann/phpunit#3673
- Loading branch information
1 parent
3553621
commit 9c02491
Showing
17 changed files
with
924 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?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; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class NullType extends Type | ||
{ | ||
public function isAssignable(Type $other): bool | ||
{ | ||
return !($other instanceof VoidType); | ||
} | ||
|
||
public function getReturnTypeDeclaration(): string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function allowsNull(): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class ObjectType extends Type | ||
{ | ||
/** | ||
* @var TypeName | ||
*/ | ||
private $className; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $allowsNull; | ||
|
||
public function __construct(TypeName $className, bool $allowsNull) | ||
{ | ||
$this->className = $className; | ||
$this->allowsNull = $allowsNull; | ||
} | ||
|
||
public function isAssignable(Type $other): bool | ||
{ | ||
if ($this->allowsNull && $other instanceof NullType) { | ||
return true; | ||
} | ||
|
||
if ($other instanceof self) { | ||
if ($this->className->getQualifiedName() === $other->className->getQualifiedName()) { | ||
return true; | ||
} | ||
|
||
if (\is_subclass_of($other->className->getQualifiedName(), $this->className->getQualifiedName(), true)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function getReturnTypeDeclaration(): string | ||
{ | ||
return ': ' . ($this->allowsNull ? '?' : '') . $this->className->getQualifiedName(); | ||
} | ||
|
||
public function allowsNull(): bool | ||
{ | ||
return $this->allowsNull; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?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; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class SimpleType extends Type | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $allowsNull; | ||
|
||
public function __construct(string $name, bool $nullable) | ||
{ | ||
$this->name = $this->normalize($name); | ||
$this->allowsNull = $nullable; | ||
} | ||
|
||
public function isAssignable(Type $other): bool | ||
{ | ||
if ($this->allowsNull && $other instanceof NullType) { | ||
return true; | ||
} | ||
|
||
if ($other instanceof self) { | ||
return $this->name === $other->name; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function getReturnTypeDeclaration(): string | ||
{ | ||
return ': ' . ($this->allowsNull ? '?' : '') . $this->name; | ||
} | ||
|
||
public function allowsNull(): bool | ||
{ | ||
return $this->allowsNull; | ||
} | ||
|
||
private function normalize(string $name): string | ||
{ | ||
$name = \mb_strtolower($name); | ||
|
||
switch ($name) { | ||
case 'boolean': | ||
return 'bool'; | ||
case 'real': | ||
case 'double': | ||
return 'float'; | ||
case 'integer': | ||
return 'int'; | ||
case '[]': | ||
return 'array'; | ||
default: | ||
return $name; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?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; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
abstract class Type | ||
{ | ||
public static function fromValue($value, bool $allowsNull): self | ||
{ | ||
$type = \gettype($value); | ||
|
||
if ($type === 'object') { | ||
return new ObjectType(TypeName::fromQualifiedName(\get_class($value)), $allowsNull); | ||
} | ||
|
||
return self::fromName($type, $allowsNull); | ||
} | ||
|
||
public static function fromName(string $typeName, bool $allowsNull): self | ||
{ | ||
switch (\strtolower($typeName)) { | ||
case 'null': | ||
return new NullType; | ||
|
||
case 'unknown type': | ||
return new UnknownType; | ||
|
||
case 'void': | ||
return new VoidType; | ||
|
||
case 'object': | ||
case 'boolean': | ||
case 'bool': | ||
case 'integer': | ||
case 'int': | ||
case 'real': | ||
case 'double': | ||
case 'float': | ||
case 'string': | ||
case 'array': | ||
case 'resource': | ||
case 'resource (closed)': | ||
return new SimpleType($typeName, $allowsNull); | ||
|
||
default: | ||
return new ObjectType(TypeName::fromQualifiedName($typeName), $allowsNull); | ||
} | ||
} | ||
|
||
abstract public function isAssignable(self $other): bool; | ||
|
||
abstract public function getReturnTypeDeclaration(): string; | ||
|
||
abstract public function allowsNull(): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?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; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class TypeName | ||
{ | ||
/** | ||
* @var ?string | ||
*/ | ||
private $namespaceName; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $simpleName; | ||
|
||
public static function fromQualifiedName(string $fullClassName): self | ||
{ | ||
if ($fullClassName[0] === '\\') { | ||
$fullClassName = \substr($fullClassName, 1); | ||
} | ||
|
||
$classNameParts = \explode('\\', $fullClassName); | ||
|
||
$simpleName = \array_pop($classNameParts); | ||
$namespaceName = \implode('\\', $classNameParts); | ||
|
||
return new self($namespaceName, $simpleName); | ||
} | ||
|
||
public static function fromReflection(\ReflectionClass $type): self | ||
{ | ||
return new self( | ||
$type->getNamespaceName(), | ||
$type->getShortName() | ||
); | ||
} | ||
|
||
public function __construct(?string $namespaceName, string $simpleName) | ||
{ | ||
if ($namespaceName === '') { | ||
$namespaceName = null; | ||
} | ||
|
||
$this->namespaceName = $namespaceName; | ||
$this->simpleName = $simpleName; | ||
} | ||
|
||
public function getNamespaceName(): ?string | ||
{ | ||
return $this->namespaceName; | ||
} | ||
|
||
public function getSimpleName(): string | ||
{ | ||
return $this->simpleName; | ||
} | ||
|
||
public function getQualifiedName(): string | ||
{ | ||
return $this->namespaceName === null | ||
? $this->simpleName | ||
: $this->namespaceName . '\\' . $this->simpleName; | ||
} | ||
|
||
public function isNamespaced(): bool | ||
{ | ||
return $this->namespaceName !== null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?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; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class UnknownType extends Type | ||
{ | ||
public function isAssignable(Type $other): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getReturnTypeDeclaration(): string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function allowsNull(): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?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; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class VoidType extends Type | ||
{ | ||
public function isAssignable(Type $other): bool | ||
{ | ||
return $other instanceof self; | ||
} | ||
|
||
public function getReturnTypeDeclaration(): string | ||
{ | ||
return ': void'; | ||
} | ||
|
||
public function allowsNull(): bool | ||
{ | ||
return false; | ||
} | ||
} |
Oops, something went wrong.