Skip to content

Commit

Permalink
Merge branch 'phpstan:2.1.x' into mb_convert_encoding-can-return-false
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-worman authored Dec 29, 2024
2 parents 1d07b0e + 5bfe8f1 commit 7004ae4
Show file tree
Hide file tree
Showing 53 changed files with 997 additions and 196 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ lint:
--exclude tests/PHPStan/Parser/data/cleaning-property-hooks-after.php \
--exclude tests/PHPStan/Rules/Properties/data/existing-classes-property-hooks.php \
--exclude tests/PHPStan/Rules/Properties/data/set-property-hook-parameter.php \
--exclude tests/PHPStan/Rules/Properties/data/overriding-final-property.php \
src tests

cs:
Expand Down
4 changes: 1 addition & 3 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ rules:
- PHPStan\Rules\Properties\MissingReadOnlyPropertyAssignRule
- PHPStan\Rules\Properties\MissingReadOnlyByPhpDocPropertyAssignRule
- PHPStan\Rules\Properties\PropertiesInInterfaceRule
- PHPStan\Rules\Properties\PropertyAssignRefRule
- PHPStan\Rules\Properties\PropertyAttributesRule
- PHPStan\Rules\Properties\PropertyHookAttributesRule
- PHPStan\Rules\Properties\PropertyInClassRule
Expand Down Expand Up @@ -179,9 +180,6 @@ services:
class: PHPStan\Rules\Properties\AccessPropertiesRule
tags:
- phpstan.rules.rule
arguments:
reportMagicProperties: %reportMagicProperties%
checkDynamicProperties: %checkDynamicProperties%

-
class: PHPStan\Rules\Properties\AccessStaticPropertiesRule
Expand Down
6 changes: 6 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,12 @@ services:
-
class: PHPStan\Rules\Playground\NeverRuleHelper

-
class: PHPStan\Rules\Properties\AccessPropertiesCheck
arguments:
reportMagicProperties: %reportMagicProperties%
checkDynamicProperties: %checkDynamicProperties%

-
class: PHPStan\Rules\Properties\LazyReadWritePropertiesExtensionProvider

Expand Down
57 changes: 56 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5381,12 +5381,67 @@ private function getBooleanExpressionDepth(Expr $expr, int $depth = 0): int
return $depth;
}

/** @api */
/**
* @api
* @deprecated Use canReadProperty() or canWriteProperty()
*/
public function canAccessProperty(PropertyReflection $propertyReflection): bool
{
return $this->canAccessClassMember($propertyReflection);
}

/** @api */
public function canReadProperty(ExtendedPropertyReflection $propertyReflection): bool
{
return $this->canAccessClassMember($propertyReflection);
}

/** @api */
public function canWriteProperty(ExtendedPropertyReflection $propertyReflection): bool
{
if (!$propertyReflection->isPrivateSet() && !$propertyReflection->isProtectedSet()) {
return $this->canAccessClassMember($propertyReflection);
}

if (!$this->phpVersion->supportsAsymmetricVisibility()) {
return $this->canAccessClassMember($propertyReflection);
}

$classReflectionName = $propertyReflection->getDeclaringClass()->getName();
$canAccessClassMember = static function (ClassReflection $classReflection) use ($propertyReflection, $classReflectionName) {
if ($propertyReflection->isPrivateSet()) {
return $classReflection->getName() === $classReflectionName;
}

// protected set

if (
$classReflection->getName() === $classReflectionName
|| $classReflection->isSubclassOf($classReflectionName)
) {
return true;
}

return $propertyReflection->getDeclaringClass()->isSubclassOf($classReflection->getName());
};

foreach ($this->inClosureBindScopeClasses as $inClosureBindScopeClass) {
if (!$this->reflectionProvider->hasClass($inClosureBindScopeClass)) {
continue;
}

if ($canAccessClassMember($this->reflectionProvider->getClass($inClosureBindScopeClass))) {
return true;
}
}

if ($this->isInClass()) {
return $canAccessClassMember($this->getClassReflection());
}

return false;
}

