Skip to content

Commit a2c0946

Browse files
VincentLangletondrejmirtes
authored andcommitted
Consider comparison as strict when type is the same
1 parent 7c0c857 commit a2c0946

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

Diff for: src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
6060

6161
$isStrictComparison = $isStrictComparison
6262
|| $needleType->isEnum()->yes()
63-
|| $arrayValueType->isEnum()->yes();
63+
|| $arrayValueType->isEnum()->yes()
64+
|| ($needleType->isString()->yes() && $arrayValueType->isString()->yes())
65+
|| ($needleType->isInteger()->yes() && $arrayValueType->isInteger()->yes())
66+
|| ($needleType->isFloat()->yes() && $arrayValueType->isFloat()->yes())
67+
|| ($needleType->isBoolean()->yes() && $arrayValueType->isBoolean()->yes());
6468

6569
if ($arrayExpr instanceof Array_) {
6670
$types = null;

Diff for: tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -3442,4 +3442,14 @@ public function testBug6828(): void
34423442
$this->analyse([__DIR__ . '/data/bug-6828.php'], []);
34433443
}
34443444

3445+
public function testBug12884(): void
3446+
{
3447+
$this->checkThisOnly = false;
3448+
$this->checkNullables = true;
3449+
$this->checkUnionTypes = true;
3450+
$this->checkExplicitMixed = true;
3451+
3452+
$this->analyse([__DIR__ . '/data/bug-12884.php'], []);
3453+
}
3454+
34453455
}

Diff for: tests/PHPStan/Rules/Methods/data/bug-12884.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12884;
4+
5+
class LogLevel
6+
{
7+
const EMERGENCY = 'emergency';
8+
const ALERT = 'alert';
9+
const CRITICAL = 'critical';
10+
const ERROR = 'error';
11+
const WARNING = 'warning';
12+
const NOTICE = 'notice';
13+
const INFO = 'info';
14+
const DEBUG = 'debug';
15+
}
16+
17+
class HelloWorld
18+
{
19+
/** @param list<LogLevel::*> $levels */
20+
public function __construct(
21+
private LoggerInterface $logger,
22+
public array $levels = []
23+
) {}
24+
25+
public function log(string $level, string $message): void
26+
{
27+
if (!in_array($level, $this->levels, true)) {
28+
$level = LogLevel::INFO;
29+
}
30+
$this->logger->log($level, $message);
31+
}
32+
}
33+
34+
interface LoggerInterface
35+
{
36+
/**
37+
* @param 'emergency'|'alert'|'critical'|'error'|'warning'|'notice'|'info'|'debug' $level
38+
*/
39+
public function log($level, string|\Stringable $message): void;
40+
}

0 commit comments

Comments
 (0)