Skip to content
Closed
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
26 changes: 22 additions & 4 deletions src/Rules/Methods/MethodCallCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\NullsafeOperatorHelper;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
Expand Down Expand Up @@ -65,6 +66,18 @@ public function check(
if ($type instanceof StaticType) {
$typeForDescribe = $type->getStaticObjectType();
}
$methodExistsCall = new Expr\FuncCall(new FullyQualified('method_exists'), [
new Arg($var),
new Arg(new String_($methodName)),
]);
if ($scope->getType($methodExistsCall)->isTrue()->yes()) {
if ($type->hasMethod($methodName)->yes()) {
return [[], $type->getMethod($methodName, $scope)];
}

return [[], null];
}

if (!$type->canCallMethods()->yes() || $type->isClassString()->yes()) {
return [
[
Expand Down Expand Up @@ -122,15 +135,20 @@ public function check(
}
}

if ($astName instanceof Expr) {
if ($astName instanceof Identifier) {
$methodExistsExpr = new Expr\FuncCall(new FullyQualified('method_exists'), [
new Arg($var),
new Arg(new String_($methodName)),
]);
} else {
$methodExistsExpr = new Expr\FuncCall(new FullyQualified('method_exists'), [
new Arg($var),
new Arg($astName),
]);
}

if ($scope->getType($methodExistsExpr)->isTrue()->yes()) {
return [[], null];
}
if ($scope->getType($methodExistsExpr)->isTrue()->yes()) {
return [[], null];
}

return [
Expand Down
23 changes: 18 additions & 5 deletions src/Rules/Properties/AccessPropertiesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace PHPStan\Rules\Properties;

use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\NullsafeOperatorHelper;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
Expand Down Expand Up @@ -119,6 +119,14 @@
$typeForDescribe = $type->getStaticObjectType();
}

$propertyExistsCall = new FuncCall(new FullyQualified('property_exists'), [
new Arg($node->var),
new Arg(new String_($name)),
]);
if ($scope->getType($propertyExistsCall)->isTrue()->yes() && !$type->canAccessProperties()->no()) {

Check warning on line 126 in src/Rules/Properties/AccessPropertiesCheck.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\LooseBooleanMutator": @@ @@ new Arg($node->var), new Arg(new String_($name)), ]); - if ($scope->getType($propertyExistsCall)->isTrue()->yes() && !$type->canAccessProperties()->no()) { + if ($scope->getType($propertyExistsCall)->toBoolean()->isTrue()->yes() && !$type->canAccessProperties()->no()) { return []; }

Check warning on line 126 in src/Rules/Properties/AccessPropertiesCheck.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\LooseBooleanMutator": @@ @@ new Arg($node->var), new Arg(new String_($name)), ]); - if ($scope->getType($propertyExistsCall)->isTrue()->yes() && !$type->canAccessProperties()->no()) { + if ($scope->getType($propertyExistsCall)->toBoolean()->isTrue()->yes() && !$type->canAccessProperties()->no()) { return []; }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test for this condition, when $type canAccessProperties is maybe

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Added testClassStringOrObject test cases to both the properties and methods bug-14667 test files. These use @param class-string|object which directly produces canAccessProperties()/canCallMethods() = maybe (class-string returns no, object returns yes, union returns maybe), explicitly exercising the !$type->canAccessProperties()->no() condition. All 12140 tests pass and make phpstan reports no errors.

return [];
}

if ($type->canAccessProperties()->no() || $type->canAccessProperties()->maybe() && !$scope->isUndefinedExpressionAllowed($node)) {
return [
RuleErrorBuilder::message(sprintf(
Expand Down Expand Up @@ -202,15 +210,20 @@
}
}

if ($node->name instanceof Expr) {
if ($node->name instanceof Identifier) {
$propertyExistsExpr = new FuncCall(new FullyQualified('property_exists'), [
new Arg($node->var),
new Arg(new String_($name)),
]);
} else {
$propertyExistsExpr = new FuncCall(new FullyQualified('property_exists'), [
new Arg($node->var),
new Arg($node->name),
]);
}

if ($scope->getType($propertyExistsExpr)->isTrue()->yes()) {
return [];
}
if ($scope->getType($propertyExistsExpr)->isTrue()->yes()) {

Check warning on line 225 in src/Rules/Properties/AccessPropertiesCheck.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\LooseBooleanMutator": @@ @@ ]); } - if ($scope->getType($propertyExistsExpr)->isTrue()->yes()) { + if ($scope->getType($propertyExistsExpr)->toBoolean()->isTrue()->yes()) { return []; }

Check warning on line 225 in src/Rules/Properties/AccessPropertiesCheck.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\LooseBooleanMutator": @@ @@ ]); } - if ($scope->getType($propertyExistsExpr)->isTrue()->yes()) { + if ($scope->getType($propertyExistsExpr)->toBoolean()->isTrue()->yes()) { return []; }
return [];
}

if ($hasStatic->yes()) {
Expand Down
19 changes: 11 additions & 8 deletions src/Type/Php/MethodExistsTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public function specifyTypes(
return $this->createFuncCallSpec($node, $context, $scope);
}

$objectType = $scope->getType($args[0]->value);
if ($objectType->isString()->yes()) {
if ($objectType->isClassString()->yes()) {
foreach ($objectType->getClassStringObjectType()->getObjectClassReflections() as $classReflection) {
$objectOrStringType = $scope->getType($args[0]->value);
if ($objectOrStringType->isString()->yes()) {
if ($objectOrStringType->isClassString()->yes()) {
foreach ($objectOrStringType->getClassStringObjectType()->getObjectClassReflections() as $classReflection) {
if ($classReflection->hasMethod($methodNameType->getValue()) && !$classReflection->hasNativeMethod($methodNameType->getValue())) {
return $this->createFuncCallSpec($node, $context, $scope);
}
Expand All @@ -68,7 +68,7 @@ public function specifyTypes(
return $this->typeSpecifier->create(
$args[0]->value,
new IntersectionType([
$objectType,
$objectOrStringType,
new HasMethodType($methodNameType->getValue()),
]),
$context,
Expand All @@ -79,7 +79,7 @@ public function specifyTypes(
return new SpecifiedTypes([], []);
}

foreach ($objectType->getObjectClassReflections() as $classReflection) {
foreach ($objectOrStringType->getObjectClassReflections() as $classReflection) {
if ($classReflection->hasMethod($methodNameType->getValue()) && !$classReflection->hasNativeMethod($methodNameType->getValue())) {
return $this->createFuncCallSpec($node, $context, $scope);
}
Expand All @@ -92,11 +92,14 @@ public function specifyTypes(
new ObjectWithoutClassType(),
new HasMethodType($methodNameType->getValue()),
]),
new ClassStringType(),
new IntersectionType([
new ClassStringType(),
new HasMethodType($methodNameType->getValue()),
]),
]),
$context,
$scope,
);
)->unionWith($this->createFuncCallSpec($node, $context, $scope));
Comment thread
staabm marked this conversation as resolved.
}

private function createFuncCallSpec(FuncCall $node, TypeSpecifierContext $context, Scope $scope): SpecifiedTypes
Expand Down
20 changes: 7 additions & 13 deletions src/Type/Php/PropertyExistsTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,23 @@ public function specifyTypes(
return new SpecifiedTypes([], []);
}

$objectType = $scope->getType($args[0]->value);
if ($objectType->isString()->yes()) {
return $this->typeSpecifier->create(
new FuncCall(new FullyQualified('property_exists'), $node->getRawArgs()),
new ConstantBooleanType(true),
$context,
$scope,
);
}

if (!$objectType->isObject()->yes()) {
$objectOrStringType = $scope->getType($args[0]->value);
if (!$objectOrStringType->isObject()->yes()) {
return $this->typeSpecifier->create(
$args[0]->value,
new UnionType([
new IntersectionType([
new ObjectWithoutClassType(),
new HasPropertyType($propertyNameType->getValue()),
]),
new ClassStringType(),
new IntersectionType([
new ClassStringType(),
new HasPropertyType($propertyNameType->getValue()),
]),
]),
$context,
$scope,
);
)->unionWith($this->createFuncCallSpec($node, $context, $scope));
Comment thread
staabm marked this conversation as resolved.
}

$propertyNode = new PropertyFetch(
Expand Down
35 changes: 35 additions & 0 deletions src/Type/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\Reflection\MissingPropertyFromReflectionException;
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
Expand Down Expand Up @@ -291,6 +294,38 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
return new BooleanType();
}

public function hasInstanceProperty(string $propertyName): TrinaryLogic
{
if ($this->isClassString()->yes()) {
return TrinaryLogic::createMaybe();
}
return TrinaryLogic::createNo();
}

public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
{
if ($this->isClassString()->yes()) {
throw new MissingPropertyFromReflectionException($this->describe(VerbosityLevel::typeOnly()), $propertyName);
}
throw new ShouldNotHappenException();
}

public function hasStaticProperty(string $propertyName): TrinaryLogic
{
if ($this->isClassString()->yes()) {
return TrinaryLogic::createMaybe();
}
return TrinaryLogic::createNo();
}

public function getStaticProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
{
if ($this->isClassString()->yes()) {
throw new MissingPropertyFromReflectionException($this->describe(VerbosityLevel::typeOnly()), $propertyName);
}
throw new ShouldNotHappenException();
}

public function hasMethod(string $methodName): TrinaryLogic
{
if ($this->isClassString()->yes()) {
Expand Down
76 changes: 76 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14667.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php // lint >= 8.0

namespace Bug14667Nsrt;

use function PHPStan\Testing\assertType;

/** @param mixed $row */
function testMixed($row): void
Comment thread
staabm marked this conversation as resolved.
{
if (property_exists($row, 'prop')) {
assertType('(class-string&hasProperty(prop))|(object&hasProperty(prop))', $row);
}
}

function testExplicitMixed(mixed $row): void
{
if (property_exists($row, 'prop')) {
assertType('(class-string&hasProperty(prop))|(object&hasProperty(prop))', $row);
}
}

/** @param mixed $row */
function testMethodExistsMixed($row): void
{
if (method_exists($row, 'foo')) {
assertType('(class-string&hasMethod(foo))|(object&hasMethod(foo))', $row);
$row->foo();
}
}

function testMethodExistsExplicitMixed(mixed $row): void
{
if (method_exists($row, 'foo')) {
assertType('(class-string&hasMethod(foo))|(object&hasMethod(foo))', $row);
$row->foo();
}
}

/** @param object|string $row */
function testMethodExistsObjectOrString($row): void
{
if (method_exists($row, 'foo')) {
$row->foo();
}
}

function testMethodExistsObject(object $row): void
{
if (method_exists($row, 'bar')) {
$row->bar();
}
}

/** @param mixed $x */
function testMethodExistsMixedChained($x): void
{
if (method_exists($x, 'getName') && $x->getName() !== null) {
echo $x->getName();
}
}

/** @param class-string|object $row */
function testMethodExistsClassStringOrObject($row): void
{
if (method_exists($row, 'foo')) {
$row->foo();
}
}

/** @param class-string $row */
function testMethodExistsClassString(string $row): void
{
if (method_exists($row, 'foo')) {
$row->foo(); // error: Cannot call method foo() on class-string.
}
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-2861.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
function testObjectOrString($objectOrClass): void {
if (property_exists($objectOrClass, 'foo')) {
assertType('class-string|(object&hasProperty(foo))', $objectOrClass);
assertType('(class-string&hasProperty(foo))|(object&hasProperty(foo))', $objectOrClass);
}
}

Expand All @@ -18,6 +18,6 @@ function testObjectOrString($objectOrClass): void {
*/
function testObjectOrClassString($objectOrClass): void {
if (property_exists($objectOrClass, 'bar')) {
assertType('class-string|(object&hasProperty(bar))', $objectOrClass);
assertType('(class-string&hasProperty(bar))|(object&hasProperty(bar))', $objectOrClass);
}
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-4573.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Foo
public function doFoo($stringOrObject): void
{
if (is_callable([$stringOrObject, 'doFoo'])) {
assertType('Bug4573\Bar|class-string', $stringOrObject);
assertType('Bug4573\Bar|(class-string&hasMethod(doFoo))', $stringOrObject);
}
}

Expand All @@ -33,7 +33,7 @@ public function doFoo($stringOrObject): void
public function doBar($stringOrObject): void
{
if (method_exists($stringOrObject, 'doFoo')) {
assertType('Bug4573\Bar|class-string', $stringOrObject);
assertType('Bug4573\Bar|(class-string&hasMethod(doFoo))', $stringOrObject);
}
}

Expand Down
18 changes: 14 additions & 4 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,6 @@ public function testCallMethods(): void
'Call to an undefined method Test\Foo::lorem().',
911,
],
[
'Cannot call method foo() on class-string|object.',
914,
],
[
'Parameter #1 $callable of method Test\\MethodExists::doBar() expects callable(): mixed, array{class-string|object, \'foo\'} given.',
915,
Expand Down Expand Up @@ -4106,4 +4102,18 @@ public function testBug14596(): void
]);
}

#[RequiresPhp('>= 8.0.0')]
public function testBug14667(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-14667.php'], [
[
'Cannot call method foo() on class-string.',
74,
],
]);
}

}
Loading
Loading