From 67f970c81ae8633ad85b6cb332a64b4ec7860b74 Mon Sep 17 00:00:00 2001 From: Martin Herndl Date: Mon, 17 Jan 2022 12:32:55 +0100 Subject: [PATCH] Revert "Support string assertions resulting in non-empty-string" This reverts commit 90091355b55dab2ad45fa1cf88b7114523940c09. --- README.md | 12 ---- .../AssertTypeSpecifyingExtension.php | 54 ++++---------- tests/Type/WebMozartAssert/data/string.php | 72 ------------------- 3 files changed, 12 insertions(+), 126 deletions(-) diff --git a/README.md b/README.md index 652e606..c72b5b5 100644 --- a/README.md +++ b/README.md @@ -69,22 +69,10 @@ This extension specifies types of values passed to: * `Assert::methodExists` * `Assert::propertyExists` * `Assert::isArrayAccessible` -* `Assert::unicodeLetters` -* `Assert::alpha` -* `Assert::digits` -* `Assert::alnum` -* `Assert::lower` -* `Assert::upper` * `Assert::length` * `Assert::minLength` * `Assert::maxLength` * `Assert::lengthBetween` -* `Assert::uuid` -* `Assert::ip` -* `Assert::ipv4` -* `Assert::ipv6` -* `Assert::email` -* `Assert::notWhitespaceOnly` * `nullOr*` and `all*` variants of the above methods diff --git a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php index 012dc41..b05be48 100644 --- a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php +++ b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php @@ -28,22 +28,6 @@ class AssertTypeSpecifyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension { - private const ASSERTIONS_RESULTING_IN_NON_EMPTY_STRING = [ - 'stringNotEmpty', - 'unicodeLetters', - 'alpha', - 'digits', - 'alnum', - 'lower', - 'upper', - 'uuid', - 'ip', - 'ipv4', - 'ipv6', - 'email', - 'notWhitespaceOnly', - ]; - /** @var \Closure[] */ private static $resolvers; @@ -66,10 +50,6 @@ public function isStaticMethodSupported( TypeSpecifierContext $context ): bool { - if (in_array($staticMethodReflection->getName(), self::ASSERTIONS_RESULTING_IN_NON_EMPTY_STRING, true)) { - return true; - } - if (substr($staticMethodReflection->getName(), 0, 6) === 'allNot') { $methods = [ 'allNotInstanceOf' => 2, @@ -164,11 +144,6 @@ private static function createExpression( ): ?\PhpParser\Node\Expr { $trimmedName = self::trimName($name); - - if (in_array($trimmedName, self::ASSERTIONS_RESULTING_IN_NON_EMPTY_STRING, true)) { - return self::createIsNonEmptyStringExpression($args); - } - $resolvers = self::getExpressionResolvers(); $resolver = $resolvers[$trimmedName]; $expression = $resolver($scope, ...$args); @@ -220,6 +195,18 @@ private static function getExpressionResolvers(): array [$value] ); }, + 'stringNotEmpty' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr { + return new BooleanAnd( + new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('is_string'), + [$value] + ), + new NotIdentical( + $value->value, + new String_('') + ) + ); + }, 'float' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr { return new \PhpParser\Node\Expr\FuncCall( new \PhpParser\Node\Name('is_float'), @@ -687,21 +674,4 @@ private function arrayOrIterable( ); } - /** - * @param \PhpParser\Node\Arg[] $args - */ - private static function createIsNonEmptyStringExpression(array $args): \PhpParser\Node\Expr - { - return new BooleanAnd( - new \PhpParser\Node\Expr\FuncCall( - new \PhpParser\Node\Name('is_string'), - [$args[0]] - ), - new NotIdentical( - $args[0]->value, - new String_('') - ) - ); - } - } diff --git a/tests/Type/WebMozartAssert/data/string.php b/tests/Type/WebMozartAssert/data/string.php index 9f89cc4..e76972c 100644 --- a/tests/Type/WebMozartAssert/data/string.php +++ b/tests/Type/WebMozartAssert/data/string.php @@ -49,76 +49,4 @@ public function lengthBetween(string $a, string $b, string $c, string $d): void \PHPStan\Testing\assertType('non-empty-string', $d); } - public function unicodeLetters($a): void - { - Assert::unicodeLetters($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - - public function alpha($a): void - { - Assert::alpha($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - - public function digits(string $a): void - { - Assert::digits($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - - public function alnum(string $a): void - { - Assert::alnum($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - - public function lower(string $a): void - { - Assert::lower($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - - public function upper(string $a): void - { - Assert::upper($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - - public function uuid(string $a): void - { - Assert::uuid($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - - public function ip($a): void - { - Assert::ip($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - - public function ipv4($a): void - { - Assert::ipv4($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - - public function ipv6($a): void - { - Assert::ipv6($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - - public function email($a): void - { - Assert::email($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - - public function notWhitespaceOnly(string $a): void - { - Assert::notWhitespaceOnly($a); - \PHPStan\Testing\assertType('non-empty-string', $a); - } - }