Skip to content

Commit 8aa4092

Browse files
committed
Fix self-analysis errors surfaced by the foreach non-empty narrowing
The unconditional non-empty narrowing makes phpstan-src's own foreach bodies provably entered, which surfaces six latent issues under phpstan-strict-rules: - foreach.valueOverwrite: three inner loops whose value variable now shadows a definitely-defined outer variable are renamed ($internalError -> $error, $file -> $sourceFile, $i -> $j). - offsetAccess.nonArray: three [$k, $v] = $this->unsealed destructurings on a list<Type>|null property gain an explicit null guard throwing ShouldNotHappenException, matching the file's existing impossible-state style.
1 parent 7e707ff commit 8aa4092

4 files changed

Lines changed: 19 additions & 10 deletions

File tree

‎src/Command/AnalyseCommand.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
477477
if ($generateBaselineFile !== null) {
478478
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput(), $analysisResult->getProcessedFiles());
479479
if (count($internalErrorsTuples) > 0) {
480-
foreach ($internalErrorsTuples as [$internalError]) {
481-
$inceptionResult->getStdOutput()->writeLineFormatted($internalError->getMessage());
480+
foreach ($internalErrorsTuples as [$error]) {
481+
$inceptionResult->getStdOutput()->writeLineFormatted($error->getMessage());
482482
$inceptionResult->getStdOutput()->writeLineFormatted('');
483483
}
484484

‎src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,16 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
134134
if ($identifier->isClass()) {
135135
$fetchedClassNode = null;
136136
$fetchedFile = null;
137-
foreach ($files as $file) {
138-
$fetchedClassNodes = $this->fileNodesFetcher->fetchNodes($file)->getClassNodes();
137+
foreach ($files as $sourceFile) {
138+
$fetchedClassNodes = $this->fileNodesFetcher->fetchNodes($sourceFile)->getClassNodes();
139139

140140
if (!array_key_exists($identifierName, $fetchedClassNodes)) {
141141
return null;
142142
}
143143

144144
/** @var FetchedNode<Node\Stmt\ClassLike> $fetchedClassNode */
145145
$fetchedClassNode = current($fetchedClassNodes[$identifierName]);
146-
$fetchedFile = $file;
146+
$fetchedFile = $sourceFile;
147147
}
148148

149149
[$reflectionCacheKey, $variableCacheKey] = $this->getCacheKeys($fetchedFile, $identifier);

‎src/Type/Constant/ConstantArrayType.php‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,11 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
811811
continue;
812812
}
813813

814-
[$thisUnsealedKey, $thisUnsealedValue] = $this->unsealed;
814+
$thisUnsealed = $this->unsealed;
815+
if ($thisUnsealed === null) {
816+
throw new ShouldNotHappenException();
817+
}
818+
[$thisUnsealedKey, $thisUnsealedValue] = $thisUnsealed;
815819
$keyCheck = $thisUnsealedKey->isSuperTypeOf($typeKey);
816820
if ($keyCheck->no()) {
817821
if ($type->isOptionalKey($i)) {
@@ -835,8 +839,13 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
835839
if ($thisUnsealedness->no()) {
836840
$results[] = IsSuperTypeOfResult::createMaybe();
837841
} else {
838-
[$thisUnsealedKey, $thisUnsealedValue] = $this->unsealed;
839-
[$typeUnsealedKey, $typeUnsealedValue] = $type->unsealed;
842+
$thisUnsealed = $this->unsealed;
843+
$typeUnsealed = $type->unsealed;
844+
if ($thisUnsealed === null || $typeUnsealed === null) {
845+
throw new ShouldNotHappenException();
846+
}
847+
[$thisUnsealedKey, $thisUnsealedValue] = $thisUnsealed;
848+
[$typeUnsealedKey, $typeUnsealedValue] = $typeUnsealed;
840849
$results[] = $thisUnsealedKey->isSuperTypeOf($typeUnsealedKey);
841850
$results[] = $thisUnsealedValue->isSuperTypeOf($typeUnsealedValue);
842851
}

‎src/Type/UnionType.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult
239239
}
240240
if ($commonReasons !== null && count($commonReasons) > 0) {
241241
$decorated = [];
242-
foreach (array_keys($innerAccepts) as $i) {
242+
foreach (array_keys($innerAccepts) as $j) {
243243
foreach ($commonReasons as $reason) {
244-
$decorated[] = sprintf('Type #%d from the union: %s', $i + 1, $reason);
244+
$decorated[] = sprintf('Type #%d from the union: %s', $j + 1, $reason);
245245
}
246246
}
247247
$result = new AcceptsResult($result->result, $decorated);

0 commit comments

Comments
 (0)