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
50 changes: 26 additions & 24 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,14 +893,6 @@ public function createUserlandMethodReflection(ClassReflection $fileDeclaringCla
}
$phpDocParameterTypes[$paramName] = $paramTag->getType();
}
foreach ($phpDocParameterTypes as $paramName => $paramType) {
$phpDocParameterTypes[$paramName] = TemplateTypeHelper::resolveTemplateTypes(
$paramType,
$phpDocBlockClassReflection->getActiveTemplateTypeMap(),
$phpDocBlockClassReflection->getCallSiteVarianceMap(),
TemplateTypeVariance::createContravariant(),
);
}
foreach ($resolvedPhpDoc->getParamOutTags() as $paramName => $paramOutTag) {
$phpDocParameterOutTypes[$paramName] = TemplateTypeHelper::resolveTemplateTypes(
$paramOutTag->getType(),
Expand All @@ -916,22 +908,6 @@ public function createUserlandMethodReflection(ClassReflection $fileDeclaringCla
$isInternal = $resolvedPhpDoc->isInternal();
$isFinal = $resolvedPhpDoc->isFinal();
$isPure ??= $resolvedPhpDoc->isPure();
if ($isPure === null) {
$classResolvedPhpDoc = $phpDocBlockClassReflection->getResolvedPhpDoc();
if ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsPure()) {
if (
strtolower($methodReflection->getName()) === '__construct'
|| (
($phpDocReturnType === null || !$phpDocReturnType->isVoid()->yes())
&& !$nativeReturnType->isVoid()->yes()
)
) {
$isPure = true;
}
} elseif ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsImpure()) {
$isPure = false;
}
}
$asserts = Assertions::createFromResolvedPhpDocBlock($resolvedPhpDoc);
$acceptsNamedArguments = $resolvedPhpDoc->acceptsNamedArguments();
$selfOutType = $resolvedPhpDoc->getSelfOutTag() !== null ? $resolvedPhpDoc->getSelfOutTag()->getType() : null;
Expand All @@ -940,6 +916,32 @@ public function createUserlandMethodReflection(ClassReflection $fileDeclaringCla
}
}

if ($isPure === null) {
$classResolvedPhpDoc = $phpDocBlockClassReflection->getResolvedPhpDoc();
if ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsPure()) {
if (
strtolower($methodReflection->getName()) === '__construct'
|| (
($phpDocReturnType === null || !$phpDocReturnType->isVoid()->yes())
&& !$nativeReturnType->isVoid()->yes()
)
) {
$isPure = true;
}
} elseif ($classResolvedPhpDoc !== null && $classResolvedPhpDoc->areAllMethodsImpure()) {
$isPure = false;
}
}

foreach ($phpDocParameterTypes as $paramName => $paramType) {
$phpDocParameterTypes[$paramName] = TemplateTypeHelper::resolveTemplateTypes(
$paramType,
$phpDocBlockClassReflection->getActiveTemplateTypeMap(),
$phpDocBlockClassReflection->getCallSiteVarianceMap(),
TemplateTypeVariance::createContravariant(),
);
}

return $this->methodReflectionFactory->create(
$actualDeclaringClass,
$declaringTrait,
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,4 +646,15 @@ public function testConstantParameterCheckInstantiation(): void
]);
}

public function testBug14138(): void
{
$this->analyse([__DIR__ . '/data/bug-14138.php'], [
[
'Parameter #1 $data of class Bug14138\Foo constructor expects array{foo: int, bar: int}, array{foo: 1} given.',
36,
"Array does not have offset 'bar'.",
],
]);
}

}
37 changes: 37 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-14138.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug14138;

/**
* @template T of array
*/
abstract class AbstractApiData
{
public function __construct(
/** @var T */
protected array $data
) {}

/**
* @return T
*/
public function getData(): array
{
return $this->data;
}
}


/**
* @extends AbstractApiData<array{
* foo: int,
* bar: int,
* }>
*/
class Foo extends AbstractApiData {}

function testing(): void {
$a = new Foo(["foo" => 1]);
}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Pure/PureMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ public function testAllMethodsArePure(): void
]);
}

#[RequiresPhp('>= 8.0')]
public function testBug14138Pure(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-14138-pure.php'], []);
}

public function testBug12382(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Pure/data/bug-14138-pure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php // lint >= 8.0

namespace Bug14138Pure;

/**
* @phpstan-all-methods-pure
*/
class PureClassWithPromotedProps
{
public function __construct(
protected int $value
) {}

public function getValue(): int
{
return $this->value;
}
}

class TestCaller
{
/** @phpstan-pure */
public function callPureConstructor(): PureClassWithPromotedProps
{
return new PureClassWithPromotedProps(1);
}
}
Loading