Skip to content

Commit 86e7f3f

Browse files
WIP
1 parent b5379a5 commit 86e7f3f

File tree

2 files changed

+99
-10
lines changed

2 files changed

+99
-10
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -743,16 +743,7 @@ public function unsetOffset(Type $offsetType): Type
743743

744744
$newIsList = TrinaryLogic::createNo();
745745
if (!$this->isList->no() && in_array($i, $this->optionalKeys, true)) {
746-
$preserveIsList = true;
747-
foreach ($newKeyTypes as $k2 => $newKeyType2) {
748-
if (!$newKeyType2 instanceof ConstantIntegerType || $newKeyType2->getValue() !== $k2) {
749-
$preserveIsList = false;
750-
break;
751-
}
752-
}
753-
if ($preserveIsList) {
754-
$newIsList = $this->isList;
755-
}
746+
$newIsList = TrinaryLogic::createMaybe();
756747
}
757748

758749
return new self($newKeyTypes, $newValueTypes, $this->nextAutoIndexes, $newOptionalKeys, $newIsList);

tests/PHPStan/Analyser/nsrt/bug-14177.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,102 @@ public function testList(array $b): void
1818
}
1919
assertType('list{0: string, 1: string, 2?: string, 3?: string}', $b);
2020
}
21+
22+
/**
23+
* @param list{0: string, 1: string, 2?: string, 3?: string} $b
24+
*/
25+
public function testUnset0(array $b): void
26+
{
27+
assertType('true', array_is_list($b));
28+
unset($b[0]);
29+
assertType('false', array_is_list($b));
30+
$b[] = 'foo';
31+
assertType('false', array_is_list($b));
32+
}
33+
34+
/**
35+
* @param list{0: string, 1: string, 2?: string, 3?: string} $b
36+
*/
37+
public function testUnset1(array $b): void
38+
{
39+
assertType('true', array_is_list($b));
40+
unset($b[1]);
41+
assertType('bool', array_is_list($b));
42+
$b[] = 'foo';
43+
assertType('false', array_is_list($b));
44+
}
45+
46+
/**
47+
* @param list{0: string, 1: string, 2?: string, 3?: string} $b
48+
*/
49+
public function testUnset2(array $b): void
50+
{
51+
assertType('true', array_is_list($b));
52+
unset($b[2]);
53+
assertType('bool', array_is_list($b));
54+
$b[] = 'foo';
55+
assertType('false', array_is_list($b));
56+
}
57+
58+
/**
59+
* @param list{0: string, 1: string, 2?: string, 3?: string} $b
60+
*/
61+
public function testUnset3(array $b): void
62+
{
63+
assertType('true', array_is_list($b));
64+
unset($b[3]);
65+
assertType('true', array_is_list($b));
66+
$b[] = 'foo';
67+
assertType('false', array_is_list($b));
68+
}
69+
70+
public function placeholderToEditor(string $html): void
71+
{
72+
$result = preg_replace_callback(
73+
'~\[image\\sid="(\\d+)"(?:\\shref="([^"]*)")?(?:\\sclass="([^"]*)")?\]~',
74+
function (array $matches): string {
75+
$id = (int) $matches[1];
76+
77+
assertType('list{0: non-falsy-string, 1: numeric-string, 2?: string, 3?: string}', $matches);
78+
79+
$replacement = sprintf(
80+
'<img src="%s"%s/>',
81+
$id,
82+
array_key_exists(3, $matches) ? sprintf(' class="%s"', $matches[3]) : '',
83+
);
84+
85+
assertType('list{0: non-falsy-string, 1: numeric-string, 2?: string, 3?: string}', $matches);
86+
87+
return array_key_exists(2, $matches) && $matches[2] !== ''
88+
? sprintf('<a href="%s">%s</a>', $matches[2], $replacement)
89+
: $replacement;
90+
},
91+
$html,
92+
);
93+
}
94+
95+
public function placeholderToEditor2(string $html): void
96+
{
97+
$result = preg_replace_callback(
98+
'~\[image\\sid="(\\d+)?"(?:\\shref="([^"]*)")?(?:\\sclass="([^"]*)")?\]~',
99+
function (array $matches): string {
100+
$id = (int) $matches[0];
101+
102+
assertType('list{0: non-falsy-string, 1?: \'\'|numeric-string, 2?: string, 3?: string}', $matches);
103+
104+
$replacement = sprintf(
105+
'<img src="%s"%s/>',
106+
$id,
107+
array_key_exists(2, $matches) ? sprintf(' class="%s"', $matches[2]) : '',
108+
);
109+
110+
assertType('list{0: non-falsy-string, 1?: \'\'|numeric-string, 2?: string, 3?: string}', $matches);
111+
112+
return array_key_exists(1, $matches) && $matches[1] !== ''
113+
? sprintf('<a href="%s">%s</a>', $matches[1], $replacement)
114+
: $replacement;
115+
},
116+
$html,
117+
);
118+
}
21119
}

0 commit comments

Comments
 (0)