Skip to content

Commit ecb6c07

Browse files
committed
Merge remote-tracking branch 'origin/2.2.x' into 2.3.x
# Conflicts: # src/Turbo/TurboExtensionEnabler.php
2 parents 8a83697 + 1e2d4e1 commit ecb6c07

22 files changed

Lines changed: 843 additions & 329 deletions

‎.github/workflows/e2e-tests.yml‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ jobs:
124124
mv src/Foo.php.orig src/Foo.php
125125
echo -n > phpstan-baseline.neon
126126
../../bin/phpstan -vvv
127+
- script: |
128+
cd e2e/result-cache-atomic-save
129+
../../bin/phpstan
130+
INODE=$(ls -i tmp/resultCache.php | awk '{print $1}')
131+
../../bin/phpstan
132+
# The cache is written next to its final path and renamed into place, so saving it
133+
# again replaces the file rather than truncating and rewriting the one the next run
134+
# reads. That is what keeps a run killed mid-save from leaving a partial cache behind.
135+
REPLACED=$([ "$INODE" != "$(ls -i tmp/resultCache.php | awk '{print $1}')" ] && echo replaced || echo 'written in place')
136+
../bashunit -a equals 'replaced' "$REPLACED"
137+
# A completed save leaves nothing next to the cache file.
138+
LEFTOVERS=$(ls tmp/ | grep -c '\.tmp$' || true)
139+
../bashunit -a equals '0' "$LEFTOVERS"
140+
OUTPUT=$(../../bin/phpstan -vvv 2>&1)
141+
echo "$OUTPUT"
142+
../bashunit -a contains 'Result cache restored. 0 files will be reanalysed.' "$OUTPUT"
127143
- script: |
128144
cd e2e/bug-14514
129145
composer install
@@ -788,6 +804,19 @@ jobs:
788804
/tmp/phpstan-install-1/bin/phpstan analyse -l 8 -vvv test.php
789805
mv /tmp/phpstan-install-1 /tmp/phpstan-install-2
790806
/tmp/phpstan-install-2/bin/phpstan analyse -l 8 -vvv test.php
807+
- script: |
808+
# PHPStan bundles symfony/polyfill-php85 for its own runtime, so array_first()
809+
# and other PHP 8.5 symbols are declared in the PHPStan process. A project
810+
# analysed with PHP 8.4 must not see them
811+
# (https://github.com/phpstan/phpstan/issues/15150).
812+
cd e2e/bug-15150
813+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse --error-format=raw")
814+
echo "$OUTPUT"
815+
../bashunit -a contains 'test.php:11:Function array_first not found.' "$OUTPUT"
816+
../bashunit -a contains 'test.php:12:Function array_last not found.' "$OUTPUT"
817+
../bashunit -a contains 'test.php:21:Instantiated class Filter\FilterException not found.' "$OUTPUT"
818+
# array_find() exists in the analysed PHP version, PhpStorm stubs describe it
819+
../bashunit -a not_contains 'array_find' "$OUTPUT"
791820
792821
steps:
793822
- name: Harden the runner (Audit all outbound calls)
@@ -1153,6 +1182,23 @@ jobs:
11531182
echo "$OUTPUT"
11541183
e2e/bashunit -a contains 'Turbo extension: enabled' "$OUTPUT"
11551184
1185+
- name: "Verify conditional types leave interned types equal to a resolved subject alone"
1186+
# With the extension active TypeCombinator's results are interned, so equal
1187+
# types share one instance. ConditionalType used to narrow the types in its
1188+
# branches identical to the subject even once the subject had resolved to a
1189+
# concrete type, and interning made that hit unrelated types - the `mixed`
1190+
# value type of an `array` branch for a `mixed` subject -
1191+
# https://github.com/phpstan/phpstan/issues/15151
1192+
run: |
1193+
cd e2e/bug-15151
1194+
OUTPUT=$(php ../../bin/phpstan analyse --no-progress --error-format=raw 2>&1 || true)
1195+
echo "$OUTPUT"
1196+
../bashunit -a contains 'reception_formulaire.php:20:Dumped type: mixed' "$OUTPUT"
1197+
../bashunit -a contains 'webservice.php:25:Dumped type: mixed' "$OUTPUT"
1198+
../bashunit -a not_contains 'Dumped type: array' "$OUTPUT"
1199+
# the two dumps are the only errors: count the file:line:message lines
1200+
../bashunit -a equals '2' "$(echo "$OUTPUT" | grep -Ec '^/.+:[0-9]+:')"
1201+
11561202
- name: "Test"
11571203
# The arena is a per-run POSIX shared-memory object: shm_open() plus an
11581204
# ftruncate() to 256 MB that tmpfs honours sparsely, committing pages on

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ e2e/bashunit
1717
/.phpbench
1818
/e2e/turbo-arena/tmp
1919
/e2e/fork-unsafe-extension/tmp
20+
/e2e/result-cache-atomic-save/tmp

‎e2e/bug-15150/phpstan.dist.neon‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 8
3+
phpVersion: 80400
4+
paths:
5+
- test.php

‎e2e/bug-15150/test.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15150;
4+
5+
/**
6+
* @param array<int, string> $a
7+
*/
8+
function doFoo(array $a): void
9+
{
10+
// PHP 8.5 functions, polyfilled by symfony/polyfill-php85 bundled with PHPStan
11+
echo array_first($a);
12+
echo array_last($a);
13+
14+
// PHP 8.4 function, available in the analysed PHP version
15+
echo array_find($a, static fn (string $v): bool => $v !== '') ?? '';
16+
}
17+
18+
function doBar(): void
19+
{
20+
// PHP 8.5 class, polyfilled by symfony/polyfill-php85 bundled with PHPStan
21+
throw new \Filter\FilterException('foo');
22+
}

