Skip to content

Commit c49acec

Browse files
committed
[Php81] Skip NullToStrictStringFuncCallArgRector for magic __get() property access resolving to ErrorType
Fixes rectorphp/rector#9762
1 parent 89d191b commit c49acec

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector\Fixture;
4+
5+
class EntityWithMagicGet
6+
{
7+
protected array $_fields = [];
8+
9+
public function __get(string $field)
10+
{
11+
$value = null;
12+
if (isset($this->_fields[$field])) {
13+
$value = &$this->_fields[$field];
14+
}
15+
16+
return $value;
17+
}
18+
}
19+
20+
$entity = new EntityWithMagicGet();
21+
if ($entity->someProp) {
22+
preg_replace('/\r\n|\n|\r/', '', $entity->someProp);
23+
}

‎rules/Php81/NodeManipulator/NullToStrictStringIntConverter.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Expr\Cast\String_ as CastString_;
1111
use PhpParser\Node\Expr\FuncCall;
1212
use PhpParser\Node\Expr\MethodCall;
13+
use PhpParser\Node\Expr\PropertyFetch;
1314
use PhpParser\Node\Expr\Ternary;
1415
use PhpParser\Node\Expr\Variable;
1516
use PhpParser\Node\Scalar\Int_;
@@ -19,9 +20,11 @@
1920
use PHPStan\Analyser\Scope;
2021
use PHPStan\Reflection\Native\ExtendedNativeParameterReflection;
2122
use PHPStan\Reflection\ParametersAcceptor;
23+
use PHPStan\Reflection\ReflectionProvider;
2224
use PHPStan\Type\ErrorType;
2325
use PHPStan\Type\MixedType;
2426
use PHPStan\Type\NullType;
27+
use PHPStan\Type\ObjectType;
2528
use PHPStan\Type\Type;
2629
use PHPStan\Type\UnionType;
2730
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
@@ -35,6 +38,7 @@ public function __construct(
3538
private ValueResolver $valueResolver,
3639
private NodeTypeResolver $nodeTypeResolver,
3740
private PropertyFetchAnalyzer $propertyFetchAnalyzer,
41+
private ReflectionProvider $reflectionProvider,
3842
) {
3943
}
4044

@@ -116,6 +120,10 @@ public function convertIfNull(
116120

117121
private function shouldSkipValue(Expr $expr, Scope $scope, bool $isTrait, string $targetType): bool
118122
{
123+
if ($this->isPropertyFetchOnClassWithMagicGet($expr)) {
124+
return true;
125+
}
126+
119127
$type = $this->nodeTypeResolver->getType($expr);
120128
if ($type->isString()->yes() && $targetType === 'string') {
121129
return true;
@@ -153,6 +161,24 @@ private function shouldSkipValue(Expr $expr, Scope $scope, bool $isTrait, string
153161
return $this->shouldSkipTrait($expr, $type, $isTrait);
154162
}
155163

164+
private function isPropertyFetchOnClassWithMagicGet(Expr $expr): bool
165+
{
166+
if (! $expr instanceof PropertyFetch) {
167+
return false;
168+
}
169+
170+
$varType = $this->nodeTypeResolver->getType($expr->var);
171+
if (! $varType instanceof ObjectType) {
172+
return false;
173+
}
174+
175+
if (! $this->reflectionProvider->hasClass($varType->getClassName())) {
176+
return false;
177+
}
178+
179+
return $this->reflectionProvider->getClass($varType->getClassName())->hasMethod('__get');
180+
}
181+
156182
private function isValidUnionType(Type $type): bool
157183
{
158184
if (! $type instanceof UnionType) {

0 commit comments

Comments
 (0)