/** @api */
public function canCallMethod(MethodReflection $methodReflection): bool
{
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2993,7 +2993,7 @@ static function (): void {
$propertyName = $expr->name->toString();
$propertyHolderType = $scopeBeforeVar->getType($expr->var);
$propertyReflection = $scopeBeforeVar->getPropertyReflection($propertyHolderType, $propertyName);
if ($propertyReflection !== null) {
if ($propertyReflection !== null && $this->phpVersion->supportsPropertyHooks()) {
$propertyDeclaringClass = $propertyReflection->getDeclaringClass();
if ($propertyDeclaringClass->hasNativeProperty($propertyName)) {
$nativeProperty = $propertyDeclaringClass->getNativeProperty($propertyName);
Expand Down
13 changes: 13 additions & 0 deletions src/Analyser/OutOfClassScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\PropertyReflection;

Expand All @@ -31,6 +32,18 @@ public function canAccessProperty(PropertyReflection $propertyReflection): bool
return $propertyReflection->isPublic();
}

public function canReadProperty(ExtendedPropertyReflection $propertyReflection): bool
{
return $propertyReflection->isPublic();
}

public function canWriteProperty(ExtendedPropertyReflection $propertyReflection): bool
{
return $propertyReflection->isPublic()
&& !$propertyReflection->isProtectedSet()
&& !$propertyReflection->isPrivateSet();
}

public function canCallMethod(MethodReflection $methodReflection): bool
{
return $methodReflection->isPublic();
Expand Down
5 changes: 5 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ public function supportsPropertyHooks(): bool
return $this->versionId >= 80400;
}

public function supportsAsymmetricVisibility(): bool
{
return $this->versionId >= 80400;
}

public function hasDateTimeExceptions(): bool
{
return $this->versionId >= 80300;
Expand Down
10 changes: 10 additions & 0 deletions src/Reflection/Annotations/AnnotationPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,14 @@ public function getHook(string $hookType): ExtendedMethodReflection
throw new ShouldNotHappenException();
}

public function isProtectedSet(): bool
{
return false;
}

public function isPrivateSet(): bool
{
return false;
}

}
7 changes: 7 additions & 0 deletions src/Reflection/ClassMemberAccessAnswerer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ public function isInClass(): bool;

public function getClassReflection(): ?ClassReflection;

/**
* @deprecated Use canReadProperty() or canWriteProperty()
*/
public function canAccessProperty(PropertyReflection $propertyReflection): bool;

public function canReadProperty(ExtendedPropertyReflection $propertyReflection): bool;

public function canWriteProperty(ExtendedPropertyReflection $propertyReflection): bool;

public function canCallMethod(MethodReflection $methodReflection): bool;

public function canAccessConstant(ClassConstantReflection $constantReflection): bool;
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public function getProperty(string $propertyName, ClassMemberAccessAnswerer $sco
}

$property = $this->wrapExtendedProperty($extension->getProperty($this, $propertyName));
if ($scope->canAccessProperty($property)) {
if ($scope->canReadProperty($property)) {
return $this->properties[$key] = $property;
}
$this->properties[$key] = $property;
Expand Down
10 changes: 10 additions & 0 deletions src/Reflection/Dummy/ChangedTypePropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,14 @@ public function getHook(string $hookType): ExtendedMethodReflection
return $this->reflection->getHook($hookType);
}

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

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

}
10 changes: 10 additions & 0 deletions src/Reflection/Dummy/DummyPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,14 @@ public function getHook(string $hookType): ExtendedMethodReflection
throw new ShouldNotHappenException();
}

public function isProtectedSet(): bool
{
return false;
}

public function isPrivateSet(): bool
{
return false;
}

}
4 changes: 4 additions & 0 deletions src/Reflection/ExtendedPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public function hasHook(string $hookType): bool;
*/
public function getHook(string $hookType): ExtendedMethodReflection;

