Skip to content
Permalink
Browse files

$a++, $a-- etc should be treated as assignments

  • Loading branch information...
muglug committed Aug 30, 2019
1 parent d891217 commit 504ccdab8cb44bcec6828454b24b38d42c02aa39
@@ -325,10 +325,20 @@ public static function analyze(
$stmt instanceof PhpParser\Node\Expr\PreInc ||
$stmt instanceof PhpParser\Node\Expr\PreDec
) {
$was_inside_assignment = $context->inside_assignment;
$context->inside_assignment = true;
if (self::analyze($statements_analyzer, $stmt->var, $context) === false) {
if (!$was_inside_assignment) {
$context->inside_assignment = false;
}
return false;
}
if (!$was_inside_assignment) {
$context->inside_assignment = false;
}
if (isset($stmt->var->inferredType)) {
$return_type = null;
@@ -1789,6 +1789,7 @@ private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, $fake_m
&& $stmt->stmts[0]->expr->var->name === 'this'
) {
$storage->mutation_free = true;
$storage->external_mutation_free = true;
} elseif (strpos($stmt->name->name, 'assert') === 0) {
$var_assertions = [];
@@ -1932,6 +1933,7 @@ private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, $fake_m
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod
&& $stmt->name->name === '__construct'
&& $class_storage
&& $storage instanceof MethodStorage
&& $storage->params
&& $this->config->infer_property_types_from_constructor
) {
@@ -2444,6 +2446,7 @@ function (FunctionLikeParameter $p) {
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod
&& $stmt->name->name === '__construct'
&& $class_storage
&& $storage instanceof MethodStorage
&& $storage->params
&& $this->config->infer_property_types_from_constructor
) {
@@ -2455,7 +2458,7 @@ function (FunctionLikeParameter $p) {
private function inferPropertyTypeFromConstructor(
PhpParser\Node\Stmt\ClassMethod $stmt,
FunctionLikeStorage $storage,
MethodStorage $storage,
ClassLikeStorage $class_storage
) : void {
if (!$stmt->stmts) {
@@ -2507,6 +2510,12 @@ private function inferPropertyTypeFromConstructor(
}
}
if (!$assigned_properties) {
return;
}
$storage->external_mutation_free = true;
foreach ($assigned_properties as $property_name => $property_type) {
$class_storage->properties[$property_name]->type = clone $property_type;
}
@@ -576,6 +576,14 @@ public function getFoo() : string {
$a = new A("hello");
$a->setFoo($a->getFoo() . "cool");',
],
'functionUsedAsArrayKeyInc' => [
'<?php
/** @param array<int, int> $arr */
function inc(array $arr) : array {
$arr[strlen("hello")]++;
return $arr;
}'
],
];
}

0 comments on commit 504ccda

Please sign in to comment.
You can’t perform that action at this time.