Skip to content

Commit 4c0e9a0

Browse files
committed
Merge remote-tracking branch 'origin/2.2.x' into 2.3.x
2 parents bdb7a9e + d27106a commit 4c0e9a0

5 files changed

Lines changed: 93 additions & 7 deletions

File tree

‎src/Analyser/ArgumentsHandler.php‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -791,11 +791,19 @@ public function processArgs(
791791
}
792792

793793
// what the call writes back is described by PHPDoc (@param, @param-out,
794-
// a parameter-out extension) - natively only the parameter's own
795-
// type declaration is guaranteed
796-
$byRefNativeType = $currentParameter instanceof ExtendedParameterReflection
797-
? $currentParameter->getNativeType()
798-
: $byRefType;
794+
// a parameter-out extension) - natively only the parameter's own type
795+
// can describe it: its declaration, or for a builtin its signature map
796+
// entry. PHP checks a declaration only on the way in and a signature
797+
// map entry not at all, so it holds only when the written-back type
798+
// fits inside it. preg_match() with PREG_OFFSET_CAPTURE writes arrays
799+
// into a slot the signature map declares as string[].
800+
$byRefNativeType = $byRefType;
801+
if ($currentParameter instanceof ExtendedParameterReflection) {
802+
$byRefNativeType = $currentParameter->getNativeType();
803+
if (!$byRefNativeType->isSuperTypeOf($byRefType)->yes()) {
804+
$byRefNativeType = new MixedType();
805+
}
806+
}
799807

800808
$scope = $this->assignHandler->processVirtualAssign(
801809
$nodeScopeResolver,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ByRefWritebackNativeType;
4+
5+
use function PHPStan\Testing\assertNativeType;
6+
use function PHPStan\Testing\assertType;
7+
8+
function offsetCapture(string $s): void
9+
{
10+
if (! preg_match('/(a)(b)?/', $s, $m, PREG_OFFSET_CAPTURE)) {
11+
return;
12+
}
13+
14+
assertType("array{0: array{non-falsy-string, int<-1, max>}, 1: array{'a', int<-1, max>}, 2?: array{'b', int<-1, max>}}", $m);
15+
assertNativeType("array{0: array{non-falsy-string, int<-1, max>}, 1: array{'a', int<-1, max>}, 2?: array{'b', int<-1, max>}}", $m);
16+
}
17+
18+
function noFlags(string $s): void
19+
{
20+
if (! preg_match('/(a)(b)?/', $s, $m)) {
21+
return;
22+
}
23+
24+
assertType("array{0: non-falsy-string, 1: 'a', 2?: 'b'}", $m);
25+
assertNativeType("array{0: non-falsy-string, 1: 'a', 2?: 'b'}", $m);
26+
}
27+
28+
/** @param-out string $v */
29+
function paramOutContradictsDeclaration(int &$v): void
30+
{
31+
$v = 'now a string';
32+
}
33+
34+
function userlandParamOut(): void
35+
{
36+
$x = 1;
37+
paramOutContradictsDeclaration($x);
38+
39+
assertType('string', $x);
40+
assertNativeType('mixed', $x);
41+
}

‎tests/PHPStan/Rules/Variables/IssetRuleTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,4 +682,11 @@ public function testBug14421(): void
682682
$this->analyse([__DIR__ . '/data/bug-14421.php'], []);
683683
}
684684

685+
public function testPregMatchOffsetCaptureWithoutTreatPhpDocTypesAsCertain(): void
686+
{
687+
$this->treatPhpDocTypesAsCertain = false;
688+
689+
$this->analyse([__DIR__ . '/data/preg-match-offset-capture-isset.php'], []);
690+
}
691+
685692
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PregMatchOffsetCaptureIsset;
4+
5+
function offsetCapture(string $s): ?string
6+
{
7+
if (! preg_match('/(a)(b)?/', $s, $m, PREG_OFFSET_CAPTURE)) {
8+
return null;
9+
}
10+
11+
return isset($m[2]) ? $m[2][0] : null;
12+
}

‎turbo-ext/src/ArgumentsHandler.cpp‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,12 +2540,30 @@ class ArgumentsHandler
25402540
}
25412541

25422542
// what the call writes back is described by PHPDoc (@param, @param-out,
2543-
// a parameter-out extension) - natively only the parameter's own
2544-
// type declaration is guaranteed
2543+
// a parameter-out extension) - natively only the parameter's own type
2544+
// can describe it: its declaration, or for a builtin its signature map
2545+
// entry. PHP checks a declaration only on the way in and a signature
2546+
// map entry not at all, so it holds only when the written-back type
2547+
// fits inside it. preg_match() with PREG_OFFSET_CAPTURE writes arrays
2548+
// into a slot the signature map declares as string[].
25452549
bool currentIsExtended = false;
25462550
if (UNEXPECTED(!isA(currentParameter, PT_CLASS_EXTENDED_PARAMETER_REFLECTION, currentIsExtended))) return false;
25472551
zv::Val byRefNativeType = currentIsExtended ? parameterGetNativeType(currentParameter) : zv::Val::copyOf(byRefType.ref());
25482552
if (UNEXPECTED(byRefNativeType.isUndef())) return false;
2553+
if (currentIsExtended) {
2554+
if (UNEXPECTED(Z_TYPE_P(byRefNativeType.raw()) != IS_OBJECT)) {
2555+
zend_throw_error(NULL, "Call to a member function isSuperTypeOf() on %s", zend_zval_value_name(byRefNativeType.raw()));
2556+
return false;
2557+
}
2558+
zv::Val fits = pt_type_op(Z_OBJ_P(byRefNativeType.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, byRefType.raw());
2559+
if (UNEXPECTED(fits.isUndef())) return false;
2560+
zend_long fitsValue = pt_type_result_trinary(fits.raw());
2561+
if (UNEXPECTED(fitsValue < 0)) return false;
2562+
if (fitsValue != PT_TRI_YES) {
2563+
byRefNativeType = pt_type_new_mixed_type();
2564+
if (UNEXPECTED(byRefNativeType.isUndef())) return false;
2565+
}
2566+
}
25492567

25502568
zv::Val typeExpr = newNativeTypeExpr(byRefType.raw(), byRefNativeType.raw());
25512569
if (UNEXPECTED(typeExpr.isUndef())) return false;

0 commit comments

Comments
 (0)