Skip to content

Commit

Permalink
More precise md5/sha1 return type
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Oct 8, 2024
1 parent ee802d6 commit 98e2b6e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
8 changes: 4 additions & 4 deletions resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6403,8 +6403,8 @@
'mcrypt_module_open' => ['resource|false', 'cipher'=>'string', 'cipher_directory'=>'string', 'mode'=>'string', 'mode_directory'=>'string'],
'mcrypt_module_self_test' => ['bool', 'algorithm'=>'string', 'lib_dir='=>'string'],
'mcrypt_ofb' => ['string', 'cipher'=>'string', 'key'=>'string', 'data'=>'string', 'mode'=>'int', 'iv='=>'string'],
'md5' => ['non-falsy-string', 'str'=>'string', 'raw_output='=>'bool'],
'md5_file' => ['non-falsy-string|false', 'filename'=>'string', 'raw_output='=>'bool'],
'md5' => ['non-falsy-string&lowercase-string', 'str'=>'string', 'raw_output='=>'bool'],
'md5_file' => ['(non-falsy-string&lowercase-string)|false', 'filename'=>'string', 'raw_output='=>'bool'],
'mdecrypt_generic' => ['string', 'td'=>'resource', 'data'=>'string'],
'Memcache::add' => ['bool', 'key'=>'string', 'var'=>'mixed', 'flag='=>'int', 'expire='=>'int'],
'Memcache::addServer' => ['bool', 'host'=>'string', 'port='=>'int', 'persistent='=>'bool', 'weight='=>'int', 'timeout='=>'int', 'retry_interval='=>'int', 'status='=>'bool', 'failure_callback='=>'callable', 'timeoutms='=>'int'],
Expand Down Expand Up @@ -10446,8 +10446,8 @@
'setRightFill' => ['void', 'red'=>'int', 'green'=>'int', 'blue'=>'int', 'a='=>'int'],
'setthreadtitle' => ['bool', 'title'=>'string'],
'settype' => ['bool', '&rw_var'=>'mixed', 'type'=>'string'],
'sha1' => ['non-falsy-string', 'str'=>'string', 'raw_output='=>'bool'],
'sha1_file' => ['non-falsy-string|false', 'filename'=>'string', 'raw_output='=>'bool'],
'sha1' => ['non-falsy-string&lowercase-string', 'str'=>'string', 'raw_output='=>'bool'],
'sha1_file' => ['(non-falsy-string&lowercase-string)|false', 'filename'=>'string', 'raw_output='=>'bool'],
'sha256' => ['string', 'str'=>'string', 'raw_output='=>'bool'],
'sha256_file' => ['string', 'filename'=>'string', 'raw_output='=>'bool'],
'shapefileObj::__construct' => ['void', 'filename'=>'string', 'type'=>'int'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1121,4 +1121,24 @@ public function testBug10493(): void
$this->analyse([__DIR__ . '/data/bug-10493.php'], []);
}

public function testHashing(): void
{
$this->checkAlwaysTrueStrictComparison = true;
$this->analyse([__DIR__ . '/data/hashing.php'], [
[
"Strict comparison using === between lowercase-string&non-falsy-string and 'ABC' will always evaluate to false.",
9,
],
[
"Strict comparison using === between (lowercase-string&non-falsy-string)|false and 'ABC' will always evaluate to false.",
12,
],
[
"Strict comparison using === between (lowercase-string&non-falsy-string)|(non-falsy-string&numeric-string) and 'A' will always evaluate to false.",
31,
'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%</>.',
],
]);
}

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/hashing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Hashing;

use function md5;
use function md5_file;

function doFoo(string $s):void {
if (md5($s) === 'ABC') {

}
if (md5_file($s) === 'ABC') {

}
}

/**
* @param (non-falsy-string&numeric-string)|(non-falsy-string&lowercase-string) $s
* @return void
*/
function doFooBar($s) {
if ($s === '598d4c200461b81522a3328565c25f7c') {

}
if ($s === 'a') {

}
if ($s === '123') {

}
if ($s === 'A') {

}
}

0 comments on commit 98e2b6e

Please sign in to comment.