Skip to content

Commit

Permalink
FunctionReflection::isPure() and ExtendedMethodReflection::isPure()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 20, 2024
1 parent e94a9fc commit 03d01ea
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Reflection/Annotations/AnnotationMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,13 @@ public function isAbstract(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isPure(): TrinaryLogic
{
if ($this->hasSideEffects()->yes()) {
return TrinaryLogic::createNo();
}

return TrinaryLogic::createMaybe();
}

}
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/ChangedTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,9 @@ public function isAbstract(): TrinaryLogic
return $abstract;
}

public function isPure(): TrinaryLogic
{
return $this->reflection->isPure();
}

}
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/DummyConstructorReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,9 @@ public function isAbstract(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isPure(): TrinaryLogic
{
return TrinaryLogic::createYes();
}

}
5 changes: 5 additions & 0 deletions src/Reflection/Dummy/DummyMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,9 @@ public function isAbstract(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isPure(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

}
9 changes: 9 additions & 0 deletions src/Reflection/ExtendedMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ public function isFinalByKeyword(): TrinaryLogic;

public function isAbstract(): TrinaryLogic|bool;

/**
* This indicates whether the method has phpstan-pure
* or phpstan-impure annotation above it.
*
* In most cases asking hasSideEffects() is much more practical
* as it also accounts for void return type (method being always impure).
*/
public function isPure(): TrinaryLogic;

}
9 changes: 9 additions & 0 deletions src/Reflection/FunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ public function getDocComment(): ?string;

public function returnsByReference(): TrinaryLogic;

/**
* This indicates whether the function has phpstan-pure
* or phpstan-impure annotation above it.
*
* In most cases asking hasSideEffects() is much more practical
* as it also accounts for void return type (method being always impure).
*/
public function isPure(): TrinaryLogic;

}
9 changes: 9 additions & 0 deletions src/Reflection/Native/NativeFunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ public function hasSideEffects(): TrinaryLogic
return $this->hasSideEffects;
}

public function isPure(): TrinaryLogic
{
if ($this->hasSideEffects()->yes()) {
return TrinaryLogic::createNo();
}

return $this->hasSideEffects->negate();
}

private function isVoid(): bool
{
foreach ($this->variants as $variant) {
Expand Down
9 changes: 9 additions & 0 deletions src/Reflection/Native/NativeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ public function hasSideEffects(): TrinaryLogic
return $this->hasSideEffects;
}

public function isPure(): TrinaryLogic
{
if ($this->hasSideEffects()->yes()) {
return TrinaryLogic::createNo();
}

return $this->hasSideEffects->negate();
}

private function isVoid(): bool
{
foreach ($this->variants as $variant) {
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/ClosureCallMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,9 @@ public function isAbstract(): TrinaryLogic
return $abstract;
}

public function isPure(): TrinaryLogic
{
return $this->nativeMethodReflection->isPure();
}

}
5 changes: 5 additions & 0 deletions src/Reflection/Php/EnumCasesMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,9 @@ public function isAbstract(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isPure(): TrinaryLogic
{
return TrinaryLogic::createYes();
}

}
9 changes: 9 additions & 0 deletions src/Reflection/Php/PhpFunctionFromParserNodeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,13 @@ public function returnsByReference(): TrinaryLogic
return TrinaryLogic::createFromBoolean($this->functionLike->returnsByRef());
}

public function isPure(): TrinaryLogic
{
if ($this->isPure === null) {
return TrinaryLogic::createMaybe();
}

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

}
9 changes: 9 additions & 0 deletions src/Reflection/Php/PhpFunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isPure(): TrinaryLogic
{
if ($this->isPure === null) {
return TrinaryLogic::createMaybe();
}

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

public function isBuiltin(): bool
{
return $this->reflection->isInternal();
Expand Down
9 changes: 9 additions & 0 deletions src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,13 @@ public function returnsByReference(): TrinaryLogic
return $this->reflection->returnsByReference();
}

public function isPure(): TrinaryLogic
{
if ($this->isPure === null) {
return TrinaryLogic::createMaybe();
}

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

}
5 changes: 5 additions & 0 deletions src/Reflection/ResolvedMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ public function hasSideEffects(): TrinaryLogic
return $this->reflection->hasSideEffects();
}

public function isPure(): TrinaryLogic
{
return $this->reflection->hasSideEffects();
}

public function getAsserts(): Assertions
{
return $this->asserts ??= $this->reflection->getAsserts()->mapTypes(fn (Type $type) => TemplateTypeHelper::resolveTemplateTypes(
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Type/IntersectionTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::lazyMaxMin($this->methods, static fn (MethodReflection $method): TrinaryLogic => $method->hasSideEffects());
}

public function isPure(): TrinaryLogic
{
return TrinaryLogic::lazyMaxMin($this->methods, static fn (ExtendedMethodReflection $method): TrinaryLogic => $method->isPure());
}

public function getDocComment(): ?string
{
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Type/UnionTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ public function hasSideEffects(): TrinaryLogic
return TrinaryLogic::lazyExtremeIdentity($this->methods, static fn (MethodReflection $method): TrinaryLogic => $method->hasSideEffects());
}

public function isPure(): TrinaryLogic
{
return TrinaryLogic::lazyExtremeIdentity($this->methods, static fn (ExtendedMethodReflection $method): TrinaryLogic => $method->isPure());
}

public function getDocComment(): ?string
{
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/WrappedExtendedMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public function hasSideEffects(): TrinaryLogic
return $this->method->hasSideEffects();
}

public function isPure(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function getAsserts(): Assertions
{
return Assertions::createEmpty();
Expand Down

0 comments on commit 03d01ea

Please sign in to comment.