Skip to content

Commit 48fad99

Browse files
committed
coerce arguments also in non-strict type mode
1 parent 3d327a8 commit 48fad99

File tree

2 files changed

+147
-14
lines changed

2 files changed

+147
-14
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5641,18 +5641,12 @@ static function (): void {
56415641

56425642
if ($assignedTypeIsCompatible) {
56435643
$scope = $scope->assignExpression($var, $assignedExprType, $assignedNativeType);
5644-
} elseif ($scope->isDeclareStrictTypes()) {
5644+
} else {
56455645
$scope = $scope->assignExpression(
56465646
$var,
56475647
TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType),
56485648
TypeCombinator::intersect($assignedNativeType->toCoercedArgumentType(true), $propertyNativeType),
56495649
);
5650-
} else {
5651-
$scope = $scope->assignExpression(
5652-
$var,
5653-
TypeCombinator::intersect($assignedExprType, $propertyNativeType),
5654-
TypeCombinator::intersect($assignedNativeType, $propertyNativeType),
5655-
);
56565650
}
56575651
} else {
56585652
$scope = $scope->assignExpression($var, $assignedExprType, $assignedNativeType);
@@ -5735,18 +5729,12 @@ static function (): void {
57355729

57365730
if ($assignedTypeIsCompatible) {
57375731
$scope = $scope->assignExpression($var, $assignedExprType, $assignedNativeType);
5738-
} elseif ($scope->isDeclareStrictTypes()) {
5732+
} else {
57395733
$scope = $scope->assignExpression(
57405734
$var,
57415735
TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType),
57425736
TypeCombinator::intersect($assignedNativeType->toCoercedArgumentType(true), $propertyNativeType),
57435737
);
5744-
} else {
5745-
$scope = $scope->assignExpression(
5746-
$var,
5747-
TypeCombinator::intersect($assignedExprType, $propertyNativeType),
5748-
TypeCombinator::intersect($assignedNativeType, $propertyNativeType),
5749-
);
57505738
}
57515739
} else {
57525740
$scope = $scope->assignExpression($var, $assignedExprType, $assignedNativeType);
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php declare(strict_types = 0);
2+
3+
namespace Bug12393b;
4+
5+
use function PHPStan\Testing\assertNativeType;
6+
use function PHPStan\Testing\assertType;
7+
8+
class HelloWorld
9+
{
10+
private string $name;
11+
12+
/** @var string */
13+
private $untypedName;
14+
15+
private float $float;
16+
17+
/** @var float */
18+
private $untypedFloat;
19+
20+
private array $a;
21+
22+
/**
23+
* @param mixed[] $plugin
24+
*/
25+
public function __construct(array $plugin){
26+
$this->name = $plugin["name"];
27+
assertType('string', $this->name);
28+
}
29+
30+
/**
31+
* @param mixed[] $plugin
32+
*/
33+
public function doFoo(array $plugin){
34+
$this->untypedName = $plugin["name"];
35+
assertType('mixed', $this->untypedName);
36+
}
37+
38+
public function doBar(int $i){
39+
$this->float = $i;
40+
assertType('float', $this->float);
41+
}
42+
43+
public function doBaz(int $i){
44+
$this->untypedFloat = $i;
45+
assertType('int', $this->untypedFloat);
46+
}
47+
48+
public function doLorem(): void
49+
{
50+
$this->a = ['a' => 1];
51+
assertType('array{a: 1}', $this->a);
52+
}
53+
54+
public function doFloatTricky(){
55+
$this->float = 1;
56+
assertType('1.0', $this->float);
57+
}
58+
}
59+
60+
class HelloWorldStatic
61+
{
62+
private static string $name;
63+
64+
/** @var string */
65+
private static $untypedName;
66+
67+
private static float $float;
68+
69+
/** @var float */
70+
private static $untypedFloat;
71+
72+
private static array $a;
73+
74+
/**
75+
* @param mixed[] $plugin
76+
*/
77+
public function __construct(array $plugin){
78+
self::$name = $plugin["name"];
79+
assertType('string', self::$name);
80+
}
81+
82+
/**
83+
* @param mixed[] $plugin
84+
*/
85+
public function doFoo(array $plugin){
86+
self::$untypedName = $plugin["name"];
87+
assertType('mixed', self::$untypedName);
88+
}
89+
90+
public function doBar(int $i){
91+
self::$float = $i;
92+
assertType('float', self::$float);
93+
}
94+
95+
public function doBaz(int $i){
96+
self::$untypedFloat = $i;
97+
assertType('int', self::$untypedFloat);
98+
}
99+
100+
public function doLorem(): void
101+
{
102+
self::$a = ['a' => 1];
103+
assertType('array{a: 1}', self::$a);
104+
}
105+
}
106+
107+
class EntryPointLookup
108+
{
109+
110+
/** @var array<string, mixed>|null */
111+
private ?array $entriesData = null;
112+
113+
/**
114+
* @return array<string, mixed>
115+
*/
116+
public function doFoo(): void
117+
{
118+
if ($this->entriesData !== null) {
119+
return;
120+
}
121+
122+
assertType('null', $this->entriesData);
123+
assertNativeType('null', $this->entriesData);
124+
125+
$data = $this->getMixed();
126+
if ($data !== null) {
127+
$this->entriesData = $data;
128+
assertType('array', $this->entriesData);
129+
assertNativeType('array', $this->entriesData);
130+
return;
131+
}
132+
133+
assertType('null', $this->entriesData);
134+
assertNativeType('null', $this->entriesData);
135+
}
136+
137+
/**
138+
* @return mixed
139+
*/
140+
public function getMixed()
141+
{
142+
143+
}
144+
145+
}

0 commit comments

Comments
 (0)