public function isProtectedSet(): bool;

public function isPrivateSet(): bool;

}
10 changes: 10 additions & 0 deletions src/Reflection/Php/EnumPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,14 @@ public function getHook(string $hookType): ExtendedMethodReflection
throw new ShouldNotHappenException();
}

public function isProtectedSet(): bool
{
return false;
}

public function isPrivateSet(): bool
{
return false;
}

}
39 changes: 38 additions & 1 deletion src/Reflection/Php/PhpPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,14 @@ public function isAbstract(): TrinaryLogic

public function isFinal(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($this->reflection->isFinal());
if ($this->reflection->isFinal()) {
return TrinaryLogic::createYes();
}
if ($this->reflection->isPrivate()) {
return TrinaryLogic::createNo();
}

return TrinaryLogic::createFromBoolean($this->isPrivateSet());
}

public function isVirtual(): TrinaryLogic
Expand Down Expand Up @@ -268,4 +275,34 @@ public function getHook(string $hookType): ExtendedMethodReflection
return $this->setHook;
}

public function isProtectedSet(): bool
{
if ($this->reflection->isProtectedSet()) {
return true;
}

if ($this->isReadOnly()) {
return !$this->isPrivate() && !$this->reflection->isPrivateSet();
}

return false;
}

public function isPrivateSet(): bool
{
if ($this->reflection->isPrivateSet()) {
return true;
}

if ($this->reflection->isProtectedSet()) {
return false;
}

if ($this->isReadOnly()) {
return $this->isPrivate();
}

return false;
}

}
10 changes: 10 additions & 0 deletions src/Reflection/Php/SimpleXMLElementProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,14 @@ public function getHook(string $hookType): ExtendedMethodReflection
throw new ShouldNotHappenException();
}

public function isProtectedSet(): bool
{
return false;
}

public function isPrivateSet(): bool
{
return false;
}

}
10 changes: 10 additions & 0 deletions src/Reflection/Php/UniversalObjectCrateProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,14 @@ public function getHook(string $hookType): ExtendedMethodReflection
throw new ShouldNotHappenException();
}

public function isProtectedSet(): bool
{
return false;
}

public function isPrivateSet(): bool
{
return false;
}

}
10 changes: 10 additions & 0 deletions src/Reflection/ResolvedPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,14 @@ public function getHook(string $hookType): ExtendedMethodReflection
);
}

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

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

}
10 changes: 10 additions & 0 deletions src/Reflection/Type/IntersectionTypePropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,14 @@ public function getHook(string $hookType): ExtendedMethodReflection
return new IntersectionTypeMethodReflection($hooks[0]->getName(), $hooks);
}

public function isProtectedSet(): bool
{
return $this->computeResult(static fn (ExtendedPropertyReflection $property) => $property->isProtectedSet());
}

public function isPrivateSet(): bool
{
return $this->computeResult(static fn (ExtendedPropertyReflection $property) => $property->isPrivateSet());
}

}
10 changes: 10 additions & 0 deletions src/Reflection/Type/UnionTypePropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,14 @@ public function getHook(string $hookType): ExtendedMethodReflection
return new UnionTypeMethodReflection($hooks[0]->getName(), $hooks);
}

public function isProtectedSet(): bool
{
return $this->computeResult(static fn (ExtendedPropertyReflection $property) => $property->isProtectedSet());
}

public function isPrivateSet(): bool
{
return $this->computeResult(static fn (ExtendedPropertyReflection $property) => $property->isPrivateSet());
}

}
10 changes: 10 additions & 0 deletions src/Reflection/WrappedExtendedPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,14 @@ public function getHook(string $hookType): ExtendedMethodReflection
throw new ShouldNotHappenException();
}

public function isProtectedSet(): bool
{
return false;
}

public function isPrivateSet(): bool
{
return false;
}

}
Loading

0 comments on commit 7004ae4

Please sign in to comment.