Skip to content

Commit

Permalink
Merge branch '1.8.x' into ctype-digit-extension
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffycondor committed Aug 23, 2022
2 parents 4f3beab + 32353a9 commit 96182fd
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 13 deletions.
7 changes: 7 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3143,6 +3143,13 @@ public function enterForeachKey(Expr $iteratee, string $keyName): self
$scope = $this->assignVariable($keyName, $iterateeType->getIterableKeyType());
$scope->nativeExpressionTypes[sprintf('$%s', $keyName)] = $nativeIterateeType->getIterableKeyType();

if ($iterateeType->isArray()->yes()) {
$scope = $scope->specifyExpressionType(
new Expr\ArrayDimFetch($iteratee, new Variable($keyName)),
$iterateeType->getIterableValueType(),
);
}

return $scope;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ public function getType(Expr $expr, InitializerExprContext $context): Type
}
if ($expr instanceof File) {
$file = $context->getFile();
return $file !== null ? new ConstantStringType($file) : new StringType();
return $file !== null ? (new ConstantStringType($file))->generalize(GeneralizePrecision::moreSpecific()) : new StringType();
}
if ($expr instanceof Dir) {
$file = $context->getFile();
return $file !== null ? new ConstantStringType(dirname($file)) : new StringType();
return $file !== null ? (new ConstantStringType(dirname($file)))->generalize(GeneralizePrecision::moreSpecific()) : new StringType();
}
if ($expr instanceof Line) {
return new ConstantIntegerType($expr->getLine());
Expand Down
4 changes: 4 additions & 0 deletions src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection;
use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Generic\TemplateMixedType;
use PHPStan\Type\Generic\TemplateType;
Expand Down Expand Up @@ -120,6 +121,9 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni

public function unsetOffset(Type $offsetType): Type
{
if ($this->subtractedType !== null) {
return new self($this->isExplicitMixed, TypeCombinator::remove($this->subtractedType, new ConstantArrayType([], [])));
}
return $this;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2308,11 +2308,11 @@ public function dataBinaryOperations(): array
'$line',
],
[
(new ConstantStringType(__DIR__ . '/data'))->describe(VerbosityLevel::precise()),
'literal-string&non-falsy-string',
'$dir',
],
[
(new ConstantStringType(__DIR__ . '/data/binary.php'))->describe(VerbosityLevel::precise()),
'literal-string&non-falsy-string',
'$file',
],
[
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/ctype-digit.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7788.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7809.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/composer-non-empty-array-after-unset.php');
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace ComposerNonEmptyArrayAfterUnset;

use function PHPStan\Testing\assertType;

class Foo
{

/** @var mixed[] */
private $config;

public function doFoo()
{
if (!empty($this->config['authors'])) {
assertType("mixed~0|0.0|''|'0'|array{}|false|null", $this->config['authors']);
foreach ($this->config['authors'] as $key => $author) {
assertType("mixed~0|0.0|''|'0'|false|null", $this->config['authors']);
if (!is_array($author)) {
unset($this->config['authors'][$key]);
assertType("mixed~0|0.0|''|'0'|false|null", $this->config['authors']);
continue;
}
foreach (['homepage', 'email', 'name', 'role'] as $authorData) {
if (isset($author[$authorData]) && !is_string($author[$authorData])) {
unset($this->config['authors'][$key][$authorData]);
}
}
if (isset($author['homepage'])) {
unset($this->config['authors'][$key]['homepage']);
}
if (isset($author['email']) && !filter_var($author['email'], FILTER_VALIDATE_EMAIL)) {
unset($this->config['authors'][$key]['email']);
}
if (empty($this->config['authors'][$key])) {
assertType("mixed~0|0.0|''|'0'|false|null", $this->config['authors']);
unset($this->config['authors'][$key]);
assertType("mixed~0|0.0|''|'0'|false|null", $this->config['authors']);
}
assertType("mixed~0|0.0|''|'0'|false|null", $this->config['authors']);
}
assertType("mixed~0|0.0|''|'0'|false|null", $this->config['authors']);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Type\VerbosityLevel;
use SingleFileSourceLocatorTestClass;
use TestSingleFileSourceLocator\AFoo;
use function str_replace;
use const PHP_VERSION_ID;

class OptimizedSingleFileSourceLocatorTest extends PHPStanTestCase
Expand Down Expand Up @@ -119,7 +118,7 @@ public function dataConst(): array
],
[
'const_with_dir_const',
"'" . str_replace('\\', '/', __DIR__ . '/data') . "'",
'literal-string&non-falsy-string',
],
[
'OPTIMIZED_SFSL_OBJECT_CONSTANT',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,7 @@ public function testBug7142(): void
public function testBug6000(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-6000.php'], [
[
'Offset \'classmap\' does not exist on array{psr-4?: array<string, array<string>|string>, classmap?: array<int, string>}.',
12,
],
]);
$this->analyse([__DIR__ . '/data/bug-6000.php'], []);
}

public function testBug5743(): void
Expand Down Expand Up @@ -508,4 +503,9 @@ public function testBug7763(): void
$this->analyse([__DIR__ . '/data/bug-7763.php'], []);
}

public function testSpecifyExistentOffsetWhenEnteringForeach(): void
{
$this->analyse([__DIR__ . '/data/specify-existent-offset-when-entering-foreach.php'], []);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace SpecifyExistentOffsetWhenEnteringForeach;

class Foo
{

public function doFoo(string $s): void
{
$hintsToFind = ['ext-' => 0, 'lib-' => 0, 'php' => 99, 'composer' => 99];
foreach ($hintsToFind as $hintPrefix => $hintCount) {
if (str_starts_with($s, $hintPrefix)) {
if ($hintCount === 0 || $hintCount >= 99) {
$hintsToFind[$hintPrefix]++;
} elseif ($hintCount === 1) {
unset($hintsToFind[$hintPrefix]);
}
}
}
}

}
8 changes: 7 additions & 1 deletion tests/PHPStan/Rules/Functions/ImplodeFunctionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ public function testFile(): void

public function testBug6000(): void
{
$this->analyse([__DIR__ . '/../Arrays/data/bug-6000.php'], []);
$this->analyse([__DIR__ . '/../Arrays/data/bug-6000.php'], [
[
// shouldn't be happening...
'Parameter #2 $array of function implode expects array<string>, array<int|string, array<string>|string> given.',
12,
],
]);
}

}

0 comments on commit 96182fd

Please sign in to comment.