‎e2e/bug-15151/functions.php‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace MyApp;
4+
5+
6+
/**
7+
* Note. The bug disappears if I remove this function or if I edit it in certain ways.
8+
*
9+
* @param ?int $quote_style
10+
* @param array{string, string}|bool|null ...$substitutions_list
11+
* @return ($string is null ? null : ($string is '' ? '' : ($string is non-empty-string ? non-empty-string : string)))
12+
*/
13+
function htmlentities2(string|int|float|\BackedEnum|null $string, ?int $quote_style = \ENT_QUOTES, array|bool|null ...$substitutions_list): string|null {
14+
if ($string instanceof \BackedEnum)
15+
$string = validate_string($string->value);
16+
if ($string === null)
17+
return null;
18+
$string = validate_string($string) ?? throw new \ValueError('Incompatible value');
19+
$sortie = \htmlspecialchars($string, ($quote_style ?? \ENT_QUOTES) | \ENT_HTML401 | \ENT_SUBSTITUTE, 'utf-8');
20+
foreach ($substitutions_list as $substitutions) {
21+
if ($substitutions) {
22+
if ($substitutions === true)
23+
$substitutions = [ [ "\n", "\r" ], [ '<br/>', '' ] ];
24+
$sortie = \str_replace($substitutions[0], $substitutions[1], $sortie);
25+
}
26+
}
27+
return $sortie;
28+
}
29+
30+
31+
32+
/**
33+
* @template T
34+
* @param T $val
35+
* @return (T is array ? T : null)
36+
*/
37+
function validate_array(mixed $val): ?array {
38+
return \is_array($val) ? $val : null;
39+
}
40+
41+
42+
/**
43+
* @template T of mixed
44+
* @param T $val
45+
* @return ($val is string ? T&string : ($val is int ? decimal-int-string : ($val is float ? ?numeric-string : null)))
46+
*/
47+
function validate_string(mixed $val): ?string {
48+
if (\is_string($val)) {
49+
return $val;
50+
}
51+
elseif (\is_int($val)) {
52+
return (string) $val;
53+
}
54+
elseif (\is_float($val)) {
55+
return (string) $val;
56+
}
57+
return null;
58+
}

‎e2e/bug-15151/phpstan.neon‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 8
3+
paths:
4+
- .
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace MyApp;
4+
5+
class reception_formulaire {
6+
7+
/** @var list<array{valeur: string, affichage: string}> */
8+
public array $liste_genre_pers = [ ];
9+
10+
11+
// @phpstan-ignore missingType.iterableValue (a phpstan error seems useful in order to trigger the bug)
12+
final function __construct(
13+
protected readonly array $params
14+
) { }
15+
16+
17+
public function foo(): void {
18+
$liste_genre = [ ];
19+
foreach (validate_array($this->params['Genre_Admis'] ?? null) ?? [ ] as $_) {
20+
\PHPStan\dumpType($_); // ### should be: mixed, sometimes incorrectly: array
21+
if (in_array($_, [ 'Cli', 'Emp', 'Fou', 'Pct', 'Pre', 'Self' ], true))
22+
$liste_genre[] = $_;
23+
}
24+
$liste_genre_pers = [ ];
25+
foreach ($liste_genre as $ce_genre) {
26+
$liste_genre_pers[] = [ 'valeur' => $ce_genre, 'affichage' => '['.$ce_genre.']' ];
27+
}
28+
$this->liste_genre_pers = $liste_genre_pers;
29+
30+
}
31+
32+
33+
}

‎e2e/bug-15151/webservice.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace MyApp;
4+
5+
class webservice {
6+
7+
/**
8+
* @param array{limit_defaut: positive-int, limit_max: positive-int} $opt
9+
* @return array{positive-int, non-negative-int, list<non-empty-string>}
10+
* @phpstan-ignore missingType.iterableValue (a phpstan error seems useful in order to trigger the bug)
11+
*/
12+
public static function get_limit_offset_sort(array $par, array $opt): array {
13+
$limit = isset($par['limit']) ? (int) $par['limit'] : $opt['limit_defaut'];
14+
if ($limit < 1)
15+
$limit = 1;
16+
if ($limit > $opt['limit_max'])
17+
$limit = $opt['limit_max'];
18+
19+
$offset = isset($par['offset']) ? (int) $par['offset'] : 0;
20+
if ($offset < 0)
21+
$offset = 0;
22+
23+
$sort = [ ];
24+
foreach (validate_array($par['sort'] ?? null) ?? [ ] as $_) {
25+
\PHPStan\dumpType($_); // ### should be: mixed, sometimes incorrectly: array
26+
$_ = validate_string($_);
27+
if ($_ !== null && $_ !== '')
28+
$sort[] = $_;
29+
}
30+
31+
return [ $limit, $offset, $sort ];
32+
33+
}
34+
35+
36+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 8
3+
tmpDir: tmp
4+
paths:
5+
- src
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2EAtomicSave;
4+
5+
class Foo
6+
{
7+
8+
public function doFoo(): int
9+
{
10+
return 1;
11+
}
12+
13+
}

0 commit comments

Comments
 (0)