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 @@ -891,14 +891,6 @@
}
$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 @@ -914,22 +906,6 @@
$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 @@ -938,6 +914,32 @@
}
}

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

Check warning on line 923 in src/Reflection/Php/PhpClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ( strtolower($methodReflection->getName()) === '__construct' || ( - ($phpDocReturnType === null || !$phpDocReturnType->isVoid()->yes()) + ($phpDocReturnType === null || $phpDocReturnType->isVoid()->no()) && !$nativeReturnType->isVoid()->yes() ) ) {

Check warning on line 923 in src/Reflection/Php/PhpClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ( strtolower($methodReflection->getName()) === '__construct' || ( - ($phpDocReturnType === null || !$phpDocReturnType->isVoid()->yes()) + ($phpDocReturnType === null || $phpDocReturnType->isVoid()->no()) && !$nativeReturnType->isVoid()->yes() ) ) {
&& !$nativeReturnType->isVoid()->yes()

Check warning on line 924 in src/Reflection/Php/PhpClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ strtolower($methodReflection->getName()) === '__construct' || ( ($phpDocReturnType === null || !$phpDocReturnType->isVoid()->yes()) - && !$nativeReturnType->isVoid()->yes() + && $nativeReturnType->isVoid()->no() ) ) { $isPure = true;

Check warning on line 924 in src/Reflection/Php/PhpClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ strtolower($methodReflection->getName()) === '__construct' || ( ($phpDocReturnType === null || !$phpDocReturnType->isVoid()->yes()) - && !$nativeReturnType->isVoid()->yes() + && $nativeReturnType->isVoid()->no() ) ) { $isPure = true;
)
) {
$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
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,4 +615,16 @@ public function testBug11006(): void
$this->analyse([__DIR__ . '/data/bug-11006.php'], []);
}

#[RequiresPhp('>= 8.0')]
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