Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.12.x' into 2.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 17, 2024
2 parents 65ddbcb + 09f7c00 commit c684505
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2932,7 +2932,7 @@
'ffmpeg_movie::hasAudio' => ['bool'],
'ffmpeg_movie::hasVideo' => ['bool'],
'fgetc' => ['string|false', 'fp'=>'resource'],
'fgetcsv' => ['list<string>|array{0: null}|false|null', 'fp'=>'resource', 'length='=>'0|positive-int', 'delimiter='=>'string', 'enclosure='=>'string', 'escape='=>'string'],
'fgetcsv' => ['list<string>|array{0: null}|false|null', 'fp'=>'resource', 'length='=>'0|positive-int|null', 'delimiter='=>'string', 'enclosure='=>'string', 'escape='=>'string'],
'fgets' => ['string|false', 'fp'=>'resource', 'length='=>'0|positive-int'],
'fgetss' => ['string|false', 'fp'=>'resource', 'length='=>'0|positive-int', 'allowable_tags='=>'string'],
'file' => ['list<string>|false', 'filename'=>'string', 'flags='=>'int-mask<FILE_USE_INCLUDE_PATH|FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES|FILE_NO_DEFAULT_CONTEXT>', 'context='=>'resource'],
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ public function getConstant(string $name): ClassConstantReflection
$deprecatedDescription = $resolvedPhpDoc->getDeprecatedTag() !== null ? $resolvedPhpDoc->getDeprecatedTag()->getMessage() : null;
$isDeprecated = $resolvedPhpDoc->isDeprecated();
$isInternal = $resolvedPhpDoc->isInternal();
$isFinal = $resolvedPhpDoc->isFinal();
$varTags = $resolvedPhpDoc->getVarTags();
if (isset($varTags[0]) && count($varTags) === 1) {
$phpDocType = $varTags[0]->getType();
Expand All @@ -1091,6 +1092,7 @@ public function getConstant(string $name): ClassConstantReflection
$deprecatedDescription,
$isDeprecated,
$isInternal,
$isFinal,
);
}
return $this->constants[$name];
Expand Down
1 change: 1 addition & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,7 @@ function (Type $type, callable $traverse): Type {
$constantReflection = $constantClassReflection->getConstant($constantName);
if (
!$constantClassReflection->isFinal()
&& !$constantReflection->isFinal()
&& !$constantReflection->hasPhpDocType()
&& !$constantReflection->hasNativeType()
) {
Expand Down
3 changes: 2 additions & 1 deletion src/Reflection/RealClassClassConstantReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
private ?string $deprecatedDescription,
private bool $isDeprecated,
private bool $isInternal,
private bool $isFinal,
)
{
}
Expand Down Expand Up @@ -105,7 +106,7 @@ public function isPublic(): bool

public function isFinal(): bool
{
return $this->reflection->isFinal();
return $this->isFinal || $this->reflection->isFinal();
}

public function isDeprecated(): TrinaryLogic
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\TemplateMixedType;
use PHPStan\Type\Generic\TemplateStrictMixedType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Traits\ArrayTypeTrait;
Expand Down Expand Up @@ -51,6 +52,10 @@ public function __construct(Type $keyType, private Type $itemType)
if ($keyType->describe(VerbosityLevel::value()) === '(int|string)') {
$keyType = new MixedType();
}
if ($keyType instanceof StrictMixedType && !$keyType instanceof TemplateStrictMixedType) {
$keyType = new UnionType([new StringType(), new IntegerType()]);
}

$this->keyType = $keyType;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/nsrt/class-constant-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Foo
/** @var string */
private const PRIVATE_TYPE = 'foo';

/** @final */
const FINAL_TYPE = 'zoo';

public function doFoo()
{
assertType('1', self::NO_TYPE);
Expand All @@ -28,6 +31,10 @@ public function doFoo()
assertType('\'foo\'', self::PRIVATE_TYPE);
assertType('string', static::PRIVATE_TYPE);
assertType('string', $this::PRIVATE_TYPE);

assertType('\'zoo\'', self::FINAL_TYPE);
assertType('\'zoo\'', static::FINAL_TYPE);
assertType('\'zoo\'', $this::FINAL_TYPE);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class CallToFunctionParametersRuleTest extends RuleTestCase

private bool $checkExplicitMixed = false;

private bool $checkImplicitMixed = false;

protected function getRule(): Rule
{
$broker = $this->createReflectionProvider();
return new CallToFunctionParametersRule(
$broker,
new FunctionCallParametersCheck(new RuleLevelHelper($broker, true, false, true, $this->checkExplicitMixed, false, false), new NullsafeCheck(), new PhpVersion(80000), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
new FunctionCallParametersCheck(new RuleLevelHelper($broker, true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false), new NullsafeCheck(), new PhpVersion(80000), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
);
}

Expand Down Expand Up @@ -1910,4 +1912,11 @@ public function testBug11759(): void
$this->analyse([__DIR__ . '/data/bug-11759.php'], []);
}

public function testBug12051(): void
{
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-12051.php'], []);
}

}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-12051.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Bug12051;

/** @param array<int|string, mixed> $a */
function foo($a): void {
print "ok\n";
}

/**
* @param array<mixed> $a
*/
function bar($a): void {
foo($a);
}

0 comments on commit c684505

Please sign in to comment.