Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Sep 14, 2024
1 parent 2bb0ab4 commit 9636e83
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tests/PHPStan/Analyser/nsrt/more-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Foo
* @param non-empty-scalar $nonEmptyScalar
* @param empty-scalar $emptyScalar
* @param non-empty-mixed $nonEmptyMixed
* @param lowercase-string $lowercaseString
* @param non-empty-lowercase-string $nonEmptyLowercaseString
*/
public function doFoo(
$pureCallable,
Expand All @@ -27,7 +29,9 @@ public function doFoo(
$nonEmptyLiteralString,
$nonEmptyScalar,
$emptyScalar,
$nonEmptyMixed
$nonEmptyMixed,
$lowercaseString,
$nonEmptyLowercaseString,
): void
{
assertType('pure-callable(): mixed', $pureCallable);
Expand All @@ -39,6 +43,8 @@ public function doFoo(
assertType('float|int<min, -1>|int<1, max>|non-falsy-string|true', $nonEmptyScalar);
assertType("0|0.0|''|'0'|false", $emptyScalar);
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $nonEmptyMixed);
assertType('lowercase-string', $lowercaseString);
assertType('lowercase-string&non-empty-string', $nonEmptyLowercaseString);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,54 @@ public function testBug10697(): void
$this->analyse([__DIR__ . '/data/bug-10697.php'], []);
}

public function testLowercaseString(): void
{
$errors = [
[
"Strict comparison using === between lowercase-string and 'AB' will always evaluate to false.",
10,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
"Strict comparison using === between 'AB' and lowercase-string will always evaluate to false.",
11,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
"Strict comparison using !== between 'AB' and lowercase-string will always evaluate to true.",
12,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
"Strict comparison using === between lowercase-string and 'aBc' will always evaluate to false.",
15,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
"Strict comparison using !== between lowercase-string and 'aBc' will always evaluate to true.",
16,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
];

if (PHP_VERSION_ID < 80000) {
$errors[] = [
"Strict comparison using === between lowercase-string|false and 'AB' will always evaluate to false.",
28,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
];
} else {
$errors[] = [
"Strict comparison using === between lowercase-string and 'AB' will always evaluate to false.",
28,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
];
}

$this->checkAlwaysTrueStrictComparison = true;
$this->analyse([__DIR__ . '/data/lowercase-string.php'], $errors);
}

public function testBug10493(): void
{
$this->checkAlwaysTrueStrictComparison = true;
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/lowercase-string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace LowercaseString;

class Foo
{

/** @param lowercase-string $s */
function doFoo($s) {
if ($s === 'AB') {}
if ('AB' === $s) {}
if ('AB' !== $s) {}
if ($s === 'ab') {}
if ($s !== 'ab') {}
if ($s === 'aBc') {}
if ($s !== 'aBc') {}
if ($s === '01') {}
if ($s === '1e2') {}
if ($s === MyClass::myConst) {}
if ($s === A_GLOBAL_CONST) {}
if (doFoo('hi') === 'AB') {}
}

/** @param lowercase-string $s */
function doFoo2($s, int $x, int $y) {
$part = substr($s, $x, $y);

if ($part === 'AB') {}
}

}

0 comments on commit 9636e83

Please sign in to comment.