From 05bc79b8bf6ad5a92c4dfa98fc9cd703c6b23c62 Mon Sep 17 00:00:00 2001 From: Jaapio Date: Fri, 29 Jul 2022 16:55:31 +0200 Subject: [PATCH] Upgrade code to php 7.4 level We are dropping support for php 7.2 and 7.3. --- composer.json | 3 +- composer.lock | 62 ++++++++++- phpcs.xml.dist | 2 +- rector.php | 26 +++++ src/PseudoTypes/False_.php | 2 +- src/PseudoTypes/IntegerRange.php | 6 +- src/PseudoTypes/True_.php | 2 +- src/TypeResolver.php | 121 +++++++++++++--------- src/Types/AggregatedType.php | 5 +- src/Types/ClassString.php | 3 +- src/Types/Collection.php | 3 +- src/Types/Context.php | 4 +- src/Types/Expression.php | 3 +- src/Types/InterfaceString.php | 3 +- src/Types/Nullable.php | 2 +- src/Types/Object_.php | 3 +- tests/unit/CollectionResolverTest.php | 61 ++++++----- tests/unit/FqsenResolverTest.php | 3 +- tests/unit/IntegerRangeResolverTest.php | 11 +- tests/unit/TypeResolverTest.php | 130 ++++++++++++++---------- 20 files changed, 296 insertions(+), 159 deletions(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 510a5ff..a7ae10f 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "phpstan/phpstan": "^1.8", "phpstan/phpstan-phpunit": "^1.1", "phpstan/extension-installer": "^1.1", - "vimeo/psalm": "^4.25" + "vimeo/psalm": "^4.25", + "rector/rector": "^0.13.9" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 732664b..adcb69d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "deb6a09787d5a7696cff3ea71b45b035", + "content-hash": "0995b56862f68800c3c014da7cd2503f", "packages": [ { "name": "phpdocumentor/reflection-common", @@ -1854,6 +1854,66 @@ }, "time": "2021-05-03T11:20:27+00:00" }, + { + "name": "rector/rector", + "version": "0.13.9", + "source": { + "type": "git", + "url": "https://github.com/rectorphp/rector.git", + "reference": "d6319ec783876579b608840cdfe1d8b566c72f74" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/d6319ec783876579b608840cdfe1d8b566c72f74", + "reference": "d6319ec783876579b608840cdfe1d8b566c72f74", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0", + "phpstan/phpstan": "^1.8.2" + }, + "conflict": { + "phpstan/phpdoc-parser": "<1.6.2", + "rector/rector-cakephp": "*", + "rector/rector-doctrine": "*", + "rector/rector-laravel": "*", + "rector/rector-nette": "*", + "rector/rector-phpoffice": "*", + "rector/rector-phpunit": "*", + "rector/rector-prefixed": "*", + "rector/rector-symfony": "*" + }, + "bin": [ + "bin/rector" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "0.13-dev" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Instant Upgrade and Automated Refactoring of any PHP code", + "support": { + "issues": "https://github.com/rectorphp/rector/issues", + "source": "https://github.com/rectorphp/rector/tree/0.13.9" + }, + "funding": [ + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2022-07-23T10:55:44+00:00" + }, { "name": "sebastian/cli-parser", "version": "1.0.1", diff --git a/phpcs.xml.dist b/phpcs.xml.dist index a688b6a..d26097a 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -9,7 +9,7 @@ - + diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..b285d6c --- /dev/null +++ b/rector.php @@ -0,0 +1,26 @@ +paths([ + __DIR__ . '/src', + __DIR__ . '/tests/unit' + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + $rectorConfig->rule(Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector::class); + $rectorConfig->rule(Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector::class); + $rectorConfig->rule(Rector\PHPUnit\Rector\Class_\AddProphecyTraitRector::class); + $rectorConfig->importNames(); + + // define sets of rules + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_74 + ]); +}; diff --git a/src/PseudoTypes/False_.php b/src/PseudoTypes/False_.php index 4ec6885..871c172 100644 --- a/src/PseudoTypes/False_.php +++ b/src/PseudoTypes/False_.php @@ -37,4 +37,4 @@ public function __toString(): string } } -class_alias('\phpDocumentor\Reflection\PseudoTypes\False_', 'phpDocumentor\Reflection\Types\False_', false); +class_alias(False_::class, 'phpDocumentor\Reflection\Types\False_', false); diff --git a/src/PseudoTypes/IntegerRange.php b/src/PseudoTypes/IntegerRange.php index c5a3bc5..37f774d 100644 --- a/src/PseudoTypes/IntegerRange.php +++ b/src/PseudoTypes/IntegerRange.php @@ -24,11 +24,9 @@ */ final class IntegerRange extends Integer implements PseudoType { - /** @var string */ - private $minValue; + private string $minValue; - /** @var string */ - private $maxValue; + private string $maxValue; public function __construct(string $minValue, string $maxValue) { diff --git a/src/PseudoTypes/True_.php b/src/PseudoTypes/True_.php index dc970b3..c9ad1cb 100644 --- a/src/PseudoTypes/True_.php +++ b/src/PseudoTypes/True_.php @@ -37,4 +37,4 @@ public function __toString(): string } } -class_alias('\phpDocumentor\Reflection\PseudoTypes\True_', 'phpDocumentor\Reflection\Types\True_', false); +class_alias(True_::class, 'phpDocumentor\Reflection\Types\True_', false); diff --git a/src/TypeResolver.php b/src/TypeResolver.php index f8bbae8..e5695b8 100644 --- a/src/TypeResolver.php +++ b/src/TypeResolver.php @@ -15,35 +15,60 @@ use ArrayIterator; use InvalidArgumentException; +use phpDocumentor\Reflection\PseudoTypes\CallableString; +use phpDocumentor\Reflection\PseudoTypes\False_; +use phpDocumentor\Reflection\PseudoTypes\HtmlEscapedString; use phpDocumentor\Reflection\PseudoTypes\IntegerRange; use phpDocumentor\Reflection\PseudoTypes\List_; +use phpDocumentor\Reflection\PseudoTypes\LiteralString; +use phpDocumentor\Reflection\PseudoTypes\LowercaseString; +use phpDocumentor\Reflection\PseudoTypes\NegativeInteger; +use phpDocumentor\Reflection\PseudoTypes\NonEmptyLowercaseString; +use phpDocumentor\Reflection\PseudoTypes\NonEmptyString; +use phpDocumentor\Reflection\PseudoTypes\Numeric_; +use phpDocumentor\Reflection\PseudoTypes\NumericString; +use phpDocumentor\Reflection\PseudoTypes\PositiveInteger; +use phpDocumentor\Reflection\PseudoTypes\TraitString; +use phpDocumentor\Reflection\PseudoTypes\True_; use phpDocumentor\Reflection\Types\Array_; use phpDocumentor\Reflection\Types\ArrayKey; +use phpDocumentor\Reflection\Types\Boolean; +use phpDocumentor\Reflection\Types\Callable_; use phpDocumentor\Reflection\Types\ClassString; use phpDocumentor\Reflection\Types\Collection; use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Context; use phpDocumentor\Reflection\Types\Expression; +use phpDocumentor\Reflection\Types\Float_; use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\InterfaceString; use phpDocumentor\Reflection\Types\Intersection; use phpDocumentor\Reflection\Types\Iterable_; +use phpDocumentor\Reflection\Types\Mixed_; +use phpDocumentor\Reflection\Types\Never_; +use phpDocumentor\Reflection\Types\Null_; use phpDocumentor\Reflection\Types\Nullable; use phpDocumentor\Reflection\Types\Object_; +use phpDocumentor\Reflection\Types\Parent_; +use phpDocumentor\Reflection\Types\Resource_; +use phpDocumentor\Reflection\Types\Scalar; +use phpDocumentor\Reflection\Types\Self_; +use phpDocumentor\Reflection\Types\Static_; use phpDocumentor\Reflection\Types\String_; +use phpDocumentor\Reflection\Types\This; +use phpDocumentor\Reflection\Types\Void_; use RuntimeException; use function array_key_exists; +use function array_key_last; use function array_pop; use function array_values; use function class_exists; use function class_implements; use function count; use function current; -use function end; use function in_array; use function is_numeric; -use function key; use function preg_split; use function strpos; use function strtolower; @@ -76,54 +101,51 @@ final class TypeResolver * @var array List of recognized keywords and unto which Value Object they map * @psalm-var array> */ - private $keywords = [ - 'string' => Types\String_::class, - 'class-string' => Types\ClassString::class, - 'interface-string' => Types\InterfaceString::class, - 'html-escaped-string' => PseudoTypes\HtmlEscapedString::class, - 'lowercase-string' => PseudoTypes\LowercaseString::class, - 'non-empty-lowercase-string' => PseudoTypes\NonEmptyLowercaseString::class, - 'non-empty-string' => PseudoTypes\NonEmptyString::class, - 'numeric-string' => PseudoTypes\NumericString::class, - 'numeric' => PseudoTypes\Numeric_::class, - 'trait-string' => PseudoTypes\TraitString::class, - 'int' => Types\Integer::class, - 'integer' => Types\Integer::class, - 'positive-int' => PseudoTypes\PositiveInteger::class, - 'negative-int' => PseudoTypes\NegativeInteger::class, - 'bool' => Types\Boolean::class, - 'boolean' => Types\Boolean::class, - 'real' => Types\Float_::class, - 'float' => Types\Float_::class, - 'double' => Types\Float_::class, - 'object' => Types\Object_::class, - 'mixed' => Types\Mixed_::class, - 'array' => Types\Array_::class, - 'array-key' => Types\ArrayKey::class, - 'resource' => Types\Resource_::class, - 'void' => Types\Void_::class, - 'null' => Types\Null_::class, - 'scalar' => Types\Scalar::class, - 'callback' => Types\Callable_::class, - 'callable' => Types\Callable_::class, - 'callable-string' => PseudoTypes\CallableString::class, - 'false' => PseudoTypes\False_::class, - 'true' => PseudoTypes\True_::class, - 'literal-string' => PseudoTypes\LiteralString::class, - 'self' => Types\Self_::class, - '$this' => Types\This::class, - 'static' => Types\Static_::class, - 'parent' => Types\Parent_::class, - 'iterable' => Types\Iterable_::class, - 'never' => Types\Never_::class, - 'list' => PseudoTypes\List_::class, + private array $keywords = [ + 'string' => String_::class, + 'class-string' => ClassString::class, + 'interface-string' => InterfaceString::class, + 'html-escaped-string' => HtmlEscapedString::class, + 'lowercase-string' => LowercaseString::class, + 'non-empty-lowercase-string' => NonEmptyLowercaseString::class, + 'non-empty-string' => NonEmptyString::class, + 'numeric-string' => NumericString::class, + 'numeric' => Numeric_::class, + 'trait-string' => TraitString::class, + 'int' => Integer::class, + 'integer' => Integer::class, + 'positive-int' => PositiveInteger::class, + 'negative-int' => NegativeInteger::class, + 'bool' => Boolean::class, + 'boolean' => Boolean::class, + 'real' => Float_::class, + 'float' => Float_::class, + 'double' => Float_::class, + 'object' => Object_::class, + 'mixed' => Mixed_::class, + 'array' => Array_::class, + 'array-key' => ArrayKey::class, + 'resource' => Resource_::class, + 'void' => Void_::class, + 'null' => Null_::class, + 'scalar' => Scalar::class, + 'callback' => Callable_::class, + 'callable' => Callable_::class, + 'callable-string' => CallableString::class, + 'false' => False_::class, + 'true' => True_::class, + 'literal-string' => LiteralString::class, + 'self' => Self_::class, + '$this' => This::class, + 'static' => Static_::class, + 'parent' => Parent_::class, + 'iterable' => Iterable_::class, + 'never' => Never_::class, + 'list' => List_::class, ]; - /** - * @var FqsenResolver - * @psalm-readonly - */ - private $fqsenResolver; + /** @psalm-readonly */ + private FqsenResolver $fqsenResolver; /** * Initializes this TypeResolver with the means to create and resolve Fqsen objects. @@ -280,8 +302,7 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser ) { break; } elseif ($token === self::OPERATOR_ARRAY) { - end($types); - $last = key($types); + $last = array_key_last($types); if ($last === null) { throw new InvalidArgumentException('Unexpected array operator'); } diff --git a/src/Types/AggregatedType.php b/src/Types/AggregatedType.php index 472a1cd..257ed51 100644 --- a/src/Types/AggregatedType.php +++ b/src/Types/AggregatedType.php @@ -34,10 +34,9 @@ abstract class AggregatedType implements Type, IteratorAggregate * @psalm-allow-private-mutation * @var array */ - private $types = []; + private array $types = []; - /** @var string */ - private $token; + private string $token; /** * @param array $types diff --git a/src/Types/ClassString.php b/src/Types/ClassString.php index fbdd879..b54c549 100644 --- a/src/Types/ClassString.php +++ b/src/Types/ClassString.php @@ -24,8 +24,7 @@ */ final class ClassString extends String_ implements PseudoType { - /** @var Fqsen|null */ - private $fqsen; + private ?Fqsen $fqsen; /** * Initializes this representation of a class string with the given Fqsen. diff --git a/src/Types/Collection.php b/src/Types/Collection.php index 943cc22..dbf3f19 100644 --- a/src/Types/Collection.php +++ b/src/Types/Collection.php @@ -31,8 +31,7 @@ */ final class Collection extends AbstractList { - /** @var Fqsen|null */ - private $fqsen; + private ?Fqsen $fqsen; /** * Initializes this representation of an array with the given Type or Fqsen. diff --git a/src/Types/Context.php b/src/Types/Context.php index 79aadaf..17aa506 100644 --- a/src/Types/Context.php +++ b/src/Types/Context.php @@ -36,13 +36,13 @@ final class Context { /** @var string The current namespace. */ - private $namespace; + private string $namespace; /** * @var string[] List of namespace aliases => Fully Qualified Namespace. * @psalm-var array */ - private $namespaceAliases; + private array $namespaceAliases; /** * Initializes the new context and normalizes all passed namespaces to be in Qualified Namespace Name (QNN) diff --git a/src/Types/Expression.php b/src/Types/Expression.php index da5f65d..2b6b095 100644 --- a/src/Types/Expression.php +++ b/src/Types/Expression.php @@ -22,8 +22,7 @@ */ final class Expression implements Type { - /** @var Type */ - protected $valueType; + protected Type $valueType; /** * Initializes this representation of an array with the given Type. diff --git a/src/Types/InterfaceString.php b/src/Types/InterfaceString.php index 9836961..0b7dbb8 100644 --- a/src/Types/InterfaceString.php +++ b/src/Types/InterfaceString.php @@ -23,8 +23,7 @@ */ final class InterfaceString implements Type { - /** @var Fqsen|null */ - private $fqsen; + private ?Fqsen $fqsen; /** * Initializes this representation of a class string with the given Fqsen. diff --git a/src/Types/Nullable.php b/src/Types/Nullable.php index a946935..52c3d53 100644 --- a/src/Types/Nullable.php +++ b/src/Types/Nullable.php @@ -23,7 +23,7 @@ final class Nullable implements Type { /** @var Type The actual type that is wrapped */ - private $realType; + private Type $realType; /** * Initialises this nullable type using the real type embedded diff --git a/src/Types/Object_.php b/src/Types/Object_.php index 90dee57..feb6ee3 100644 --- a/src/Types/Object_.php +++ b/src/Types/Object_.php @@ -30,8 +30,7 @@ */ final class Object_ implements Type { - /** @var Fqsen|null */ - private $fqsen; + private ?Fqsen $fqsen; /** * Initializes this object with an optional FQSEN, if not provided this object is considered 'untyped'. diff --git a/tests/unit/CollectionResolverTest.php b/tests/unit/CollectionResolverTest.php index 0d39176..eb25b04 100644 --- a/tests/unit/CollectionResolverTest.php +++ b/tests/unit/CollectionResolverTest.php @@ -13,12 +13,19 @@ namespace phpDocumentor\Reflection; +use InvalidArgumentException; use phpDocumentor\Reflection\PseudoTypes\List_; use phpDocumentor\Reflection\Types\Array_; use phpDocumentor\Reflection\Types\Collection; +use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Context; +use phpDocumentor\Reflection\Types\Float_; +use phpDocumentor\Reflection\Types\Integer; +use phpDocumentor\Reflection\Types\Nullable; use phpDocumentor\Reflection\Types\Object_; +use phpDocumentor\Reflection\Types\String_; use PHPUnit\Framework\TestCase; +use RuntimeException; /** * @covers :: @@ -50,8 +57,8 @@ public function testResolvingCollection(): void $keyType = $resolvedType->getKeyType(); - $this->assertInstanceOf(Types\String_::class, $valueType); - $this->assertInstanceOf(Types\Compound::class, $keyType); + $this->assertInstanceOf(String_::class, $valueType); + $this->assertInstanceOf(Compound::class, $keyType); } /** @@ -78,10 +85,10 @@ public function testResolvingCollectionWithKeyType(): void $keyType = $resolvedType->getKeyType(); - $this->assertInstanceOf(Types\Object_::class, $valueType); + $this->assertInstanceOf(Object_::class, $valueType); $this->assertEquals('\\Iterator', (string) $valueType->getFqsen()); - $this->assertInstanceOf(Types\Array_::class, $keyType); - $this->assertInstanceOf(Types\String_::class, $keyType->getValueType()); + $this->assertInstanceOf(Array_::class, $keyType); + $this->assertInstanceOf(String_::class, $keyType->getValueType()); } /** @@ -106,8 +113,8 @@ public function testResolvingArrayCollection(): void $keyType = $resolvedType->getKeyType(); - $this->assertInstanceOf(Types\String_::class, $valueType); - $this->assertInstanceOf(Types\Compound::class, $keyType); + $this->assertInstanceOf(String_::class, $valueType); + $this->assertInstanceOf(Compound::class, $keyType); } /** @@ -132,8 +139,8 @@ public function testResolvingArrayCollectionWithKey(): void $keyType = $resolvedType->getKeyType(); - $this->assertInstanceOf(Types\String_::class, $keyType); - $this->assertInstanceOf(Types\Compound::class, $valueType); + $this->assertInstanceOf(String_::class, $keyType); + $this->assertInstanceOf(Compound::class, $valueType); } /** @@ -158,8 +165,8 @@ public function testResolvingArrayCollectionWithKeyAndWhitespace(): void $keyType = $resolvedType->getKeyType(); - $this->assertInstanceOf(Types\String_::class, $keyType); - $this->assertInstanceOf(Types\Compound::class, $valueType); + $this->assertInstanceOf(String_::class, $keyType); + $this->assertInstanceOf(Compound::class, $valueType); } /** @@ -173,7 +180,7 @@ public function testResolvingArrayCollectionWithKeyAndWhitespace(): void */ public function testResolvingArrayCollectionWithKeyAndTooManyWhitespace(): void { - $this->expectException('InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); $fixture = new TypeResolver(); $fixture->resolve('array', new Context('')); @@ -200,19 +207,19 @@ public function testResolvingCollectionOfCollection(): void $this->assertEquals('\\ArrayObject', (string) $resolvedType->getFqsen()); $valueType = $resolvedType->getValueType(); - $this->assertInstanceOf(Types\Collection::class, $valueType); + $this->assertInstanceOf(Collection::class, $valueType); $collectionValueType = $valueType->getValueType(); - $this->assertInstanceOf(Types\Object_::class, $valueType->getValueType()); + $this->assertInstanceOf(Object_::class, $valueType->getValueType()); $this->assertEquals('\\ArrayObject', (string) $valueType->getFqsen()); $this->assertInstanceOf(Object_::class, $collectionValueType); $this->assertEquals('\\DateTime', (string) $collectionValueType->getFqsen()); $keyType = $resolvedType->getKeyType(); - $this->assertInstanceOf(Types\Compound::class, $keyType); - $this->assertInstanceOf(Types\String_::class, $keyType->get(0)); - $this->assertInstanceOf(Types\Integer::class, $keyType->get(1)); - $this->assertInstanceOf(Types\Float_::class, $keyType->get(2)); + $this->assertInstanceOf(Compound::class, $keyType); + $this->assertInstanceOf(String_::class, $keyType->get(0)); + $this->assertInstanceOf(Integer::class, $keyType->get(1)); + $this->assertInstanceOf(Float_::class, $keyType->get(2)); } /** @@ -221,7 +228,7 @@ public function testResolvingCollectionOfCollection(): void */ public function testBadArrayCollectionKey(): void { - $this->expectException('RuntimeException'); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('An array can have only integers or strings as keys'); $fixture = new TypeResolver(); $fixture->resolve('array', new Context('')); @@ -252,7 +259,7 @@ public function testGoodArrayCollectionKey(): void */ public function testMissingStartCollection(): void { - $this->expectException('RuntimeException'); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Unexpected collection operator "<", class name is missing'); $fixture = new TypeResolver(); $fixture->resolve('', new Context('')); @@ -264,7 +271,7 @@ public function testMissingStartCollection(): void */ public function testMissingEndCollection(): void { - $this->expectException('RuntimeException'); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Collection: ">" is missing'); $fixture = new TypeResolver(); $fixture->resolve('ArrayObjectexpectException('RuntimeException'); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('string is not a collection'); $fixture = new TypeResolver(); $fixture->resolve('string', new Context('')); @@ -304,8 +311,8 @@ public function testResolvingCollectionAsArray(): void $keyType = $resolvedType->getKeyType(); - $this->assertInstanceOf(Types\Float_::class, $valueType); - $this->assertInstanceOf(Types\String_::class, $keyType); + $this->assertInstanceOf(Float_::class, $valueType); + $this->assertInstanceOf(String_::class, $keyType); } /** @@ -328,8 +335,8 @@ public function testResolvingList(): void $keyType = $resolvedType->getKeyType(); - $this->assertInstanceOf(Types\String_::class, $valueType); - $this->assertInstanceOf(Types\Integer::class, $keyType); + $this->assertInstanceOf(String_::class, $valueType); + $this->assertInstanceOf(Integer::class, $keyType); } /** @@ -345,7 +352,7 @@ public function testResolvingNullableArray(): void $resolvedType = $fixture->resolve('?array', new Context('')); - $this->assertInstanceOf(Types\Nullable::class, $resolvedType); + $this->assertInstanceOf(Nullable::class, $resolvedType); $this->assertSame('?int[]', (string) $resolvedType); } } diff --git a/tests/unit/FqsenResolverTest.php b/tests/unit/FqsenResolverTest.php index 2cecc5f..d07eea7 100644 --- a/tests/unit/FqsenResolverTest.php +++ b/tests/unit/FqsenResolverTest.php @@ -13,6 +13,7 @@ namespace phpDocumentor\Reflection; +use InvalidArgumentException; use phpDocumentor\Reflection\Types\Context; use PHPUnit\Framework\TestCase; @@ -87,7 +88,7 @@ public function testResolveFromPartialAlias(): void public function testResolveThrowsExceptionWhenGarbageInputIsPassed(): void { - $this->expectException('InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); $fqsenResolver = new FqsenResolver(); $context = new Context('', []); diff --git a/tests/unit/IntegerRangeResolverTest.php b/tests/unit/IntegerRangeResolverTest.php index c6dc757..e3008e0 100644 --- a/tests/unit/IntegerRangeResolverTest.php +++ b/tests/unit/IntegerRangeResolverTest.php @@ -16,6 +16,7 @@ use phpDocumentor\Reflection\PseudoTypes\IntegerRange; use phpDocumentor\Reflection\Types\Context; use PHPUnit\Framework\TestCase; +use RuntimeException; /** * @covers :: @@ -84,7 +85,7 @@ public function testResolvingIntRangeWithKeywords(): void */ public function testResolvingIntRangeErrorMisingMaxValue(): void { - $this->expectException('RuntimeException'); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('int has not the correct format'); $fixture = new TypeResolver(); @@ -102,7 +103,7 @@ public function testResolvingIntRangeErrorMisingMaxValue(): void */ public function testResolvingIntRangeErrorMisingMinValue(): void { - $this->expectException('RuntimeException'); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('int has not the correct format'); $fixture = new TypeResolver(); @@ -120,7 +121,7 @@ public function testResolvingIntRangeErrorMisingMinValue(): void */ public function testResolvingIntRangeErrorMisingComma(): void { - $this->expectException('RuntimeException'); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('int has not the correct format'); $fixture = new TypeResolver(); @@ -138,7 +139,7 @@ public function testResolvingIntRangeErrorMisingComma(): void */ public function testResolvingIntRangeErrorMissingEnd(): void { - $this->expectException('RuntimeException'); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Unexpected character "max", ">" is missing'); $fixture = new TypeResolver(); @@ -156,7 +157,7 @@ public function testResolvingIntRangeErrorMissingEnd(): void */ public function testResolvingIntRangeErrorFormat(): void { - $this->expectException('RuntimeException'); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage('int has not the correct format'); $fixture = new TypeResolver(); diff --git a/tests/unit/TypeResolverTest.php b/tests/unit/TypeResolverTest.php index 47557a6..be168f0 100644 --- a/tests/unit/TypeResolverTest.php +++ b/tests/unit/TypeResolverTest.php @@ -13,21 +13,49 @@ namespace phpDocumentor\Reflection; +use InvalidArgumentException; +use phpDocumentor\Reflection\PseudoTypes\CallableString; +use phpDocumentor\Reflection\PseudoTypes\False_; +use phpDocumentor\Reflection\PseudoTypes\HtmlEscapedString; +use phpDocumentor\Reflection\PseudoTypes\List_; +use phpDocumentor\Reflection\PseudoTypes\LiteralString; +use phpDocumentor\Reflection\PseudoTypes\LowercaseString; +use phpDocumentor\Reflection\PseudoTypes\NegativeInteger; +use phpDocumentor\Reflection\PseudoTypes\NonEmptyLowercaseString; +use phpDocumentor\Reflection\PseudoTypes\NonEmptyString; +use phpDocumentor\Reflection\PseudoTypes\Numeric_; +use phpDocumentor\Reflection\PseudoTypes\NumericString; +use phpDocumentor\Reflection\PseudoTypes\PositiveInteger; +use phpDocumentor\Reflection\PseudoTypes\TraitString; +use phpDocumentor\Reflection\PseudoTypes\True_; use phpDocumentor\Reflection\Types\Array_; +use phpDocumentor\Reflection\Types\ArrayKey; use phpDocumentor\Reflection\Types\Boolean; +use phpDocumentor\Reflection\Types\Callable_; use phpDocumentor\Reflection\Types\ClassString; use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Context; use phpDocumentor\Reflection\Types\Expression; +use phpDocumentor\Reflection\Types\Float_; use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\InterfaceString; use phpDocumentor\Reflection\Types\Intersection; use phpDocumentor\Reflection\Types\Iterable_; +use phpDocumentor\Reflection\Types\Mixed_; +use phpDocumentor\Reflection\Types\Never_; use phpDocumentor\Reflection\Types\Null_; use phpDocumentor\Reflection\Types\Nullable; use phpDocumentor\Reflection\Types\Object_; +use phpDocumentor\Reflection\Types\Parent_; +use phpDocumentor\Reflection\Types\Resource_; +use phpDocumentor\Reflection\Types\Scalar; +use phpDocumentor\Reflection\Types\Self_; +use phpDocumentor\Reflection\Types\Static_; use phpDocumentor\Reflection\Types\String_; +use phpDocumentor\Reflection\Types\This; +use phpDocumentor\Reflection\Types\Void_; use PHPUnit\Framework\TestCase; +use RuntimeException; use stdClass; use function get_class; @@ -73,7 +101,7 @@ public function testResolvingClassStrings(string $classString, bool $throwsExcep $fixture = new TypeResolver(); if ($throwsException) { - $this->expectException('RuntimeException'); + $this->expectException(RuntimeException::class); } $resolvedType = $fixture->resolve($classString, new Context('')); @@ -97,7 +125,7 @@ public function testResolvingInterfaceStrings(string $interfaceString, bool $thr $fixture = new TypeResolver(); if ($throwsException) { - $this->expectException('RuntimeException'); + $this->expectException(RuntimeException::class); } $resolvedType = $fixture->resolve($interfaceString, new Context('')); @@ -191,7 +219,7 @@ public function testResolvingTypedArrays(): void $this->assertInstanceOf(Array_::class, $resolvedType); $this->assertSame('string[]', (string) $resolvedType); $this->assertInstanceOf(Compound::class, $resolvedType->getKeyType()); - $this->assertInstanceOf(Types\String_::class, $resolvedType->getValueType()); + $this->assertInstanceOf(String_::class, $resolvedType->getValueType()); } /** @@ -239,7 +267,7 @@ public function testResolvingNestedTypedArrays(): void $this->assertSame('string[]', (string) $childValueType); $this->assertInstanceOf(Compound::class, $childValueType->getKeyType()); - $this->assertInstanceOf(Types\String_::class, $childValueType->getValueType()); + $this->assertInstanceOf(String_::class, $childValueType->getValueType()); } /** @@ -267,7 +295,7 @@ public function testResolvingCompoundTypes(): void $secondType = $resolvedType->get(1); - $this->assertInstanceOf(Types\String_::class, $firstType); + $this->assertInstanceOf(String_::class, $firstType); $this->assertInstanceOf(Object_::class, $secondType); $this->assertInstanceOf(Fqsen::class, $secondType->getFqsen()); } @@ -649,9 +677,9 @@ public function testResolvingCompoundTypesWithTwoArrays(): void $secondType = $resolvedType->get(1); $this->assertInstanceOf(Array_::class, $firstType); - $this->assertInstanceOf(Types\Integer::class, $firstType->getValueType()); + $this->assertInstanceOf(Integer::class, $firstType->getValueType()); $this->assertInstanceOf(Array_::class, $secondType); - $this->assertInstanceOf(Types\String_::class, $secondType->getValueType()); + $this->assertInstanceOf(String_::class, $secondType->getValueType()); } /** @@ -685,7 +713,7 @@ public function testAddingAKeyword(): void */ public function testAddingAKeywordFailsIfTypeClassDoesNotExist(): void { - $this->expectException('InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); $fixture = new TypeResolver(); $fixture->addKeyword('mock', 'IDoNotExist'); } @@ -698,7 +726,7 @@ public function testAddingAKeywordFailsIfTypeClassDoesNotExist(): void */ public function testAddingAKeywordFailsIfTypeClassDoesNotImplementTypeInterface(): void { - $this->expectException('InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); $fixture = new TypeResolver(); $fixture->addKeyword('mock', stdClass::class); } @@ -711,7 +739,7 @@ public function testAddingAKeywordFailsIfTypeClassDoesNotImplementTypeInterface( */ public function testExceptionIsThrownIfTypeIsEmpty(): void { - $this->expectException('InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); $fixture = new TypeResolver(); $fixture->resolve(' ', new Context('')); } @@ -724,7 +752,7 @@ public function testExceptionIsThrownIfTypeIsEmpty(): void */ public function testInvalidArrayOperator(): void { - $this->expectException('InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); $fixture = new TypeResolver(); $fixture->resolve('[]', new Context('')); } @@ -737,46 +765,46 @@ public function testInvalidArrayOperator(): void public function provideKeywords(): array { return [ - ['string', Types\String_::class], - ['class-string', Types\ClassString::class], - ['html-escaped-string', PseudoTypes\HtmlEscapedString::class], - ['lowercase-string', PseudoTypes\LowercaseString::class], - ['non-empty-lowercase-string', PseudoTypes\NonEmptyLowercaseString::class], - ['non-empty-string', PseudoTypes\NonEmptyString::class], - ['numeric-string', PseudoTypes\NumericString::class], - ['numeric', PseudoTypes\Numeric_::class], - ['trait-string', PseudoTypes\TraitString::class], - ['int', Types\Integer::class], - ['integer', Types\Integer::class], - ['positive-int', PseudoTypes\PositiveInteger::class], - ['negative-int', PseudoTypes\NegativeInteger::class], - ['float', Types\Float_::class], - ['double', Types\Float_::class], - ['bool', Types\Boolean::class], - ['boolean', Types\Boolean::class], - ['true', Types\Boolean::class], - ['true', PseudoTypes\True_::class], - ['false', Types\Boolean::class], - ['false', PseudoTypes\False_::class], - ['resource', Types\Resource_::class], - ['null', Types\Null_::class], - ['callable', Types\Callable_::class], - ['callable-string', PseudoTypes\CallableString::class], - ['callback', Types\Callable_::class], - ['array', Types\Array_::class], - ['array-key', Types\ArrayKey::class], - ['scalar', Types\Scalar::class], - ['object', Types\Object_::class], - ['mixed', Types\Mixed_::class], - ['void', Types\Void_::class], - ['$this', Types\This::class], - ['static', Types\Static_::class], - ['self', Types\Self_::class], - ['parent', Types\Parent_::class], - ['iterable', Types\Iterable_::class], - ['never', Types\Never_::class], - ['literal-string', PseudoTypes\LiteralString::class], - ['list', PseudoTypes\List_::class], + ['string', String_::class], + ['class-string', ClassString::class], + ['html-escaped-string', HtmlEscapedString::class], + ['lowercase-string', LowercaseString::class], + ['non-empty-lowercase-string', NonEmptyLowercaseString::class], + ['non-empty-string', NonEmptyString::class], + ['numeric-string', NumericString::class], + ['numeric', Numeric_::class], + ['trait-string', TraitString::class], + ['int', Integer::class], + ['integer', Integer::class], + ['positive-int', PositiveInteger::class], + ['negative-int', NegativeInteger::class], + ['float', Float_::class], + ['double', Float_::class], + ['bool', Boolean::class], + ['boolean', Boolean::class], + ['true', Boolean::class], + ['true', True_::class], + ['false', Boolean::class], + ['false', False_::class], + ['resource', Resource_::class], + ['null', Null_::class], + ['callable', Callable_::class], + ['callable-string', CallableString::class], + ['callback', Callable_::class], + ['array', Array_::class], + ['array-key', ArrayKey::class], + ['scalar', Scalar::class], + ['object', Object_::class], + ['mixed', Mixed_::class], + ['void', Void_::class], + ['$this', This::class], + ['static', Static_::class], + ['self', Self_::class], + ['parent', Parent_::class], + ['iterable', Iterable_::class], + ['never', Never_::class], + ['literal-string', LiteralString::class], + ['list', List_::class], ]; }