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
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 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
$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()) {
return [];
}

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

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()) {
return [];
}

if ($hasStatic->yes()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Php/MethodExistsTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function specifyTypes(
]),
$context,
$scope,
);
)->unionWith($this->createFuncCallSpec($node, $context, $scope));
}

private function createFuncCallSpec(FuncCall $node, TypeSpecifierContext $context, Scope $scope): SpecifiedTypes
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Php/PropertyExistsTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function specifyTypes(
]),
$context,
$scope,
);
)->unionWith($this->createFuncCallSpec($node, $context, $scope));
}

$propertyNode = new PropertyFetch(
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14667.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace Bug14667Nsrt;

use function PHPStan\Testing\assertType;

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

function testExplicitMixed(mixed $row): void
{
if (property_exists($row, 'prop')) {
assertType('class-string|(object&hasProperty(prop))', $row);
}
}
12 changes: 8 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,12 @@ public function testBug14596(): void
]);
}

public function testBug14667(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-14667.php'], []);
}

}
41 changes: 41 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-14667.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace Bug14667Methods;

/** @param mixed $row */
function testImplicitMixed($row): void
{
if (method_exists($row, 'foo')) {
$row->foo();
}
}

function testExplicitMixed(mixed $row): void
{
if (method_exists($row, 'foo')) {
$row->foo();
}
}

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

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

/** @param mixed $x */
function testMixedChained($x): void
{
if (method_exists($x, 'getName') && $x->getName() !== null) {
echo $x->getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,9 @@ public function testBug2861(): void
$this->analyse([__DIR__ . '/data/bug-2861-assign.php'], []);
}

public function testBug14667(): void
{
$this->analyse([__DIR__ . '/data/bug-14667-assign.php'], []);
}

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1302,4 +1302,12 @@ public function testBug2861(): void
$this->analyse([__DIR__ . '/data/bug-2861.php'], []);
}

public function testBug14667(): void
{
$this->checkThisOnly = false;
$this->checkUnionTypes = true;
$this->checkDynamicProperties = true;
$this->analyse([__DIR__ . '/data/bug-14667.php'], []);
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-14667-assign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace Bug14667Assign;

/** @param mixed $row */
function testImplicitMixed($row): void
{
if (property_exists($row, 'prop')) {
$row->prop = 'value';
}
}

function testExplicitMixed(mixed $row): void
{
if (property_exists($row, 'prop')) {
$row->prop = 'value';
}
}

/** @param object|string $row */
function testObjectOrString($row): void
{
if (property_exists($row, 'prop')) {
$row->prop = 'value';
}
}
59 changes: 59 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-14667.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

namespace Bug14667;

/** @param mixed $row */
function testImplicitMixed($row): void
{
if (property_exists($row, 'prop')) {
echo $row->prop;
}
}

function testExplicitMixed(mixed $row): void
{
if (property_exists($row, 'prop')) {
echo $row->prop;
}
}

/** @param object|string $row */
function testObjectOrString($row): void
{
if (property_exists($row, 'prop')) {
echo $row->prop;
}
}

function testObject(object $row): void
{
if (property_exists($row, 'prop')) {
echo $row->prop;
}
}

final class Foo
{
public function testThis(): void
{
if (property_exists($this, 'default')) {
echo $this->default;
}
}

/** @param self $obj */
public function testSelf(self $obj): void
{
if (property_exists($obj, 'default')) {
echo $obj->default;
}
}

/** @param mixed $x */
public function testMixedChained($x): void
{
if (property_exists($x, 'name') && $x->name !== null) {
echo $x->name;
}
}
}
Loading