Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1383,11 +1383,14 @@ private function specifyTypesForCountFuncCall(
if ($context->falsey() && $isConstantArray->yes()) {
$remainingSize = TypeCombinator::remove($type->getArraySize(), $sizeType);
if (!$remainingSize instanceof NeverType) {
$negatedContext = $context->false()
? TypeSpecifierContext::createTrue()
: TypeSpecifierContext::createTruthy();
$result = $this->specifyTypesForCountFuncCall(
$countFuncCall,
$type,
$remainingSize,
$context->negate(),
$negatedContext,
$scope,
$rootExpr,
);
Expand Down
44 changes: 44 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14605.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Bug14605;

use function PHPStan\Testing\assertType;

function testPregMatchOptionalGroupWithCountAndSubstr(string $resource): void {
if (preg_match("!^3(/.*)?$!", $resource, $m)) {
assertType('array{0: non-falsy-string, 1?: non-falsy-string}', $m);
$resource = count($m) > 1 ? substr($m[1], 1) : '';
assertType('string', $resource);
}
}

/** @param array{0: string, 1?: string} $m */
function testPhpDocArrayWithCountAndSubstr(array $m): void {
$x = count($m) > 1 ? substr($m[1], 1) : '';
assertType('string', $x);
}

/** @param array{0: string, 1?: string} $m */
function testPhpDocArrayWithCountEqualsAndSubstr(array $m): void {
$x = count($m) === 2 ? substr($m[1], 1) : '';
assertType('string', $x);
}

/** @param array{0: string, 1?: string} $m */
function testCountNarrowingInIfStatement(array $m): void {
if (count($m) > 1) {
$x = substr($m[1], 1);
assertType('string', $x);
}
}

/** @param array{0: string, 1?: string} $m */
function testReturnTernary(array $m): string {
return count($m) > 1 ? substr($m[1], 1) : '';
}

/** @param array{0: string, 1?: string} $m */
function testCountTernaryWithFalsyBranches(array $m): void {
$x = count($m) > 1 ? false : '';
assertType("''|false", $x);
}
Loading