Skip to content

Commit 98e2b6e

Browse files
authored
More precise md5/sha1 return type
1 parent ee802d6 commit 98e2b6e

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

Diff for: resources/functionMap.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -6403,8 +6403,8 @@
64036403
'mcrypt_module_open' => ['resource|false', 'cipher'=>'string', 'cipher_directory'=>'string', 'mode'=>'string', 'mode_directory'=>'string'],
64046404
'mcrypt_module_self_test' => ['bool', 'algorithm'=>'string', 'lib_dir='=>'string'],
64056405
'mcrypt_ofb' => ['string', 'cipher'=>'string', 'key'=>'string', 'data'=>'string', 'mode'=>'int', 'iv='=>'string'],
6406-
'md5' => ['non-falsy-string', 'str'=>'string', 'raw_output='=>'bool'],
6407-
'md5_file' => ['non-falsy-string|false', 'filename'=>'string', 'raw_output='=>'bool'],
6406+
'md5' => ['non-falsy-string&lowercase-string', 'str'=>'string', 'raw_output='=>'bool'],
6407+
'md5_file' => ['(non-falsy-string&lowercase-string)|false', 'filename'=>'string', 'raw_output='=>'bool'],
64086408
'mdecrypt_generic' => ['string', 'td'=>'resource', 'data'=>'string'],
64096409
'Memcache::add' => ['bool', 'key'=>'string', 'var'=>'mixed', 'flag='=>'int', 'expire='=>'int'],
64106410
'Memcache::addServer' => ['bool', 'host'=>'string', 'port='=>'int', 'persistent='=>'bool', 'weight='=>'int', 'timeout='=>'int', 'retry_interval='=>'int', 'status='=>'bool', 'failure_callback='=>'callable', 'timeoutms='=>'int'],
@@ -10446,8 +10446,8 @@
1044610446
'setRightFill' => ['void', 'red'=>'int', 'green'=>'int', 'blue'=>'int', 'a='=>'int'],
1044710447
'setthreadtitle' => ['bool', 'title'=>'string'],
1044810448
'settype' => ['bool', '&rw_var'=>'mixed', 'type'=>'string'],
10449-
'sha1' => ['non-falsy-string', 'str'=>'string', 'raw_output='=>'bool'],
10450-
'sha1_file' => ['non-falsy-string|false', 'filename'=>'string', 'raw_output='=>'bool'],
10449+
'sha1' => ['non-falsy-string&lowercase-string', 'str'=>'string', 'raw_output='=>'bool'],
10450+
'sha1_file' => ['(non-falsy-string&lowercase-string)|false', 'filename'=>'string', 'raw_output='=>'bool'],
1045110451
'sha256' => ['string', 'str'=>'string', 'raw_output='=>'bool'],
1045210452
'sha256_file' => ['string', 'filename'=>'string', 'raw_output='=>'bool'],
1045310453
'shapefileObj::__construct' => ['void', 'filename'=>'string', 'type'=>'int'],

Diff for: tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -1121,4 +1121,24 @@ public function testBug10493(): void
11211121
$this->analyse([__DIR__ . '/data/bug-10493.php'], []);
11221122
}
11231123

1124+
public function testHashing(): void
1125+
{
1126+
$this->checkAlwaysTrueStrictComparison = true;
1127+
$this->analyse([__DIR__ . '/data/hashing.php'], [
1128+
[
1129+
"Strict comparison using === between lowercase-string&non-falsy-string and 'ABC' will always evaluate to false.",
1130+
9,
1131+
],
1132+
[
1133+
"Strict comparison using === between (lowercase-string&non-falsy-string)|false and 'ABC' will always evaluate to false.",
1134+
12,
1135+
],
1136+
[
1137+
"Strict comparison using === between (lowercase-string&non-falsy-string)|(non-falsy-string&numeric-string) and 'A' will always evaluate to false.",
1138+
31,
1139+
'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%</>.',
1140+
],
1141+
]);
1142+
}
1143+
11241144
}

Diff for: tests/PHPStan/Rules/Comparison/data/hashing.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Hashing;
4+
5+
use function md5;
6+
use function md5_file;
7+
8+
function doFoo(string $s):void {
9+
if (md5($s) === 'ABC') {
10+
11+
}
12+
if (md5_file($s) === 'ABC') {
13+
14+
}
15+
}
16+
17+
/**
18+
* @param (non-falsy-string&numeric-string)|(non-falsy-string&lowercase-string) $s
19+
* @return void
20+
*/
21+
function doFooBar($s) {
22+
if ($s === '598d4c200461b81522a3328565c25f7c') {
23+
24+
}
25+
if ($s === 'a') {
26+
27+
}
28+
if ($s === '123') {
29+
30+
}
31+
if ($s === 'A') {
32+
33+
}
34+
}

0 commit comments

Comments
 (0)