Skip to content
Open
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
17 changes: 16 additions & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PhpParser\Node\Name;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\AlwaysRememberedExpr;
use PHPStan\Node\Expr\PropertyInitializationExpr;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Node\IssetExpr;
use PHPStan\Node\Printer\ExprPrinter;
Expand Down Expand Up @@ -1028,7 +1029,21 @@ public function specifyTypesInCondition(
}
}

return new SpecifiedTypes();
if (
$issetExpr instanceof PropertyFetch
&& $issetExpr->var instanceof Expr\Variable
&& $issetExpr->var->name === 'this'
&& $issetExpr->name instanceof Node\Identifier
) {
return $this->create(
new PropertyInitializationExpr($issetExpr->name->toString()),
new NeverType(),
TypeSpecifierContext::createTrue(),
$scope,
)->setRootExpr($expr);
}

return new SpecifiedTypes();
}

$tmpVars = [$issetExpr];
Expand Down
21 changes: 16 additions & 5 deletions src/Node/ClassPropertiesNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
if (array_key_exists($propertyName, $initializedPropertiesMap)) {
$hasInitialization = $initializedPropertiesMap[$propertyName];
if (in_array($function->getName(), $constructors, true)) {
$hasInitialization = $hasInitialization->or($usageScope->hasExpressionType(new PropertyInitializationExpr($propertyName)));
$hasInitialization = $hasInitialization->or(self::hasPropertyInitialization($usageScope, $propertyName));
}
if (
!$hasInitialization->no()
Expand All @@ -234,9 +234,9 @@
) {
continue;
}
$hasInitialization = $initializedPropertiesMap[$propertyName]->or($usageScope->hasExpressionType(new PropertyInitializationExpr($propertyName)));
$hasInitialization = $initializedPropertiesMap[$propertyName]->or(self::hasPropertyInitialization($usageScope, $propertyName));
if (!$hasInitialization->yes() && $usageScope->isInAnonymousFunction() && $usageScope->getParentScope() !== null) {
$hasInitialization = $hasInitialization->or($usageScope->getParentScope()->hasExpressionType(new PropertyInitializationExpr($propertyName)));
$hasInitialization = $hasInitialization->or(self::hasPropertyInitialization($usageScope->getParentScope(), $propertyName));
}
if (!$hasInitialization->yes()) {
$prematureAccess[] = [
Expand Down Expand Up @@ -304,7 +304,7 @@
}

foreach (array_keys($uninitializedProperties) as $propertyName) {
if (!$methodScope->hasExpressionType(new PropertyInitializationExpr($propertyName))->yes()) {
if (!self::hasPropertyInitialization($methodScope, $propertyName)->yes()) {
continue;
}

Expand Down Expand Up @@ -405,12 +405,23 @@
private function getInitializedProperties(Scope $scope, array $initialInitializedProperties): array
{
foreach ($initialInitializedProperties as $propertyName => $isInitialized) {
$initialInitializedProperties[$propertyName] = $isInitialized->or($scope->hasExpressionType(new PropertyInitializationExpr($propertyName)));
$initialInitializedProperties[$propertyName] = $isInitialized->or(self::hasPropertyInitialization($scope, $propertyName));
}

return $initialInitializedProperties;
}

private static function hasPropertyInitialization(Scope $scope, string $propertyName): TrinaryLogic
{
$initExpr = new PropertyInitializationExpr($propertyName);
$has = $scope->hasExpressionType($initExpr);
if ($has->yes() && $scope->getType($initExpr) instanceof NeverType) {

Check warning on line 418 in src/Node/ClassPropertiesNode.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ { $initExpr = new PropertyInitializationExpr($propertyName); $has = $scope->hasExpressionType($initExpr); - if ($has->yes() && $scope->getType($initExpr) instanceof NeverType) { + if (!$has->no() && $scope->getType($initExpr) instanceof NeverType) { return TrinaryLogic::createNo(); }

Check warning on line 418 in src/Node/ClassPropertiesNode.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ { $initExpr = new PropertyInitializationExpr($propertyName); $has = $scope->hasExpressionType($initExpr); - if ($has->yes() && $scope->getType($initExpr) instanceof NeverType) { + if (!$has->no() && $scope->getType($initExpr) instanceof NeverType) { return TrinaryLogic::createNo(); }
return TrinaryLogic::createNo();
}

return $has;
}

/**
* @return list<PropertyAssign>
*/
Expand Down
1 change: 1 addition & 0 deletions src/Rules/IssetCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function check(Expr $expr, Scope $scope, string $operatorDescription, str
&& $expr->var instanceof Expr\Variable
&& $expr->var->name === 'this'
&& $scope->hasExpressionType(new PropertyInitializationExpr($propertyReflection->getName()))->yes()
&& !$scope->getType(new PropertyInitializationExpr($propertyReflection->getName())) instanceof NeverType
) {
return $this->generateError(
$propertyReflection->getNativeType(),
Expand Down
12 changes: 12 additions & 0 deletions src/Rules/Properties/ReadOnlyPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\Expr\PropertyInitializationExpr;
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
use PHPStan\Node\Expr\UnsetOffsetExpr;
use PHPStan\Node\PropertyAssignNode;
Expand All @@ -14,6 +15,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeUtils;
use function in_array;
Expand Down Expand Up @@ -111,6 +113,16 @@
continue;
}

if (
$propertyFetch->var instanceof Node\Expr\Variable
&& $propertyFetch->var->name === 'this'
&& $propertyFetch->name instanceof Node\Identifier
&& $scope->hasExpressionType(new PropertyInitializationExpr($propertyFetch->name->toString()))->yes()

Check warning on line 120 in src/Rules/Properties/ReadOnlyPropertyAssignRule.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $propertyFetch->var instanceof Node\Expr\Variable && $propertyFetch->var->name === 'this' && $propertyFetch->name instanceof Node\Identifier - && $scope->hasExpressionType(new PropertyInitializationExpr($propertyFetch->name->toString()))->yes() + && !$scope->hasExpressionType(new PropertyInitializationExpr($propertyFetch->name->toString()))->no() && $scope->getType(new PropertyInitializationExpr($propertyFetch->name->toString())) instanceof NeverType ) { continue;

Check warning on line 120 in src/Rules/Properties/ReadOnlyPropertyAssignRule.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $propertyFetch->var instanceof Node\Expr\Variable && $propertyFetch->var->name === 'this' && $propertyFetch->name instanceof Node\Identifier - && $scope->hasExpressionType(new PropertyInitializationExpr($propertyFetch->name->toString()))->yes() + && !$scope->hasExpressionType(new PropertyInitializationExpr($propertyFetch->name->toString()))->no() && $scope->getType(new PropertyInitializationExpr($propertyFetch->name->toString())) instanceof NeverType ) { continue;
&& $scope->getType(new PropertyInitializationExpr($propertyFetch->name->toString())) instanceof NeverType
) {
continue;
}

$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is assigned outside of the constructor.', $declaringClass->getDisplayName(), $propertyReflection->getName()))
->line($propertyFetch->name->getStartLine())
->identifier('property.readOnlyAssignNotInConstructor')
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,15 @@ public function testCloneWith(): void
$this->analyse([__DIR__ . '/data/readonly-property-assign-clone-with.php'], []);
}

#[RequiresPhp('>= 8.1')]
public function testBug13853(): void
{
$this->analyse([__DIR__ . '/data/bug-13853.php'], [
[
'Readonly property Bug13853\NoIssetGuard::$prop is assigned outside of the constructor.',
58,
],
]);
}

}
60 changes: 60 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-13853.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug13853;

abstract class BaseReportLocator
{
private readonly string $report;

public function __construct(
private readonly string $defaultPathname,
) {
}

final public function locate(): string
{
if (isset($this->report)) {
return $this->report;
}

$this->report = is_file($this->defaultPathname)
? $this->defaultPathname
: $this->lookup();

return $this->report;
}

public abstract function lookup(): string;
}

class AnotherExample
{
private readonly int $value;

public function getValue(): int
{
if (!isset($this->value)) {
$this->value = $this->compute();
}

return $this->value;
}

private function compute(): int
{
return 42;
}
}

class NoIssetGuard
{
private readonly string $prop;

public function setProp(): void
{
// no isset guard - should still report error
$this->prop = 'foo';
}
}
Loading