Skip to content

Commit

Permalink
Revert "Correct Psalm’s concept of string emptiness"
Browse files Browse the repository at this point in the history
This reverts commit 331ce8e.

It was too hasty
  • Loading branch information
muglug committed Feb 3, 2021
1 parent 331ce8e commit 03665b9
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 49 deletions.
8 changes: 4 additions & 4 deletions src/Psalm/Internal/Codebase/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use const PATHINFO_EXTENSION;

/**
* @psalm-type TaggedCodeType = array<int, array{0: int, 1: string}>
* @psalm-type TaggedCodeType = array<int, array{0: int, 1: non-empty-string}>
*
* @psalm-type FileMapType = array{
* 0: TaggedCodeType,
Expand Down Expand Up @@ -153,12 +153,12 @@ class Analyzer
private $existing_issues = [];

/**
* @var array<string, array<int, array{0: int, 1: string}>>
* @var array<string, array<int, array{0: int, 1: non-empty-string}>>
*/
private $reference_map = [];

/**
* @var array<string, array<int, array{0: int, 1: string}>>
* @var array<string, array<int, array{0: int, 1: non-empty-string}>>
*/
private $type_map = [];

Expand Down Expand Up @@ -1161,7 +1161,7 @@ public function addNodeArgument(
string $reference,
int $argument_number
): void {
if (!$reference) {
if ($reference === '') {
throw new \UnexpectedValueException('non-empty reference expected');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Codebase/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function getFullyQualifiedFunctionNameFromString(string $function_name, S
if ($function_name[0] === '\\') {
$function_name = substr($function_name, 1);

if (!$function_name) {
if ($function_name === '') {
throw new \UnexpectedValueException('Malformed function name');
}

Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ public static function isContainedBy(

if ($container_type_part instanceof TNonEmptyString
&& $input_type_part instanceof TLiteralString
&& ($input_type_part->value === ''
|| $input_type_part->value === '0')
&& $input_type_part->value === ''
) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Psalm/Internal/Type/NegatedAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ private static function handleLiteralNegatedEquality(

$did_remove_type = true;
}
} elseif ($assertion === 'string()') {
$existing_var_type->addType(new Type\Atomic\TNonEmptyString());
}
} elseif ($scalar_type === 'string') {
$scalar_value = substr($assertion, $bracket_pos + 1, -1);
Expand Down
12 changes: 6 additions & 6 deletions tests/Internal/Provider/FakeFileReferenceCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class FakeFileReferenceCacheProvider extends \Psalm\Internal\Provider\FileRefere
* @var array<
* string,
* array{
* 0: array<int, array{0: int, 1: string}>,
* 1: array<int, array{0: int, 1: string}>,
* 0: array<int, array{0: int, 1: non-empty-string}>,
* 1: array<int, array{0: int, 1: non-empty-string}>,
* 2: array<int, array{0: int, 1: non-empty-string, 2: int}>
* }
* >
Expand Down Expand Up @@ -190,8 +190,8 @@ public function setAnalyzedMethodCache(array $analyzed_methods): void
* @return array<
* string,
* array{
* 0: array<int, array{0: int, 1: string}>,
* 1: array<int, array{0: int, 1: string}>,
* 0: array<int, array{0: int, 1: non-empty-string}>,
* 1: array<int, array{0: int, 1: non-empty-string}>,
* 2: array<int, array{0: int, 1: non-empty-string, 2: int}>
* }
* >
Expand All @@ -205,8 +205,8 @@ public function getFileMapCache(): array
* @param array<
* string,
* array{
* 0: array<int, array{0: int, 1: string}>,
* 1: array<int, array{0: int, 1: string}>,
* 0: array<int, array{0: int, 1: non-empty-string}>,
* 1: array<int, array{0: int, 1: non-empty-string}>,
* 2: array<int, array{0: int, 1: non-empty-string, 2: int}>
* }
* > $file_maps
Expand Down
4 changes: 2 additions & 2 deletions tests/TypeReconciliation/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2411,7 +2411,7 @@ function splitDocLine($return_block)
return [$type];
}'
],
'SKIPPED-nonEmptyStringAfterLiteralCheck' => [
'nonEmptyStringAfterLiteralCheck' => [
'<?php
/**
* @param non-empty-string $greeting
Expand All @@ -2423,7 +2423,7 @@ function sayHi(string $greeting): void {
/** @var string */
$hello = "foo";
if ($hello === "" || $hello === "0") {
if ($hello === "") {
throw new \Exception("an empty string is not a greeting");
}
Expand Down
34 changes: 0 additions & 34 deletions tests/TypeReconciliation/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1053,40 +1053,6 @@ function bar(string $s) : void {
}',
'error_message' => 'ArgumentTypeCoercion'
],
'zeroIsEmptyString' => [
'<?php
/**
* @param non-empty-string $s
*/
function foo(string $s) : void {}
foo("0");',
'error_message' => 'InvalidArgument'
],
'notLiteralEmptyIsNotNotEmptyString' => [
'<?php
/**
* @param non-empty-string $s
*/
function foo(string $s) : void {}
function takesString(string $s) : void {
if ($s !== "") {
foo($s);
}
}',
'error_message' => 'ArgumentTypeCoercion'
],
'nonEmptyStringCannotBeStringZero' => [
'<?php
/**
* @param non-empty-string $s
*/
function foo(string $s) : void {
if ($s === "0") {}
}',
'error_message' => 'TypeDoesNotContainType'
],
];
}
}

0 comments on commit 03665b9

Please sign in to comment.