2016-12-28 21:52:44 +01:00
|
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Codebase;
|
|
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2020-07-22 01:40:35 +02:00
|
|
|
|
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
|
2016-12-28 21:52:44 +01:00
|
|
|
|
use Psalm\CodeLocation;
|
2018-02-23 21:39:33 +01:00
|
|
|
|
use Psalm\FileSource;
|
2018-04-18 18:01:13 +02:00
|
|
|
|
use Psalm\Issue\DocblockTypeContradiction;
|
2020-07-25 23:27:45 +02:00
|
|
|
|
use Psalm\Issue\RedundantIdentityWithTrue;
|
2018-04-17 21:39:09 +02:00
|
|
|
|
use Psalm\Issue\RedundantCondition;
|
2018-06-26 00:02:05 +02:00
|
|
|
|
use Psalm\Issue\RedundantConditionGivenDocblockType;
|
2017-04-06 21:36:22 +02:00
|
|
|
|
use Psalm\Issue\TypeDoesNotContainNull;
|
2016-12-28 21:52:44 +01:00
|
|
|
|
use Psalm\Issue\TypeDoesNotContainType;
|
2017-10-23 01:53:53 +02:00
|
|
|
|
use Psalm\Issue\UnevaluatedCode;
|
2016-12-28 21:52:44 +01:00
|
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
|
use Psalm\Type;
|
2020-09-22 07:10:46 +02:00
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
|
use function substr;
|
|
|
|
|
use function count;
|
|
|
|
|
use function strtolower;
|
|
|
|
|
use function in_array;
|
|
|
|
|
use function array_merge;
|
|
|
|
|
use function strpos;
|
|
|
|
|
use function is_int;
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
2017-01-07 20:35:07 +01:00
|
|
|
|
class AssertionFinder
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
2020-09-20 18:54:46 +02:00
|
|
|
|
public const ASSIGNMENT_TO_RIGHT = 1;
|
|
|
|
|
public const ASSIGNMENT_TO_LEFT = -1;
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets all the type assertions in a conditional
|
|
|
|
|
*
|
2020-10-25 15:49:39 +01:00
|
|
|
|
* @return list<non-empty-array<string, non-empty-list<non-empty-list<string>>>>
|
2016-12-28 21:52:44 +01:00
|
|
|
|
*/
|
2018-06-26 00:02:05 +02:00
|
|
|
|
public static function scrapeAssertions(
|
2016-12-28 21:52:44 +01:00
|
|
|
|
PhpParser\Node\Expr $conditional,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
?string $this_class_name,
|
2018-11-06 03:57:36 +01:00
|
|
|
|
FileSource $source,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
?Codebase $codebase = null,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
bool $inside_negation = false,
|
2020-07-10 22:49:45 +02:00
|
|
|
|
bool $cache = true,
|
|
|
|
|
bool $inside_conditional = true
|
2020-09-16 23:35:55 +02:00
|
|
|
|
): array {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$if_types = [];
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
2016-12-28 21:52:44 +01:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\Instanceof_) {
|
2019-01-27 20:10:33 +01:00
|
|
|
|
$instanceof_types = self::getInstanceOfTypes($conditional, $this_class_name, $source);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2019-01-27 20:10:33 +01:00
|
|
|
|
if ($instanceof_types) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2016-12-28 21:52:44 +01:00
|
|
|
|
$conditional->expr,
|
|
|
|
|
$this_class_name,
|
2017-01-07 20:35:07 +01:00
|
|
|
|
$source
|
2016-12-28 21:52:44 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
2019-01-27 20:10:33 +01:00
|
|
|
|
$if_types[$var_name] = [$instanceof_types];
|
2019-03-16 17:34:48 +01:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$var_type = $source instanceof StatementsAnalyzer
|
|
|
|
|
? $source->node_data->getType($conditional->expr)
|
|
|
|
|
: null;
|
2019-03-16 17:34:48 +01:00
|
|
|
|
|
|
|
|
|
foreach ($instanceof_types as $instanceof_type) {
|
2019-06-20 14:37:57 +02:00
|
|
|
|
if ($instanceof_type[0] === '=') {
|
|
|
|
|
$instanceof_type = substr($instanceof_type, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-16 17:34:48 +01:00
|
|
|
|
if ($codebase
|
|
|
|
|
&& $var_type
|
|
|
|
|
&& $inside_negation
|
2019-11-25 17:44:54 +01:00
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
2019-03-16 17:34:48 +01:00
|
|
|
|
) {
|
|
|
|
|
if ($codebase->interfaceExists($instanceof_type)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 06:26:11 +02:00
|
|
|
|
$instanceof_type = Type::parseString(
|
|
|
|
|
$instanceof_type,
|
|
|
|
|
null,
|
|
|
|
|
$source->getTemplateTypeMap() ?: []
|
|
|
|
|
);
|
2019-03-16 17:34:48 +01:00
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::canExpressionTypesBeIdentical(
|
2019-03-16 17:34:48 +01:00
|
|
|
|
$codebase,
|
|
|
|
|
$instanceof_type,
|
|
|
|
|
$var_type
|
|
|
|
|
)) {
|
|
|
|
|
if ($var_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantConditionGivenDocblockType(
|
2020-04-10 06:26:11 +02:00
|
|
|
|
$var_type->getId() . ' does not contain '
|
|
|
|
|
. $instanceof_type->getId(),
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' ' . $instanceof_type->getId()
|
2019-03-16 17:34:48 +01:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantCondition(
|
2020-04-10 06:26:11 +02:00
|
|
|
|
$var_type->getId() . ' cannot be identical to '
|
|
|
|
|
. $instanceof_type->getId(),
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' ' . $instanceof_type->getId()
|
2019-03-16 17:34:48 +01:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 19:16:52 +02:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\Assign) {
|
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
|
|
|
|
$conditional->var,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
2020-07-10 22:49:45 +02:00
|
|
|
|
$candidate_if_types = $inside_conditional
|
|
|
|
|
? self::scrapeAssertions(
|
|
|
|
|
$conditional->expr,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$inside_negation,
|
|
|
|
|
$cache,
|
|
|
|
|
$inside_conditional
|
|
|
|
|
)
|
|
|
|
|
: [];
|
2020-06-24 19:16:52 +02:00
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
|
|
|
|
if ($candidate_if_types) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
$if_types[$var_name] = [['>' . \json_encode($candidate_if_types[0])]];
|
2020-06-24 19:16:52 +02:00
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['!falsy']];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2020-06-24 19:16:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2016-12-28 21:52:44 +01:00
|
|
|
|
$conditional,
|
|
|
|
|
$this_class_name,
|
2017-01-07 20:35:07 +01:00
|
|
|
|
$source
|
2018-05-07 07:26:06 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$var_name] = [['!falsy']];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2019-12-30 16:01:31 +01:00
|
|
|
|
if (!$conditional instanceof PhpParser\Node\Expr\MethodCall
|
|
|
|
|
&& !$conditional instanceof PhpParser\Node\Expr\StaticCall
|
|
|
|
|
) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return [$if_types];
|
2019-12-30 16:01:31 +01:00
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BooleanNot) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$expr_assertions = null;
|
|
|
|
|
|
|
|
|
|
if ($cache && $source instanceof StatementsAnalyzer) {
|
|
|
|
|
$expr_assertions = $source->node_data->getAssertions($conditional->expr);
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($expr_assertions === null) {
|
|
|
|
|
$expr_assertions = self::scrapeAssertions(
|
|
|
|
|
$conditional->expr,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
!$inside_negation,
|
2020-07-10 22:49:45 +02:00
|
|
|
|
$cache,
|
|
|
|
|
$inside_conditional
|
2019-11-25 17:44:54 +01:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($cache && $source instanceof StatementsAnalyzer) {
|
|
|
|
|
$source->node_data->setAssertions($conditional->expr, $expr_assertions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-14 03:31:58 +02:00
|
|
|
|
if (count($expr_assertions) !== 1) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
$expr_assertions = $expr_assertions[0];
|
|
|
|
|
|
|
|
|
|
if (count($expr_assertions) !== 1) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 22:15:44 +01:00
|
|
|
|
$if_types = \Psalm\Internal\Algebra::negateTypes($expr_assertions);
|
2020-07-14 03:31:58 +02:00
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical ||
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Equal
|
|
|
|
|
) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
$and_types = self::scrapeEqualityAssertions(
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
false,
|
2020-07-10 22:49:45 +02:00
|
|
|
|
$cache,
|
|
|
|
|
$inside_conditional
|
2019-11-25 17:44:54 +01:00
|
|
|
|
);
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $and_types;
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical ||
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\NotEqual
|
|
|
|
|
) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
$and_types = self::scrapeInequalityAssertions(
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
false,
|
2020-07-10 22:49:45 +02:00
|
|
|
|
$cache,
|
|
|
|
|
$inside_conditional
|
2019-11-25 17:44:54 +01:00
|
|
|
|
);
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $and_types;
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-19 22:15:19 +01:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\GreaterOrEqual
|
|
|
|
|
) {
|
2019-11-26 22:34:13 +01:00
|
|
|
|
$min_count = null;
|
|
|
|
|
$count_equality_position = self::hasNonEmptyCountEqualityCheck($conditional, $min_count);
|
2020-07-27 00:29:17 +02:00
|
|
|
|
$min_comparison = null;
|
|
|
|
|
$positive_number_position = self::hasPositiveNumberCheck($conditional, $min_comparison);
|
2020-09-12 22:13:13 +02:00
|
|
|
|
$max_count = null;
|
|
|
|
|
$count_inequality_position = self::hasLessThanCountEqualityCheck($conditional, $max_count);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2018-12-19 22:15:19 +01:00
|
|
|
|
if ($count_equality_position) {
|
|
|
|
|
if ($count_equality_position === self::ASSIGNMENT_TO_RIGHT) {
|
2019-02-21 23:17:10 +01:00
|
|
|
|
$counted_expr = $conditional->left;
|
2018-12-19 22:15:19 +01:00
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$count_equality_position value');
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 21:41:44 +02:00
|
|
|
|
/** @var PhpParser\Node\Expr\FuncCall $counted_expr */
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2019-09-19 21:41:44 +02:00
|
|
|
|
$counted_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
|
|
|
|
if (self::hasReconcilableNonEmptyCountEqualityCheck($conditional)) {
|
|
|
|
|
$if_types[$var_name] = [['non-empty-countable']];
|
|
|
|
|
} else {
|
2019-11-26 22:34:13 +01:00
|
|
|
|
if ($min_count) {
|
|
|
|
|
$if_types[$var_name] = [['=has-at-least-' . $min_count]];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['=non-empty-countable']];
|
|
|
|
|
}
|
2019-02-21 23:17:10 +01:00
|
|
|
|
}
|
2018-12-19 22:15:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-12-19 22:15:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 22:13:13 +02:00
|
|
|
|
if ($count_inequality_position) {
|
|
|
|
|
if ($count_inequality_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$count_expr = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$count_inequality_position value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var PhpParser\Node\Expr\FuncCall $count_expr */
|
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
|
|
|
|
$count_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
|
|
|
|
if ($max_count) {
|
|
|
|
|
$if_types[$var_name] = [['!has-at-least-' . ($max_count + 1)]];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['!non-empty-countable']];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2020-09-12 22:13:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 00:29:17 +02:00
|
|
|
|
if ($positive_number_position) {
|
|
|
|
|
if ($positive_number_position === self::ASSIGNMENT_TO_RIGHT) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$conditional->left,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
2020-07-30 17:25:47 +02:00
|
|
|
|
$value_node = $conditional->left;
|
2017-10-23 01:11:28 +02:00
|
|
|
|
} else {
|
2020-07-27 00:29:17 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
|
|
|
|
$conditional->right,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
2020-07-30 17:25:47 +02:00
|
|
|
|
$value_node = $conditional->right;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
&& ($var_type = $source->node_data->getType($value_node))
|
|
|
|
|
&& $var_type->isSingle()
|
|
|
|
|
&& $var_type->hasBool()
|
|
|
|
|
&& $min_comparison > 1
|
|
|
|
|
) {
|
|
|
|
|
if ($var_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new DocblockTypeContradiction(
|
|
|
|
|
$var_type . ' cannot be greater than ' . $min_comparison,
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
null
|
2020-07-30 17:25:47 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TypeDoesNotContainType(
|
|
|
|
|
$var_type . ' cannot be greater than ' . $min_comparison,
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
null
|
2020-07-30 17:25:47 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
2020-07-27 00:29:17 +02:00
|
|
|
|
$if_types[$var_name] = [[($min_comparison === 1 ? '' : '=') . 'positive-numeric']];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
return [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2017-10-22 18:09:22 +02:00
|
|
|
|
|
2018-12-19 22:15:19 +01:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\SmallerOrEqual
|
|
|
|
|
) {
|
2019-11-26 22:34:13 +01:00
|
|
|
|
$min_count = null;
|
|
|
|
|
$count_equality_position = self::hasNonEmptyCountEqualityCheck($conditional, $min_count);
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$typed_value_position = self::hasTypedValueComparison($conditional, $source);
|
2018-06-26 00:02:05 +02:00
|
|
|
|
|
2020-08-30 17:32:01 +02:00
|
|
|
|
$max_count = null;
|
|
|
|
|
$count_inequality_position = self::hasLessThanCountEqualityCheck($conditional, $max_count);
|
|
|
|
|
|
2018-12-19 22:15:19 +01:00
|
|
|
|
if ($count_equality_position) {
|
2019-03-26 03:30:40 +01:00
|
|
|
|
if ($count_equality_position === self::ASSIGNMENT_TO_LEFT) {
|
2018-12-19 22:15:19 +01:00
|
|
|
|
$count_expr = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$count_equality_position value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var PhpParser\Node\Expr\FuncCall $count_expr */
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-12-19 22:15:19 +01:00
|
|
|
|
$count_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
2019-11-26 22:34:13 +01:00
|
|
|
|
if ($min_count) {
|
|
|
|
|
$if_types[$var_name] = [['=has-at-least-' . $min_count]];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['=non-empty-countable']];
|
|
|
|
|
}
|
2018-12-19 22:15:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-12-19 22:15:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 17:32:01 +02:00
|
|
|
|
if ($count_inequality_position) {
|
|
|
|
|
if ($count_inequality_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$count_expr = $conditional->left;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$count_inequality_position value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var PhpParser\Node\Expr\FuncCall $count_expr */
|
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
|
|
|
|
$count_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
|
|
|
|
if ($max_count) {
|
|
|
|
|
$if_types[$var_name] = [['!has-at-least-' . ($max_count + 1)]];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['!non-empty-countable']];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2020-08-30 17:32:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($typed_value_position) {
|
|
|
|
|
if ($typed_value_position === self::ASSIGNMENT_TO_RIGHT) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2020-05-18 14:52:37 +02:00
|
|
|
|
$conditional->left,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$expr = $conditional->right;
|
2018-06-26 00:02:05 +02:00
|
|
|
|
} elseif ($typed_value_position === self::ASSIGNMENT_TO_LEFT) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$conditional->right,
|
2017-10-23 01:11:28 +02:00
|
|
|
|
$this_class_name,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$source
|
2017-10-23 01:11:28 +02:00
|
|
|
|
);
|
2020-05-18 14:52:37 +02:00
|
|
|
|
|
|
|
|
|
$expr = $conditional->left;
|
2018-06-26 00:02:05 +02:00
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$typed_value_position value');
|
2017-10-22 18:09:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 14:52:37 +02:00
|
|
|
|
$expr_type = $source instanceof StatementsAnalyzer
|
|
|
|
|
? $source->node_data->getType($expr)
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
if ($var_name
|
|
|
|
|
&& $expr_type
|
|
|
|
|
&& $expr_type->isSingleIntLiteral()
|
|
|
|
|
&& ($expr_type->getSingleIntLiteral()->value === 0)
|
|
|
|
|
) {
|
2018-11-16 16:13:52 +01:00
|
|
|
|
$if_types[$var_name] = [['=isset']];
|
2017-10-22 18:09:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2017-10-22 18:09:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
return [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2017-04-06 20:53:45 +02:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\FuncCall) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
$and_types = self::processFunctionCall($conditional, $this_class_name, $source, $codebase, false);
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $and_types;
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2019-05-22 23:49:38 +02:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\MethodCall
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\StaticCall
|
|
|
|
|
) {
|
2019-12-30 16:48:50 +01:00
|
|
|
|
$custom_assertions = self::processCustomAssertion($conditional, $this_class_name, $source, false);
|
|
|
|
|
|
|
|
|
|
if ($custom_assertions) {
|
|
|
|
|
return $custom_assertions;
|
|
|
|
|
}
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-07-11 17:22:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\Empty_) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$conditional->expr,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
2017-10-23 01:53:53 +02:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($var_name) {
|
2020-10-24 18:46:27 +02:00
|
|
|
|
if ($conditional->expr instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
&& ($var_type = $source->node_data->getType($conditional->expr))
|
|
|
|
|
&& !$var_type->isMixed()
|
|
|
|
|
&& !$var_type->possibly_undefined
|
|
|
|
|
) {
|
|
|
|
|
$if_types[$var_name] = [['falsy']];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['empty']];
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\Isset_) {
|
|
|
|
|
foreach ($conditional->vars as $isset_var) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$isset_var,
|
2017-10-23 01:53:53 +02:00
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($var_name) {
|
|
|
|
|
$if_types[$var_name] = [['isset']];
|
|
|
|
|
} else {
|
|
|
|
|
// look for any variables we *can* use for an isset assertion
|
|
|
|
|
$array_root = $isset_var;
|
2017-10-23 01:53:53 +02:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
while ($array_root instanceof PhpParser\Node\Expr\ArrayDimFetch && !$var_name) {
|
|
|
|
|
$array_root = $array_root->var;
|
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$array_root,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
2017-10-23 01:53:53 +02:00
|
|
|
|
}
|
2018-06-26 00:02:05 +02:00
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
2018-11-16 16:13:52 +01:00
|
|
|
|
$if_types[$var_name] = [['=isset']];
|
2017-10-23 01:53:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2017-10-23 01:53:53 +02:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Coalesce) {
|
2020-01-14 06:53:38 +01:00
|
|
|
|
return self::scrapeAssertions(
|
|
|
|
|
new PhpParser\Node\Expr\Ternary(
|
|
|
|
|
new PhpParser\Node\Expr\Isset_(
|
|
|
|
|
[$conditional->left]
|
|
|
|
|
),
|
|
|
|
|
$conditional->left,
|
|
|
|
|
$conditional->right,
|
|
|
|
|
$conditional->getAttributes()
|
|
|
|
|
),
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$this_class_name,
|
2020-01-14 06:53:38 +01:00
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$inside_negation,
|
2020-07-10 22:49:45 +02:00
|
|
|
|
false,
|
|
|
|
|
$inside_conditional
|
2018-06-26 00:02:05 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2018-05-13 00:46:47 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
return [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param PhpParser\Node\Expr\BinaryOp\Identical|PhpParser\Node\Expr\BinaryOp\Equal $conditional
|
|
|
|
|
*
|
2020-10-25 15:49:39 +01:00
|
|
|
|
* @return list<non-empty-array<string, non-empty-list<non-empty-list<string>>>>
|
2018-06-26 00:02:05 +02:00
|
|
|
|
*/
|
|
|
|
|
private static function scrapeEqualityAssertions(
|
|
|
|
|
PhpParser\Node\Expr\BinaryOp $conditional,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
?string $this_class_name,
|
2018-11-06 03:57:36 +01:00
|
|
|
|
FileSource $source,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
?Codebase $codebase = null,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
bool $inside_negation = false,
|
2020-07-10 22:49:45 +02:00
|
|
|
|
bool $cache = true,
|
|
|
|
|
bool $inside_conditional = true
|
2020-09-04 22:26:33 +02:00
|
|
|
|
): array {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$if_types = [];
|
|
|
|
|
|
2020-08-26 23:58:01 +02:00
|
|
|
|
$null_position = self::hasNullVariable($conditional, $source);
|
2018-06-26 00:02:05 +02:00
|
|
|
|
|
|
|
|
|
if ($null_position !== null) {
|
|
|
|
|
if ($null_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$base_conditional = $conditional->left;
|
|
|
|
|
} elseif ($null_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$base_conditional = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$null_position value');
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
2020-01-06 17:09:07 +01:00
|
|
|
|
if ($var_name && $base_conditional instanceof PhpParser\Node\Expr\Assign) {
|
|
|
|
|
$var_name = '=' . $var_name;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($var_name) {
|
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) {
|
|
|
|
|
$if_types[$var_name] = [['null']];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['falsy']];
|
2018-05-03 19:56:30 +02:00
|
|
|
|
}
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2018-04-18 18:01:13 +02:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if ($codebase
|
2020-01-09 21:45:17 +01:00
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
&& ($var_type = $source->node_data->getType($base_conditional))
|
2018-06-26 00:02:05 +02:00
|
|
|
|
&& $conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|
) {
|
|
|
|
|
$null_type = Type::getNull();
|
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::isContainedBy(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$var_type,
|
|
|
|
|
$null_type
|
2020-07-22 01:40:35 +02:00
|
|
|
|
) && !UnionTypeComparator::isContainedBy(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$null_type,
|
|
|
|
|
$var_type
|
|
|
|
|
)) {
|
|
|
|
|
if ($var_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new DocblockTypeContradiction(
|
|
|
|
|
$var_type . ' does not contain null',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type . ' null'
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TypeDoesNotContainNull(
|
|
|
|
|
$var_type . ' does not contain null',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId()
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$true_position = self::hasTrueVariable($conditional);
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($true_position) {
|
|
|
|
|
if ($true_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$base_conditional = $conditional->left;
|
|
|
|
|
} elseif ($true_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$base_conditional = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('Unrecognised position');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($base_conditional instanceof PhpParser\Node\Expr\FuncCall) {
|
2019-04-12 15:38:56 +02:00
|
|
|
|
$if_types = self::processFunctionCall(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source,
|
2020-02-22 16:41:57 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
false
|
|
|
|
|
);
|
2019-04-12 15:38:56 +02:00
|
|
|
|
} else {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2019-04-12 15:38:56 +02:00
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
2018-06-26 00:02:05 +02:00
|
|
|
|
|
2019-04-12 15:38:56 +02:00
|
|
|
|
if ($var_name) {
|
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) {
|
|
|
|
|
$if_types[$var_name] = [['true']];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['!falsy']];
|
|
|
|
|
}
|
2020-10-25 15:49:39 +01:00
|
|
|
|
|
|
|
|
|
$if_types = [$if_types];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$base_assertions = null;
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($source instanceof StatementsAnalyzer && $cache) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$base_assertions = $source->node_data->getAssertions($base_conditional);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($base_assertions === null) {
|
|
|
|
|
$base_assertions = self::scrapeAssertions(
|
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$inside_negation,
|
|
|
|
|
$cache
|
|
|
|
|
);
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($source instanceof StatementsAnalyzer && $cache) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$source->node_data->setAssertions($base_conditional, $base_assertions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$if_types = $base_assertions;
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
&& ($var_type = $source->node_data->getType($base_conditional))
|
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) {
|
2020-07-30 16:25:59 +02:00
|
|
|
|
$config = $source->getCodebase()->config;
|
|
|
|
|
|
|
|
|
|
if ($config->strict_binary_operands
|
|
|
|
|
&& $var_type->isSingle()
|
|
|
|
|
&& $var_type->hasBool()
|
|
|
|
|
&& !$var_type->from_docblock
|
|
|
|
|
) {
|
2020-07-25 23:27:45 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantIdentityWithTrue(
|
2020-07-30 16:25:59 +02:00
|
|
|
|
'The "=== true" part of this comparison is redundant',
|
2020-07-25 23:27:45 +02:00
|
|
|
|
new CodeLocation($source, $conditional)
|
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-30 16:25:59 +02:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$true_type = Type::getTrue();
|
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::canExpressionTypesBeIdentical(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$true_type,
|
|
|
|
|
$var_type
|
2018-05-03 19:56:30 +02:00
|
|
|
|
)) {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($var_type->from_docblock) {
|
2018-05-03 19:56:30 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new DocblockTypeContradiction(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$var_type . ' does not contain true',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type . ' true'
|
2018-05-03 19:56:30 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-05-18 17:02:50 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TypeDoesNotContainType(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$var_type . ' does not contain true',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type . ' true'
|
2018-05-18 17:02:50 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
2017-04-06 20:53:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
2017-04-06 20:53:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
return $if_types;
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$false_position = self::hasFalseVariable($conditional);
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($false_position) {
|
|
|
|
|
if ($false_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$base_conditional = $conditional->left;
|
|
|
|
|
} elseif ($false_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$base_conditional = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$false_position value');
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($base_conditional instanceof PhpParser\Node\Expr\FuncCall) {
|
2019-04-12 16:30:56 +02:00
|
|
|
|
$if_types = self::processFunctionCall(
|
2017-10-23 01:11:28 +02:00
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$source,
|
2020-02-22 16:41:57 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
true
|
2017-10-23 01:11:28 +02:00
|
|
|
|
);
|
2019-04-12 16:30:56 +02:00
|
|
|
|
} else {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2019-04-12 16:30:56 +02:00
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2019-04-12 16:30:56 +02:00
|
|
|
|
if ($var_name) {
|
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) {
|
|
|
|
|
$if_types[$var_name] = [['false']];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['falsy']];
|
|
|
|
|
}
|
2020-01-09 21:45:17 +01:00
|
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$base_assertions = null;
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($source instanceof StatementsAnalyzer && $cache) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$base_assertions = $source->node_data->getAssertions($base_conditional);
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($base_assertions === null) {
|
|
|
|
|
$base_assertions = self::scrapeAssertions(
|
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$inside_negation,
|
2020-07-10 22:49:45 +02:00
|
|
|
|
$cache,
|
|
|
|
|
$inside_conditional
|
2019-11-25 17:44:54 +01:00
|
|
|
|
);
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($source instanceof StatementsAnalyzer && $cache) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$source->node_data->setAssertions($base_conditional, $base_assertions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$notif_types = $base_assertions;
|
2018-06-17 19:20:37 +02:00
|
|
|
|
|
2019-04-12 16:30:56 +02:00
|
|
|
|
if (count($notif_types) === 1) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
$notif_types = $notif_types[0];
|
|
|
|
|
|
|
|
|
|
if (count($notif_types) === 1) {
|
2020-11-03 22:15:44 +01:00
|
|
|
|
$if_types = \Psalm\Internal\Algebra::negateTypes($notif_types);
|
2020-10-25 15:49:39 +01:00
|
|
|
|
}
|
2019-04-12 16:30:56 +02:00
|
|
|
|
}
|
2018-06-17 19:20:37 +02:00
|
|
|
|
}
|
2020-10-25 15:49:39 +01:00
|
|
|
|
|
|
|
|
|
$if_types = $if_types ? [$if_types] : [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2018-06-17 19:20:37 +02:00
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
&& ($var_type = $source->node_data->getType($base_conditional))
|
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$false_type = Type::getFalse();
|
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::canExpressionTypesBeIdentical(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$false_type,
|
|
|
|
|
$var_type
|
|
|
|
|
)) {
|
|
|
|
|
if ($var_type->from_docblock) {
|
2018-06-17 19:20:37 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
new DocblockTypeContradiction(
|
|
|
|
|
$var_type . ' does not contain false',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type . ' false'
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TypeDoesNotContainType(
|
|
|
|
|
$var_type . ' does not contain false',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type . ' false'
|
2018-06-17 19:20:37 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
return $if_types;
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$empty_array_position = self::hasEmptyArrayVariable($conditional);
|
|
|
|
|
|
2019-03-02 21:18:29 +01:00
|
|
|
|
if ($empty_array_position !== null) {
|
|
|
|
|
if ($empty_array_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$base_conditional = $conditional->left;
|
|
|
|
|
} elseif ($empty_array_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$base_conditional = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$empty_array_position value');
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2019-03-02 21:18:29 +01:00
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical) {
|
|
|
|
|
$if_types[$var_name] = [['!non-empty-countable']];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['falsy']];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($codebase
|
2020-01-09 21:45:17 +01:00
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
&& ($var_type = $source->node_data->getType($base_conditional))
|
2019-03-02 21:18:29 +01:00
|
|
|
|
&& $conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|
) {
|
2020-05-16 22:41:07 +02:00
|
|
|
|
$empty_array_type = Type::getEmptyArray();
|
2019-03-02 21:18:29 +01:00
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::canExpressionTypesBeIdentical(
|
2019-03-02 21:18:29 +01:00
|
|
|
|
$codebase,
|
2020-05-16 22:41:07 +02:00
|
|
|
|
$empty_array_type,
|
2019-03-02 21:18:29 +01:00
|
|
|
|
$var_type
|
|
|
|
|
)) {
|
|
|
|
|
if ($var_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new DocblockTypeContradiction(
|
2019-03-02 21:19:59 +01:00
|
|
|
|
$var_type . ' does not contain an empty array',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
null
|
2019-03-02 21:18:29 +01:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
2019-03-26 03:30:40 +01:00
|
|
|
|
new TypeDoesNotContainType(
|
2019-03-02 21:19:59 +01:00
|
|
|
|
$var_type . ' does not contain empty array',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
null
|
2019-03-02 21:18:29 +01:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2019-03-02 21:18:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$gettype_position = self::hasGetTypeCheck($conditional);
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($gettype_position) {
|
|
|
|
|
if ($gettype_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$string_expr = $conditional->left;
|
|
|
|
|
$gettype_expr = $conditional->right;
|
|
|
|
|
} elseif ($gettype_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$string_expr = $conditional->right;
|
|
|
|
|
$gettype_expr = $conditional->left;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$gettype_position value');
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
/** @var PhpParser\Node\Expr\FuncCall $gettype_expr */
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$gettype_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
2018-06-17 19:20:37 +02:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
/** @var PhpParser\Node\Scalar\String_ $string_expr */
|
|
|
|
|
$var_type = $string_expr->value;
|
2018-06-17 19:20:37 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if (!isset(ClassLikeAnalyzer::GETTYPE_TYPES[$var_type])) {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new UnevaluatedCode(
|
|
|
|
|
'gettype cannot return this value',
|
|
|
|
|
new CodeLocation($source, $string_expr)
|
|
|
|
|
)
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
2018-06-26 00:02:05 +02:00
|
|
|
|
} else {
|
|
|
|
|
if ($var_name && $var_type) {
|
|
|
|
|
$if_types[$var_name] = [[$var_type]];
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2018-06-17 19:20:37 +02:00
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$get_debug_type_position = self::hasGetDebugTypeCheck($conditional);
|
|
|
|
|
|
2020-10-03 01:15:47 +02:00
|
|
|
|
if ($get_debug_type_position) {
|
|
|
|
|
if ($get_debug_type_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$whichclass_expr = $conditional->left;
|
|
|
|
|
$get_debug_type_expr = $conditional->right;
|
|
|
|
|
} elseif ($get_debug_type_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$whichclass_expr = $conditional->right;
|
|
|
|
|
$get_debug_type_expr = $conditional->left;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$gettype_position value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var PhpParser\Node\Expr\FuncCall $get_debug_type_expr */
|
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
|
|
|
|
$get_debug_type_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($whichclass_expr instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
|
$var_type = $whichclass_expr->value;
|
|
|
|
|
} elseif ($whichclass_expr instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $whichclass_expr->class instanceof PhpParser\Node\Name
|
|
|
|
|
) {
|
|
|
|
|
$var_type = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
|
|
|
|
$whichclass_expr->class,
|
|
|
|
|
$source->getAliases()
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('Shouldn’t get here');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($var_name && $var_type) {
|
|
|
|
|
if ($var_type === 'class@anonymous') {
|
|
|
|
|
$if_types[$var_name] = [['=object']];
|
|
|
|
|
} elseif ($var_type === 'resource (closed)') {
|
|
|
|
|
$if_types[$var_name] = [['closed-resource']];
|
|
|
|
|
} elseif (substr($var_type, 0, 10) === 'resource (') {
|
|
|
|
|
$if_types[$var_name] = [['=resource']];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [[$var_type]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2020-10-03 01:15:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$min_count = null;
|
|
|
|
|
$count_equality_position = self::hasNonEmptyCountEqualityCheck($conditional, $min_count);
|
|
|
|
|
|
2018-12-19 22:15:19 +01:00
|
|
|
|
if ($count_equality_position) {
|
|
|
|
|
if ($count_equality_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$count_expr = $conditional->left;
|
|
|
|
|
} elseif ($count_equality_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$count_expr = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$count_equality_position value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var PhpParser\Node\Expr\FuncCall $count_expr */
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-12-19 22:15:19 +01:00
|
|
|
|
$count_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
2019-11-26 22:34:13 +01:00
|
|
|
|
if ($min_count) {
|
|
|
|
|
$if_types[$var_name] = [['=has-at-least-' . $min_count]];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['=non-empty-countable']];
|
|
|
|
|
}
|
2018-12-19 22:15:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-12-19 22:15:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if (!$source instanceof StatementsAnalyzer) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$getclass_position = self::hasGetClassCheck($conditional, $source);
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($getclass_position) {
|
|
|
|
|
if ($getclass_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$whichclass_expr = $conditional->left;
|
|
|
|
|
$getclass_expr = $conditional->right;
|
|
|
|
|
} elseif ($getclass_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$whichclass_expr = $conditional->right;
|
|
|
|
|
$getclass_expr = $conditional->left;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$getclass_position value');
|
|
|
|
|
}
|
2018-06-17 19:20:37 +02:00
|
|
|
|
|
2020-04-19 18:53:50 +02:00
|
|
|
|
if ($getclass_expr instanceof PhpParser\Node\Expr\FuncCall && isset($getclass_expr->args[0])) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-07-18 04:50:30 +02:00
|
|
|
|
$getclass_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$var_name = '$this';
|
|
|
|
|
}
|
2018-06-17 19:20:37 +02:00
|
|
|
|
|
2018-11-29 05:59:43 +01:00
|
|
|
|
if ($whichclass_expr instanceof PhpParser\Node\Expr\ClassConstFetch
|
2018-06-26 00:02:05 +02:00
|
|
|
|
&& $whichclass_expr->class instanceof PhpParser\Node\Name
|
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$var_type = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$whichclass_expr->class,
|
|
|
|
|
$source->getAliases()
|
|
|
|
|
);
|
2018-07-13 15:52:15 +02:00
|
|
|
|
|
2020-06-23 19:11:19 +02:00
|
|
|
|
if ($var_type === 'self' || $var_type === 'static') {
|
2018-07-13 15:52:15 +02:00
|
|
|
|
$var_type = $this_class_name;
|
2020-06-23 19:11:19 +02:00
|
|
|
|
} elseif ($var_type === 'parent') {
|
2018-07-13 15:52:15 +02:00
|
|
|
|
$var_type = null;
|
|
|
|
|
}
|
2018-06-17 19:20:37 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($var_type) {
|
2019-07-06 04:57:38 +02:00
|
|
|
|
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
|
|
|
|
$source,
|
|
|
|
|
$var_type,
|
|
|
|
|
new CodeLocation($source, $whichclass_expr),
|
2020-02-11 22:39:33 +01:00
|
|
|
|
null,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
null,
|
2019-07-06 04:57:38 +02:00
|
|
|
|
$source->getSuppressedIssues(),
|
2020-02-15 02:54:26 +01:00
|
|
|
|
true
|
2019-07-06 04:57:38 +02:00
|
|
|
|
) === false
|
|
|
|
|
) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return [];
|
2019-07-06 04:57:38 +02:00
|
|
|
|
}
|
2018-06-17 19:20:37 +02:00
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2019-07-06 04:57:38 +02:00
|
|
|
|
if ($var_name && $var_type) {
|
|
|
|
|
$if_types[$var_name] = [['=getclass-' . $var_type]];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$type = $source->node_data->getType($whichclass_expr);
|
2019-07-06 04:57:38 +02:00
|
|
|
|
|
|
|
|
|
if ($type && $var_name) {
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($type->getAtomicTypes() as $type_part) {
|
2019-07-06 04:57:38 +02:00
|
|
|
|
if ($type_part instanceof Type\Atomic\TTemplateParamClass) {
|
|
|
|
|
$if_types[$var_name] = [['=' . $type_part->param_name]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-29 05:59:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2017-10-23 02:17:04 +02:00
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
$typed_value_position = self::hasTypedValueComparison($conditional, $source);
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($typed_value_position) {
|
|
|
|
|
if ($typed_value_position === self::ASSIGNMENT_TO_RIGHT) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$conditional->left,
|
2017-10-23 02:17:04 +02:00
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$other_type = $source->node_data->getType($conditional->left);
|
|
|
|
|
$var_type = $source->node_data->getType($conditional->right);
|
2018-06-26 00:02:05 +02:00
|
|
|
|
} elseif ($typed_value_position === self::ASSIGNMENT_TO_LEFT) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$conditional->right,
|
2017-10-23 02:17:04 +02:00
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$var_type = $source->node_data->getType($conditional->left);
|
|
|
|
|
$other_type = $source->node_data->getType($conditional->right);
|
2018-06-26 00:02:05 +02:00
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$typed_value_position value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($var_name && $var_type) {
|
|
|
|
|
$identical = $conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|
|| ($other_type
|
2020-06-18 15:31:38 +02:00
|
|
|
|
&& (($var_type->isString(true) && $other_type->isString(true))
|
|
|
|
|
|| ($var_type->isInt(true) && $other_type->isInt(true))
|
2018-06-26 00:02:05 +02:00
|
|
|
|
|| ($var_type->isFloat() && $other_type->isFloat())
|
|
|
|
|
)
|
2017-11-06 18:04:38 +01:00
|
|
|
|
);
|
2018-06-26 00:02:05 +02:00
|
|
|
|
|
|
|
|
|
if ($identical) {
|
2019-01-02 17:35:49 +01:00
|
|
|
|
$if_types[$var_name] = [['=' . $var_type->getAssertionString()]];
|
2017-11-06 18:04:38 +01:00
|
|
|
|
} else {
|
2019-01-02 17:35:49 +01:00
|
|
|
|
$if_types[$var_name] = [['~' . $var_type->getAssertionString()]];
|
2017-11-06 18:04:38 +01:00
|
|
|
|
}
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2017-10-23 02:17:04 +02:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if ($codebase
|
|
|
|
|
&& $other_type
|
2018-06-26 00:02:05 +02:00
|
|
|
|
&& $var_type
|
2020-07-14 16:08:31 +02:00
|
|
|
|
&& ($conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|
|| ($other_type->isString()
|
|
|
|
|
&& $var_type->isString())
|
|
|
|
|
)
|
2018-06-26 00:02:05 +02:00
|
|
|
|
) {
|
2019-01-02 03:00:34 +01:00
|
|
|
|
$parent_source = $source->getSource();
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer
|
2019-01-02 03:00:34 +01:00
|
|
|
|
&& (($var_type->isSingleStringLiteral()
|
|
|
|
|
&& $var_type->getSingleStringLiteral()->value === $this_class_name)
|
|
|
|
|
|| ($other_type->isSingleStringLiteral()
|
|
|
|
|
&& $other_type->getSingleStringLiteral()->value === $this_class_name))
|
|
|
|
|
) {
|
|
|
|
|
// do nothing
|
2020-07-22 01:40:35 +02:00
|
|
|
|
} elseif (!UnionTypeComparator::canExpressionTypesBeIdentical(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$other_type,
|
2018-09-09 18:20:49 +02:00
|
|
|
|
$var_type
|
2018-06-26 00:02:05 +02:00
|
|
|
|
)) {
|
|
|
|
|
if ($var_type->from_docblock || $other_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new DocblockTypeContradiction(
|
2019-01-13 20:40:21 +01:00
|
|
|
|
$var_type->getId() . ' does not contain ' . $other_type->getId(),
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' ' . $other_type->getId()
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TypeDoesNotContainType(
|
2018-12-06 04:29:06 +01:00
|
|
|
|
$var_type->getId() . ' cannot be identical to ' . $other_type->getId(),
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' ' . $other_type->getId()
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
2017-10-23 02:17:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2018-04-17 21:39:09 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$var_type = $source->node_data->getType($conditional->left);
|
|
|
|
|
$other_type = $source->node_data->getType($conditional->right);
|
2018-04-17 21:39:09 +02:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if ($codebase
|
|
|
|
|
&& $var_type
|
2018-06-26 00:02:05 +02:00
|
|
|
|
&& $other_type
|
|
|
|
|
&& $conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|
) {
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::canExpressionTypesBeIdentical($codebase, $var_type, $other_type)) {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TypeDoesNotContainType(
|
2018-12-06 04:29:06 +01:00
|
|
|
|
$var_type->getId() . ' cannot be identical to ' . $other_type->getId(),
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' ' . $other_type->getId()
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-17 21:39:09 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
return [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2018-04-17 21:39:09 +02:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param PhpParser\Node\Expr\BinaryOp\NotIdentical|PhpParser\Node\Expr\BinaryOp\NotEqual $conditional
|
|
|
|
|
*
|
2020-10-25 15:49:39 +01:00
|
|
|
|
* @return list<non-empty-array<string, non-empty-list<non-empty-list<string>>>>
|
2018-06-26 00:02:05 +02:00
|
|
|
|
*/
|
|
|
|
|
private static function scrapeInequalityAssertions(
|
|
|
|
|
PhpParser\Node\Expr\BinaryOp $conditional,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
?string $this_class_name,
|
2018-11-06 03:57:36 +01:00
|
|
|
|
FileSource $source,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
?Codebase $codebase = null,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
bool $inside_negation = false,
|
2020-07-10 22:49:45 +02:00
|
|
|
|
bool $cache = true,
|
|
|
|
|
bool $inside_conditional = true
|
2020-09-04 22:26:33 +02:00
|
|
|
|
): array {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$if_types = [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
|
2020-08-27 00:16:12 +02:00
|
|
|
|
$null_position = self::hasNullVariable($conditional, $source);
|
2018-06-26 00:02:05 +02:00
|
|
|
|
|
|
|
|
|
if ($null_position !== null) {
|
|
|
|
|
if ($null_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$base_conditional = $conditional->left;
|
|
|
|
|
} elseif ($null_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$base_conditional = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('Bad null variable position');
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
2019-12-08 16:17:40 +01:00
|
|
|
|
if ($base_conditional instanceof PhpParser\Node\Expr\Assign) {
|
|
|
|
|
$var_name = '=' . $var_name;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical) {
|
|
|
|
|
$if_types[$var_name] = [['!null']];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['!falsy']];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
&& ($var_type = $source->node_data->getType($base_conditional))
|
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical) {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$null_type = Type::getNull();
|
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::isContainedBy(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$var_type,
|
|
|
|
|
$null_type
|
2020-07-22 01:40:35 +02:00
|
|
|
|
) && !UnionTypeComparator::isContainedBy(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$null_type,
|
|
|
|
|
$var_type
|
|
|
|
|
)) {
|
|
|
|
|
if ($var_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantConditionGivenDocblockType(
|
2020-09-11 04:44:35 +02:00
|
|
|
|
'Docblock-defined type ' . $var_type . ' can never contain null',
|
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' null'
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantCondition(
|
2019-04-25 19:43:05 +02:00
|
|
|
|
$var_type . ' can never contain null',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' null'
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
2018-04-17 21:39:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$false_position = self::hasFalseVariable($conditional);
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($false_position) {
|
|
|
|
|
if ($false_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$base_conditional = $conditional->left;
|
|
|
|
|
} elseif ($false_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$base_conditional = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('Bad false variable position');
|
|
|
|
|
}
|
2017-12-14 02:06:19 +01:00
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical) {
|
|
|
|
|
$if_types[$var_name] = [['!false']];
|
2017-12-14 02:48:01 +01:00
|
|
|
|
} else {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$if_types[$var_name] = [['!falsy']];
|
2017-12-14 02:48:01 +01:00
|
|
|
|
}
|
2020-01-09 21:45:17 +01:00
|
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$base_assertions = null;
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($source instanceof StatementsAnalyzer && $cache) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$base_assertions = $source->node_data->getAssertions($base_conditional);
|
|
|
|
|
}
|
2017-12-14 02:48:01 +01:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($base_assertions === null) {
|
|
|
|
|
$base_assertions = self::scrapeAssertions(
|
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$inside_negation,
|
2020-07-10 22:49:45 +02:00
|
|
|
|
$cache,
|
|
|
|
|
$inside_conditional
|
2019-11-25 17:44:54 +01:00
|
|
|
|
);
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($source instanceof StatementsAnalyzer && $cache) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$source->node_data->setAssertions($base_conditional, $base_assertions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$notif_types = $base_assertions;
|
2018-06-26 00:02:05 +02:00
|
|
|
|
|
|
|
|
|
if (count($notif_types) === 1) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
$notif_types = $notif_types[0];
|
|
|
|
|
|
|
|
|
|
if (count($notif_types) === 1) {
|
2020-11-03 22:15:44 +01:00
|
|
|
|
$if_types = \Psalm\Internal\Algebra::negateTypes($notif_types);
|
2020-10-25 15:49:39 +01:00
|
|
|
|
}
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2017-12-14 02:48:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
&& ($var_type = $source->node_data->getType($base_conditional))
|
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical) {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$false_type = Type::getFalse();
|
2017-12-14 02:48:01 +01:00
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::isContainedBy(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$var_type,
|
|
|
|
|
$false_type
|
2020-07-22 01:40:35 +02:00
|
|
|
|
) && !UnionTypeComparator::isContainedBy(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$false_type,
|
|
|
|
|
$var_type
|
|
|
|
|
)) {
|
|
|
|
|
if ($var_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantConditionGivenDocblockType(
|
2020-09-11 04:44:35 +02:00
|
|
|
|
'Docblock-defined type ' . $var_type . ' can never contain false',
|
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' false'
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantCondition(
|
|
|
|
|
$var_type . ' can never contain false',
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' false'
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-14 02:48:01 +01:00
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$true_position = self::hasTrueVariable($conditional);
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($true_position) {
|
|
|
|
|
if ($true_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$base_conditional = $conditional->left;
|
|
|
|
|
} elseif ($true_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$base_conditional = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('Bad null variable position');
|
2017-12-14 02:06:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 15:38:56 +02:00
|
|
|
|
if ($base_conditional instanceof PhpParser\Node\Expr\FuncCall) {
|
|
|
|
|
$if_types = self::processFunctionCall(
|
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source,
|
2020-02-22 16:41:57 +01:00
|
|
|
|
$codebase,
|
2019-04-12 15:38:56 +02:00
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2019-04-12 15:38:56 +02:00
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
2017-12-05 18:14:10 +01:00
|
|
|
|
|
2019-04-12 15:38:56 +02:00
|
|
|
|
if ($var_name) {
|
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical) {
|
|
|
|
|
$if_types[$var_name] = [['!true']];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['falsy']];
|
|
|
|
|
}
|
2020-01-09 21:45:17 +01:00
|
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$base_assertions = null;
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($source instanceof StatementsAnalyzer && $cache) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$base_assertions = $source->node_data->getAssertions($base_conditional);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($base_assertions === null) {
|
|
|
|
|
$base_assertions = self::scrapeAssertions(
|
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$inside_negation,
|
2020-07-10 22:49:45 +02:00
|
|
|
|
$cache,
|
|
|
|
|
$inside_conditional
|
2019-11-25 17:44:54 +01:00
|
|
|
|
);
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($source instanceof StatementsAnalyzer && $cache) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$source->node_data->setAssertions($base_conditional, $base_assertions);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-05 18:14:10 +01:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$notif_types = $base_assertions;
|
2019-04-12 15:38:56 +02:00
|
|
|
|
|
|
|
|
|
if (count($notif_types) === 1) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
$notif_types = $notif_types[0];
|
|
|
|
|
|
|
|
|
|
if (count($notif_types) === 1) {
|
2020-11-03 22:15:44 +01:00
|
|
|
|
$if_types = \Psalm\Internal\Algebra::negateTypes($notif_types);
|
2020-10-25 15:49:39 +01:00
|
|
|
|
}
|
2019-04-12 15:38:56 +02:00
|
|
|
|
}
|
2017-12-05 18:14:10 +01:00
|
|
|
|
}
|
2020-10-25 15:49:39 +01:00
|
|
|
|
|
|
|
|
|
$if_types = $if_types ? [$if_types] : [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2017-12-05 18:14:10 +01:00
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
&& ($var_type = $source->node_data->getType($base_conditional))
|
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical) {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$true_type = Type::getTrue();
|
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::isContainedBy(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$var_type,
|
|
|
|
|
$true_type
|
2020-07-22 01:40:35 +02:00
|
|
|
|
) && !UnionTypeComparator::isContainedBy(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$true_type,
|
|
|
|
|
$var_type
|
|
|
|
|
)) {
|
|
|
|
|
if ($var_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantConditionGivenDocblockType(
|
2020-09-11 04:44:35 +02:00
|
|
|
|
'Docblock-defined type ' . $var_type . ' can never contain true',
|
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' true'
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantCondition(
|
|
|
|
|
$var_type . ' can never contain ' . $true_type,
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' true'
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-05 18:14:10 +01:00
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
return $if_types;
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$count = null;
|
|
|
|
|
$count_inequality_position = self::hasNotCountEqualityCheck($conditional, $count);
|
|
|
|
|
|
2020-08-30 17:32:01 +02:00
|
|
|
|
if ($count_inequality_position) {
|
|
|
|
|
if ($count_inequality_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$count_expr = $conditional->left;
|
|
|
|
|
} elseif ($count_inequality_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$count_expr = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$count_equality_position value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var PhpParser\Node\Expr\FuncCall $count_expr */
|
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
|
|
|
|
$count_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
|
|
|
|
if ($count) {
|
|
|
|
|
$if_types[$var_name] = [['!has-exactly-' . $count]];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['non-empty-countable']];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2020-08-30 17:32:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$empty_array_position = self::hasEmptyArrayVariable($conditional);
|
|
|
|
|
|
2019-03-02 21:18:29 +01:00
|
|
|
|
if ($empty_array_position !== null) {
|
|
|
|
|
if ($empty_array_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$base_conditional = $conditional->left;
|
|
|
|
|
} elseif ($empty_array_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$base_conditional = $conditional->right;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('Bad empty array variable position');
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2019-03-02 21:18:29 +01:00
|
|
|
|
$base_conditional,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical) {
|
|
|
|
|
$if_types[$var_name] = [['non-empty-countable']];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['!falsy']];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if ($codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
&& ($var_type = $source->node_data->getType($base_conditional))
|
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($conditional instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical) {
|
2019-03-02 21:18:29 +01:00
|
|
|
|
$empty_array_type = Type::getEmptyArray();
|
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::isContainedBy(
|
2019-03-02 21:18:29 +01:00
|
|
|
|
$codebase,
|
|
|
|
|
$var_type,
|
|
|
|
|
$empty_array_type
|
2020-07-22 01:40:35 +02:00
|
|
|
|
) && !UnionTypeComparator::isContainedBy(
|
2019-03-02 21:18:29 +01:00
|
|
|
|
$codebase,
|
|
|
|
|
$empty_array_type,
|
|
|
|
|
$var_type
|
|
|
|
|
)) {
|
|
|
|
|
if ($var_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantConditionGivenDocblockType(
|
2020-09-11 04:44:35 +02:00
|
|
|
|
'Docblock-defined type ' . $var_type->getId() . ' can never contain null',
|
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' null'
|
2019-03-02 21:18:29 +01:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantCondition(
|
2020-09-11 04:44:35 +02:00
|
|
|
|
$var_type->getId() . ' can never contain null',
|
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' null'
|
2019-03-02 21:18:29 +01:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2019-03-02 21:18:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$gettype_position = self::hasGetTypeCheck($conditional);
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($gettype_position) {
|
|
|
|
|
if ($gettype_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$whichclass_expr = $conditional->left;
|
|
|
|
|
$gettype_expr = $conditional->right;
|
|
|
|
|
} elseif ($gettype_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$whichclass_expr = $conditional->right;
|
|
|
|
|
$gettype_expr = $conditional->left;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$gettype_position value');
|
|
|
|
|
}
|
2017-01-31 07:35:44 +01:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
/** @var PhpParser\Node\Expr\FuncCall $gettype_expr */
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$gettype_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
2017-01-31 07:35:44 +01:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($whichclass_expr instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
|
$var_type = $whichclass_expr->value;
|
|
|
|
|
} elseif ($whichclass_expr instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $whichclass_expr->class instanceof PhpParser\Node\Name
|
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$var_type = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$whichclass_expr->class,
|
|
|
|
|
$source->getAliases()
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('Shouldn’t get here');
|
|
|
|
|
}
|
2017-01-31 07:35:44 +01:00
|
|
|
|
|
2019-01-05 22:23:18 +01:00
|
|
|
|
if (!isset(ClassLikeAnalyzer::GETTYPE_TYPES[$var_type])) {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new UnevaluatedCode(
|
|
|
|
|
'gettype cannot return this value',
|
|
|
|
|
new CodeLocation($source, $whichclass_expr)
|
|
|
|
|
)
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if ($var_name && $var_type) {
|
|
|
|
|
$if_types[$var_name] = [['!' . $var_type]];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-18 02:50:47 +01:00
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2017-02-18 02:50:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$get_debug_type_position = self::hasGetDebugTypeCheck($conditional);
|
|
|
|
|
|
2020-10-03 01:15:47 +02:00
|
|
|
|
if ($get_debug_type_position) {
|
|
|
|
|
if ($get_debug_type_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$whichclass_expr = $conditional->left;
|
|
|
|
|
$get_debug_type_expr = $conditional->right;
|
|
|
|
|
} elseif ($get_debug_type_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$whichclass_expr = $conditional->right;
|
|
|
|
|
$get_debug_type_expr = $conditional->left;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$gettype_position value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var PhpParser\Node\Expr\FuncCall $get_debug_type_expr */
|
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
|
|
|
|
$get_debug_type_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($whichclass_expr instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
|
$var_type = $whichclass_expr->value;
|
|
|
|
|
} elseif ($whichclass_expr instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $whichclass_expr->class instanceof PhpParser\Node\Name
|
|
|
|
|
) {
|
|
|
|
|
$var_type = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
|
|
|
|
$whichclass_expr->class,
|
|
|
|
|
$source->getAliases()
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('Shouldn’t get here');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($var_name && $var_type) {
|
|
|
|
|
if ($var_type === 'class@anonymous') {
|
|
|
|
|
$if_types[$var_name] = [['!=object']];
|
|
|
|
|
} elseif ($var_type === 'resource (closed)') {
|
|
|
|
|
$if_types[$var_name] = [['!closed-resource']];
|
|
|
|
|
} elseif (substr($var_type, 0, 10) === 'resource (') {
|
|
|
|
|
$if_types[$var_name] = [['!=resource']];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_name] = [['!' . $var_type]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2020-10-03 01:15:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 21:45:17 +01:00
|
|
|
|
if (!$source instanceof StatementsAnalyzer) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$getclass_position = self::hasGetClassCheck($conditional, $source);
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($getclass_position) {
|
|
|
|
|
if ($getclass_position === self::ASSIGNMENT_TO_RIGHT) {
|
|
|
|
|
$whichclass_expr = $conditional->left;
|
|
|
|
|
$getclass_expr = $conditional->right;
|
|
|
|
|
} elseif ($getclass_position === self::ASSIGNMENT_TO_LEFT) {
|
|
|
|
|
$whichclass_expr = $conditional->right;
|
|
|
|
|
$getclass_expr = $conditional->left;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$getclass_position value');
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 04:50:30 +02:00
|
|
|
|
if ($getclass_expr instanceof PhpParser\Node\Expr\FuncCall) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-07-18 04:50:30 +02:00
|
|
|
|
$getclass_expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$var_name = '$this';
|
|
|
|
|
}
|
2017-02-18 02:50:47 +01:00
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($whichclass_expr instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
|
$var_type = $whichclass_expr->value;
|
|
|
|
|
} elseif ($whichclass_expr instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $whichclass_expr->class instanceof PhpParser\Node\Name
|
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$var_type = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$whichclass_expr->class,
|
|
|
|
|
$source->getAliases()
|
|
|
|
|
);
|
2018-07-13 15:52:15 +02:00
|
|
|
|
|
2020-06-23 19:11:19 +02:00
|
|
|
|
if ($var_type === 'self' || $var_type === 'static') {
|
2018-07-13 15:52:15 +02:00
|
|
|
|
$var_type = $this_class_name;
|
2020-06-23 19:11:19 +02:00
|
|
|
|
} elseif ($var_type === 'parent') {
|
2018-07-13 15:52:15 +02:00
|
|
|
|
$var_type = null;
|
|
|
|
|
}
|
2017-02-18 02:50:47 +01:00
|
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$type = $source->node_data->getType($whichclass_expr);
|
2019-07-06 04:57:38 +02:00
|
|
|
|
|
|
|
|
|
if ($type && $var_name) {
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($type->getAtomicTypes() as $type_part) {
|
2019-07-06 04:57:38 +02:00
|
|
|
|
if ($type_part instanceof Type\Atomic\TTemplateParamClass) {
|
|
|
|
|
$if_types[$var_name] = [['!=' . $type_part->param_name]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2017-02-18 02:50:47 +01:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($var_type
|
2018-11-06 03:57:36 +01:00
|
|
|
|
&& ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$source,
|
|
|
|
|
$var_type,
|
|
|
|
|
new CodeLocation($source, $whichclass_expr),
|
2020-02-11 22:39:33 +01:00
|
|
|
|
null,
|
2020-03-26 17:35:27 +01:00
|
|
|
|
null,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$source->getSuppressedIssues(),
|
|
|
|
|
false
|
|
|
|
|
) === false
|
|
|
|
|
) {
|
|
|
|
|
// fall through
|
|
|
|
|
} else {
|
|
|
|
|
if ($var_name && $var_type) {
|
2018-11-16 16:13:52 +01:00
|
|
|
|
$if_types[$var_name] = [['!=getclass-' . $var_type]];
|
2017-02-18 02:50:47 +01:00
|
|
|
|
}
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
2017-02-18 02:50:47 +01:00
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:50:17 +01:00
|
|
|
|
$typed_value_position = self::hasTypedValueComparison($conditional, $source);
|
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
|
if ($typed_value_position) {
|
|
|
|
|
if ($typed_value_position === self::ASSIGNMENT_TO_RIGHT) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$conditional->left,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$other_type = $source->node_data->getType($conditional->left);
|
|
|
|
|
$var_type = $source->node_data->getType($conditional->right);
|
2018-06-26 00:02:05 +02:00
|
|
|
|
} elseif ($typed_value_position === self::ASSIGNMENT_TO_LEFT) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$conditional->right,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$var_type = $source->node_data->getType($conditional->left);
|
|
|
|
|
$other_type = $source->node_data->getType($conditional->right);
|
2018-06-26 00:02:05 +02:00
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException('$typed_value_position value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($var_type) {
|
2017-02-18 02:50:47 +01:00
|
|
|
|
if ($var_name) {
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$not_identical = $conditional instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical
|
|
|
|
|
|| ($other_type
|
|
|
|
|
&& (($var_type->isString() && $other_type->isString())
|
|
|
|
|
|| ($var_type->isInt() && $other_type->isInt())
|
|
|
|
|
|| ($var_type->isFloat() && $other_type->isFloat())
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($not_identical) {
|
2019-01-02 17:35:49 +01:00
|
|
|
|
$if_types[$var_name] = [['!=' . $var_type->getAssertionString()]];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
} else {
|
2019-01-02 17:35:49 +01:00
|
|
|
|
$if_types[$var_name] = [['!~' . $var_type->getAssertionString()]];
|
2018-06-26 00:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if ($codebase
|
|
|
|
|
&& $other_type
|
2018-06-26 00:02:05 +02:00
|
|
|
|
&& $conditional instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical
|
|
|
|
|
) {
|
2019-01-02 03:00:34 +01:00
|
|
|
|
$parent_source = $source->getSource();
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer
|
2019-01-02 03:00:34 +01:00
|
|
|
|
&& (($var_type->isSingleStringLiteral()
|
|
|
|
|
&& $var_type->getSingleStringLiteral()->value === $this_class_name)
|
|
|
|
|
|| ($other_type->isSingleStringLiteral()
|
|
|
|
|
&& $other_type->getSingleStringLiteral()->value === $this_class_name))
|
|
|
|
|
) {
|
|
|
|
|
// do nothing
|
2020-07-22 01:40:35 +02:00
|
|
|
|
} elseif (!UnionTypeComparator::canExpressionTypesBeIdentical(
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase,
|
2018-06-26 00:02:05 +02:00
|
|
|
|
$other_type,
|
2019-03-21 22:26:10 +01:00
|
|
|
|
$var_type
|
2018-06-26 00:02:05 +02:00
|
|
|
|
)) {
|
|
|
|
|
if ($var_type->from_docblock || $other_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new DocblockTypeContradiction(
|
2020-09-14 03:42:44 +02:00
|
|
|
|
$var_type . ' can never contain ' . $other_type->getId(),
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type . ' ' . $other_type
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantCondition(
|
|
|
|
|
$var_type->getId() . ' can never contain ' . $other_type->getId(),
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $conditional),
|
|
|
|
|
$var_type->getId() . ' ' . $other_type->getId()
|
2018-06-26 00:02:05 +02:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-18 02:50:47 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
return [];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-25 15:49:39 +01:00
|
|
|
|
* @return list<non-empty-array<string, non-empty-list<non-empty-list<string>>>>
|
2016-12-28 21:52:44 +01:00
|
|
|
|
*/
|
2019-02-26 07:03:33 +01:00
|
|
|
|
public static function processFunctionCall(
|
2016-12-28 21:52:44 +01:00
|
|
|
|
PhpParser\Node\Expr\FuncCall $expr,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
?string $this_class_name,
|
2018-02-23 21:39:33 +01:00
|
|
|
|
FileSource $source,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
?Codebase $codebase = null,
|
|
|
|
|
bool $negate = false
|
2020-09-04 22:26:33 +02:00
|
|
|
|
): array {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
$prefix = $negate ? '!' : '';
|
|
|
|
|
|
|
|
|
|
$first_var_name = isset($expr->args[0]->value)
|
2020-05-18 21:13:27 +02:00
|
|
|
|
? ExpressionIdentifier::getArrayVarId(
|
2016-12-28 21:52:44 +01:00
|
|
|
|
$expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
2017-01-07 20:35:07 +01:00
|
|
|
|
$source
|
2016-12-28 21:52:44 +01:00
|
|
|
|
)
|
|
|
|
|
: null;
|
|
|
|
|
|
2017-10-23 01:53:53 +02:00
|
|
|
|
$if_types = [];
|
|
|
|
|
|
2020-02-22 16:41:57 +01:00
|
|
|
|
$first_var_type = isset($expr->args[0]->value)
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
? $source->node_data->getType($expr->args[0]->value)
|
|
|
|
|
: null;
|
|
|
|
|
|
2016-12-28 21:52:44 +01:00
|
|
|
|
if (self::hasNullCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'null']];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
2020-02-02 18:26:28 +01:00
|
|
|
|
} elseif ($source instanceof StatementsAnalyzer && self::hasIsACheck($expr, $source)) {
|
2018-07-18 04:50:30 +02:00
|
|
|
|
if ($expr->args[0]->value instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $expr->args[0]->value->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& strtolower($expr->args[0]->value->name->name) === 'class'
|
|
|
|
|
&& $expr->args[0]->value->class instanceof PhpParser\Node\Name
|
|
|
|
|
&& count($expr->args[0]->value->class->parts) === 1
|
|
|
|
|
&& strtolower($expr->args[0]->value->class->parts[0]) === 'static'
|
|
|
|
|
) {
|
|
|
|
|
$first_var_name = '$this';
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-28 21:52:44 +01:00
|
|
|
|
if ($first_var_name) {
|
2020-02-02 18:26:28 +01:00
|
|
|
|
$first_arg = $expr->args[0]->value;
|
2018-05-30 16:13:55 +02:00
|
|
|
|
$second_arg = $expr->args[1]->value;
|
2019-01-05 22:58:34 +01:00
|
|
|
|
$third_arg = isset($expr->args[2]->value) ? $expr->args[2]->value : null;
|
2018-04-04 05:14:23 +02:00
|
|
|
|
|
2019-01-05 22:58:34 +01:00
|
|
|
|
if ($third_arg instanceof PhpParser\Node\Expr\ConstFetch) {
|
|
|
|
|
if (!in_array(strtolower($third_arg->name->parts[0]), ['true', 'false'])) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return [];
|
2018-04-04 05:14:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-05 22:58:34 +01:00
|
|
|
|
$third_arg_value = strtolower($third_arg->name->parts[0]);
|
|
|
|
|
} else {
|
|
|
|
|
$third_arg_value = $expr->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($expr->name->parts[0]) === 'is_subclass_of'
|
|
|
|
|
? 'true'
|
|
|
|
|
: 'false';
|
2018-04-04 05:14:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-05 22:58:34 +01:00
|
|
|
|
$is_a_prefix = $third_arg_value === 'true' ? 'isa-string-' : 'isa-';
|
|
|
|
|
|
2020-02-02 18:26:28 +01:00
|
|
|
|
if ($first_arg
|
|
|
|
|
&& ($first_arg_type = $source->node_data->getType($first_arg))
|
|
|
|
|
&& $first_arg_type->isSingleStringLiteral()
|
|
|
|
|
&& $source->getSource()->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer
|
|
|
|
|
&& $first_arg_type->getSingleStringLiteral()->value === $this_class_name
|
2018-01-23 21:46:14 +01:00
|
|
|
|
) {
|
2020-02-02 18:26:28 +01:00
|
|
|
|
// do nothing
|
|
|
|
|
} else {
|
|
|
|
|
if ($second_arg instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
|
$fq_class_name = $second_arg->value;
|
|
|
|
|
if ($fq_class_name[0] === '\\') {
|
|
|
|
|
$fq_class_name = substr($fq_class_name, 1);
|
|
|
|
|
}
|
2018-01-23 21:46:14 +01:00
|
|
|
|
|
2020-02-02 18:26:28 +01:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . $is_a_prefix . $fq_class_name]];
|
|
|
|
|
} elseif ($second_arg instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $second_arg->class instanceof PhpParser\Node\Name
|
|
|
|
|
&& $second_arg->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& strtolower($second_arg->name->name) === 'class'
|
2018-05-30 16:13:55 +02:00
|
|
|
|
) {
|
|
|
|
|
$class_node = $second_arg->class;
|
|
|
|
|
|
2020-03-24 23:58:15 +01:00
|
|
|
|
if ($class_node->parts === ['static']) {
|
|
|
|
|
if ($this_class_name) {
|
|
|
|
|
$if_types[$first_var_name] = [[$prefix . $is_a_prefix . $this_class_name . '&static']];
|
|
|
|
|
}
|
|
|
|
|
} elseif ($class_node->parts === ['self']) {
|
2018-06-15 16:33:51 +02:00
|
|
|
|
if ($this_class_name) {
|
|
|
|
|
$if_types[$first_var_name] = [[$prefix . $is_a_prefix . $this_class_name]];
|
|
|
|
|
}
|
2018-05-30 16:13:55 +02:00
|
|
|
|
} elseif ($class_node->parts === ['parent']) {
|
|
|
|
|
// do nothing
|
|
|
|
|
} else {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[
|
|
|
|
|
$prefix . $is_a_prefix
|
2018-11-06 03:57:36 +01:00
|
|
|
|
. ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$class_node,
|
|
|
|
|
$source->getAliases()
|
|
|
|
|
)
|
|
|
|
|
]];
|
2018-05-30 16:13:55 +02:00
|
|
|
|
}
|
2020-02-02 18:26:28 +01:00
|
|
|
|
} elseif (($second_arg_type = $source->node_data->getType($second_arg))
|
|
|
|
|
&& $second_arg_type->hasString()
|
|
|
|
|
) {
|
|
|
|
|
$vals = [];
|
|
|
|
|
|
|
|
|
|
foreach ($second_arg_type->getAtomicTypes() as $second_arg_atomic_type) {
|
|
|
|
|
if ($second_arg_atomic_type instanceof Type\Atomic\TTemplateParamClass) {
|
|
|
|
|
$vals[] = [$prefix . $is_a_prefix . $second_arg_atomic_type->param_name];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($vals) {
|
|
|
|
|
$if_types[$first_var_name] = $vals;
|
|
|
|
|
}
|
2018-01-23 21:46:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif (self::hasArrayCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'array']];
|
2020-02-22 16:41:57 +01:00
|
|
|
|
} elseif ($first_var_type
|
|
|
|
|
&& $codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
self::processIrreconcilableFunctionCall(
|
|
|
|
|
$first_var_type,
|
|
|
|
|
Type::getArray(),
|
|
|
|
|
$expr,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$negate
|
|
|
|
|
);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif (self::hasBoolCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'bool']];
|
2020-02-22 16:41:57 +01:00
|
|
|
|
} elseif ($first_var_type
|
|
|
|
|
&& $codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
self::processIrreconcilableFunctionCall(
|
|
|
|
|
$first_var_type,
|
|
|
|
|
Type::getBool(),
|
|
|
|
|
$expr,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$negate
|
|
|
|
|
);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif (self::hasStringCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'string']];
|
2020-02-22 16:41:57 +01:00
|
|
|
|
} elseif ($first_var_type
|
|
|
|
|
&& $codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
self::processIrreconcilableFunctionCall(
|
|
|
|
|
$first_var_type,
|
|
|
|
|
Type::getString(),
|
|
|
|
|
$expr,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$negate
|
|
|
|
|
);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif (self::hasObjectCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'object']];
|
2020-02-22 16:41:57 +01:00
|
|
|
|
} elseif ($first_var_type
|
|
|
|
|
&& $codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
self::processIrreconcilableFunctionCall(
|
|
|
|
|
$first_var_type,
|
|
|
|
|
Type::getObject(),
|
|
|
|
|
$expr,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$negate
|
|
|
|
|
);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif (self::hasNumericCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'numeric']];
|
2020-02-22 16:41:57 +01:00
|
|
|
|
} elseif ($first_var_type
|
|
|
|
|
&& $codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
self::processIrreconcilableFunctionCall(
|
|
|
|
|
$first_var_type,
|
|
|
|
|
Type::getNumeric(),
|
|
|
|
|
$expr,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$negate
|
|
|
|
|
);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif (self::hasIntCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'int']];
|
2020-02-22 16:41:57 +01:00
|
|
|
|
} elseif ($first_var_type
|
|
|
|
|
&& $codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
self::processIrreconcilableFunctionCall(
|
|
|
|
|
$first_var_type,
|
|
|
|
|
Type::getInt(),
|
|
|
|
|
$expr,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$negate
|
|
|
|
|
);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif (self::hasFloatCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'float']];
|
2020-02-22 16:41:57 +01:00
|
|
|
|
} elseif ($first_var_type
|
|
|
|
|
&& $codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
self::processIrreconcilableFunctionCall(
|
|
|
|
|
$first_var_type,
|
|
|
|
|
Type::getFloat(),
|
|
|
|
|
$expr,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$negate
|
|
|
|
|
);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif (self::hasResourceCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'resource']];
|
2020-02-22 16:41:57 +01:00
|
|
|
|
} elseif ($first_var_type
|
|
|
|
|
&& $codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
self::processIrreconcilableFunctionCall(
|
|
|
|
|
$first_var_type,
|
|
|
|
|
Type::getResource(),
|
|
|
|
|
$expr,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$negate
|
|
|
|
|
);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif (self::hasScalarCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'scalar']];
|
2020-02-22 16:41:57 +01:00
|
|
|
|
} elseif ($first_var_type
|
|
|
|
|
&& $codebase
|
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
|
|
|
|
) {
|
|
|
|
|
self::processIrreconcilableFunctionCall(
|
|
|
|
|
$first_var_type,
|
|
|
|
|
Type::getScalar(),
|
|
|
|
|
$expr,
|
|
|
|
|
$source,
|
|
|
|
|
$codebase,
|
|
|
|
|
$negate
|
|
|
|
|
);
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
} elseif (self::hasCallableCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'callable']];
|
2020-05-16 14:50:43 +02:00
|
|
|
|
} elseif ($expr->args[0]->value instanceof PhpParser\Node\Expr\Array_
|
|
|
|
|
&& isset($expr->args[0]->value->items[0], $expr->args[0]->value->items[1])
|
|
|
|
|
&& $expr->args[0]->value->items[1]->value instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$first_var_name_in_array_argument = ExpressionIdentifier::getArrayVarId(
|
2020-05-16 14:50:43 +02:00
|
|
|
|
$expr->args[0]->value->items[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
if ($first_var_name_in_array_argument) {
|
|
|
|
|
$if_types[$first_var_name_in_array_argument] = [
|
|
|
|
|
[$prefix . 'hasmethod-' . $expr->args[0]->value->items[1]->value->value]
|
|
|
|
|
];
|
|
|
|
|
}
|
2018-06-08 19:53:42 +02:00
|
|
|
|
}
|
2018-07-09 14:31:43 +02:00
|
|
|
|
} elseif (self::hasIterableCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'iterable']];
|
|
|
|
|
}
|
2019-06-07 21:49:10 +02:00
|
|
|
|
} elseif (self::hasCountableCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'countable']];
|
|
|
|
|
}
|
2019-05-28 19:16:09 +02:00
|
|
|
|
} elseif ($class_exists_check_type = self::hasClassExistsCheck($expr)) {
|
2018-11-12 18:03:55 +01:00
|
|
|
|
if ($first_var_name) {
|
2020-11-01 19:14:17 +01:00
|
|
|
|
$class_string_type = ($class_exists_check_type === 1 ? 'loaded-' : '') . 'class-string';
|
|
|
|
|
$if_types[$first_var_name] = [[$prefix . $class_string_type]];
|
2018-11-12 18:03:55 +01:00
|
|
|
|
}
|
2019-05-31 15:43:46 +02:00
|
|
|
|
} elseif ($class_exists_check_type = self::hasTraitExistsCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
2020-04-30 18:48:21 +02:00
|
|
|
|
if ($class_exists_check_type === 2 || $prefix) {
|
2019-05-31 15:43:46 +02:00
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'trait-string']];
|
2020-04-30 18:48:21 +02:00
|
|
|
|
} else {
|
2019-05-31 15:43:46 +02:00
|
|
|
|
$if_types[$first_var_name] = [['=trait-string']];
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-21 02:57:59 +02:00
|
|
|
|
} elseif (self::hasInterfaceExistsCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'interface-string']];
|
|
|
|
|
}
|
2019-04-10 00:09:57 +02:00
|
|
|
|
} elseif (self::hasFunctionExistsCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'callable-string']];
|
|
|
|
|
}
|
2019-08-16 17:33:58 +02:00
|
|
|
|
} elseif ($expr->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($expr->name->parts[0]) === 'method_exists'
|
|
|
|
|
&& isset($expr->args[1])
|
|
|
|
|
&& $expr->args[1]->value instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
) {
|
|
|
|
|
if ($first_var_name) {
|
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'hasmethod-' . $expr->args[1]->value->value]];
|
|
|
|
|
}
|
2019-11-25 17:44:54 +01:00
|
|
|
|
} elseif (self::hasInArrayCheck($expr) && $source instanceof StatementsAnalyzer) {
|
|
|
|
|
if ($first_var_name
|
|
|
|
|
&& ($second_arg_type = $source->node_data->getType($expr->args[1]->value))
|
|
|
|
|
) {
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($second_arg_type->getAtomicTypes() as $atomic_type) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
if ($atomic_type instanceof Type\Atomic\TArray
|
2020-08-30 17:44:14 +02:00
|
|
|
|
|| $atomic_type instanceof Type\Atomic\TKeyedArray
|
2020-11-19 15:26:41 +01:00
|
|
|
|
|| $atomic_type instanceof Type\Atomic\TList
|
2018-06-08 19:53:42 +02:00
|
|
|
|
) {
|
2020-11-19 15:26:41 +01:00
|
|
|
|
if ($atomic_type instanceof Type\Atomic\TList) {
|
|
|
|
|
$key_type = $atomic_type->type_param;
|
|
|
|
|
} elseif ($atomic_type instanceof Type\Atomic\TKeyedArray) {
|
|
|
|
|
$key_type = $atomic_type->getGenericKeyType();
|
|
|
|
|
} else {
|
|
|
|
|
$key_type = $atomic_type->type_params[1];
|
2018-06-08 19:53:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array_literal_types = array_merge(
|
2020-11-19 15:26:41 +01:00
|
|
|
|
$key_type->getLiteralStrings(),
|
|
|
|
|
$key_type->getLiteralInts(),
|
|
|
|
|
$key_type->getLiteralFloats()
|
2018-06-08 19:53:42 +02:00
|
|
|
|
);
|
|
|
|
|
|
2019-10-09 15:17:43 +02:00
|
|
|
|
if ($array_literal_types
|
2020-11-19 15:26:41 +01:00
|
|
|
|
&& count($key_type->getAtomicTypes())
|
2019-10-09 15:17:43 +02:00
|
|
|
|
) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$literal_assertions = [];
|
|
|
|
|
|
|
|
|
|
foreach ($array_literal_types as $array_literal_type) {
|
2018-11-16 16:13:52 +01:00
|
|
|
|
$literal_assertions[] = '=' . $array_literal_type->getId();
|
2018-06-08 19:53:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 15:26:41 +01:00
|
|
|
|
if ($key_type->isFalsable()) {
|
2020-09-20 00:12:14 +02:00
|
|
|
|
$literal_assertions[] = 'false';
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 15:26:41 +01:00
|
|
|
|
if ($key_type->isNullable()) {
|
2020-09-20 00:12:14 +02:00
|
|
|
|
$literal_assertions[] = 'null';
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-08 19:53:42 +02:00
|
|
|
|
if ($negate) {
|
2020-11-03 22:15:44 +01:00
|
|
|
|
$if_types = \Psalm\Internal\Algebra::negateTypes([
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$first_var_name => [$literal_assertions]
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$first_var_name] = [$literal_assertions];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
2018-03-18 00:03:46 +01:00
|
|
|
|
} elseif (self::hasArrayKeyExistsCheck($expr)) {
|
|
|
|
|
$array_root = isset($expr->args[1]->value)
|
2020-05-18 21:13:27 +02:00
|
|
|
|
? ExpressionIdentifier::getArrayVarId(
|
2018-03-18 00:03:46 +01:00
|
|
|
|
$expr->args[1]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
)
|
|
|
|
|
: null;
|
|
|
|
|
|
2018-03-18 00:28:01 +01:00
|
|
|
|
if ($first_var_name === null && isset($expr->args[0])) {
|
|
|
|
|
$first_arg = $expr->args[0];
|
|
|
|
|
|
|
|
|
|
if ($first_arg->value instanceof PhpParser\Node\Scalar\String_) {
|
2019-10-01 21:44:43 +02:00
|
|
|
|
$first_var_name = '\'' . $first_arg->value->value . '\'';
|
2018-03-18 00:28:01 +01:00
|
|
|
|
} elseif ($first_arg->value instanceof PhpParser\Node\Scalar\LNumber) {
|
|
|
|
|
$first_var_name = (string) $first_arg->value->value;
|
2018-03-18 00:03:46 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 15:03:36 +02:00
|
|
|
|
if ($expr->args[0]->value instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $expr->args[0]->value->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& $expr->args[0]->value->name->name !== 'class'
|
|
|
|
|
) {
|
|
|
|
|
$const_type = null;
|
|
|
|
|
|
|
|
|
|
if ($source instanceof StatementsAnalyzer) {
|
|
|
|
|
$const_type = $source->node_data->getType($expr->args[0]->value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($const_type) {
|
|
|
|
|
if ($const_type->isSingleStringLiteral()) {
|
|
|
|
|
$first_var_name = $const_type->getSingleStringLiteral()->value;
|
|
|
|
|
} elseif ($const_type->isSingleIntLiteral()) {
|
|
|
|
|
$first_var_name = (string) $const_type->getSingleIntLiteral()->value;
|
|
|
|
|
} else {
|
|
|
|
|
$first_var_name = null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$first_var_name = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-20 00:14:55 +01:00
|
|
|
|
if ($first_var_name !== null
|
|
|
|
|
&& $array_root
|
|
|
|
|
&& !strpos($first_var_name, '->')
|
|
|
|
|
&& !strpos($first_var_name, '[')
|
|
|
|
|
) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$array_root . '[' . $first_var_name . ']'] = [[$prefix . 'array-key-exists']];
|
2018-03-18 00:03:46 +01:00
|
|
|
|
}
|
2018-12-19 22:15:19 +01:00
|
|
|
|
} elseif (self::hasNonEmptyCountCheck($expr)) {
|
|
|
|
|
if ($first_var_name) {
|
|
|
|
|
$if_types[$first_var_name] = [[$prefix . 'non-empty-countable']];
|
|
|
|
|
}
|
2018-07-11 17:22:07 +02:00
|
|
|
|
} else {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return self::processCustomAssertion($expr, $this_class_name, $source, $negate);
|
2018-07-11 17:22:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $if_types ? [$if_types] : [];
|
2018-07-11 17:22:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-22 16:41:57 +01:00
|
|
|
|
private static function processIrreconcilableFunctionCall(
|
|
|
|
|
Type\Union $first_var_type,
|
|
|
|
|
Type\Union $expected_type,
|
|
|
|
|
PhpParser\Node\Expr $expr,
|
|
|
|
|
StatementsAnalyzer $source,
|
|
|
|
|
Codebase $codebase,
|
|
|
|
|
bool $negate
|
|
|
|
|
) : void {
|
2020-02-22 16:53:30 +01:00
|
|
|
|
if ($first_var_type->hasMixed()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
|
if (!UnionTypeComparator::isContainedBy(
|
2020-02-22 16:41:57 +01:00
|
|
|
|
$codebase,
|
|
|
|
|
$first_var_type,
|
|
|
|
|
$expected_type
|
2020-02-22 17:28:24 +01:00
|
|
|
|
)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-22 16:41:57 +01:00
|
|
|
|
|
2020-02-22 17:28:24 +01:00
|
|
|
|
if (!$negate) {
|
2020-02-22 16:41:57 +01:00
|
|
|
|
if ($first_var_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantConditionGivenDocblockType(
|
|
|
|
|
'Docblock type ' . $first_var_type . ' always contains ' . $expected_type,
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $expr),
|
|
|
|
|
$first_var_type . ' ' . $expected_type
|
2020-02-22 16:41:57 +01:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new RedundantCondition(
|
|
|
|
|
$first_var_type . ' always contains ' . $expected_type,
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $expr),
|
|
|
|
|
$first_var_type . ' ' . $expected_type
|
2020-02-22 16:41:57 +01:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-22 17:28:24 +01:00
|
|
|
|
} else {
|
2020-02-22 16:41:57 +01:00
|
|
|
|
if ($first_var_type->from_docblock) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new DocblockTypeContradiction(
|
|
|
|
|
$first_var_type . ' does not contain ' . $expected_type,
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $expr),
|
|
|
|
|
$first_var_type . ' ' . $expected_type
|
2020-02-22 16:41:57 +01:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new TypeDoesNotContainType(
|
|
|
|
|
$first_var_type . ' does not contain ' . $expected_type,
|
2020-09-11 04:44:35 +02:00
|
|
|
|
new CodeLocation($source, $expr),
|
|
|
|
|
$first_var_type . ' ' . $expected_type
|
2020-02-22 16:41:57 +01:00
|
|
|
|
),
|
|
|
|
|
$source->getSuppressedIssues()
|
|
|
|
|
)) {
|
|
|
|
|
// fall through
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 17:22:07 +02:00
|
|
|
|
/**
|
2019-05-23 00:09:36 +02:00
|
|
|
|
* @param PhpParser\Node\Expr\FuncCall|PhpParser\Node\Expr\MethodCall|PhpParser\Node\Expr\StaticCall $expr
|
2018-07-11 17:22:07 +02:00
|
|
|
|
*
|
2020-10-25 15:49:39 +01:00
|
|
|
|
* @return list<non-empty-array<string, non-empty-list<non-empty-list<string>>>>
|
2018-07-11 17:22:07 +02:00
|
|
|
|
*/
|
|
|
|
|
protected static function processCustomAssertion(
|
2020-09-07 01:36:47 +02:00
|
|
|
|
PhpParser\Node\Expr $expr,
|
|
|
|
|
?string $this_class_name,
|
2018-07-11 17:22:07 +02:00
|
|
|
|
FileSource $source,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
bool $negate = false
|
2020-09-04 22:26:33 +02:00
|
|
|
|
): array {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if (!$source instanceof StatementsAnalyzer) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$if_true_assertions = $source->node_data->getIfTrueAssertions($expr);
|
|
|
|
|
$if_false_assertions = $source->node_data->getIfFalseAssertions($expr);
|
|
|
|
|
|
|
|
|
|
if ($if_true_assertions === null && $if_false_assertions === null) {
|
2018-07-11 17:22:07 +02:00
|
|
|
|
return [];
|
|
|
|
|
}
|
2018-05-28 21:07:42 +02:00
|
|
|
|
|
2018-07-11 17:22:07 +02:00
|
|
|
|
$prefix = $negate ? '!' : '';
|
2018-05-28 21:07:42 +02:00
|
|
|
|
|
2018-07-11 17:22:07 +02:00
|
|
|
|
$first_var_name = isset($expr->args[0]->value)
|
2020-05-18 21:13:27 +02:00
|
|
|
|
? ExpressionIdentifier::getArrayVarId(
|
2018-07-11 17:22:07 +02:00
|
|
|
|
$expr->args[0]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
)
|
|
|
|
|
: null;
|
2018-05-28 21:07:42 +02:00
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
$anded_types = [];
|
2018-07-11 17:22:07 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($if_true_assertions) {
|
|
|
|
|
foreach ($if_true_assertions as $assertion) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
$if_types = [];
|
|
|
|
|
|
2020-08-12 23:03:41 +02:00
|
|
|
|
$assertion = clone $assertion;
|
|
|
|
|
|
|
|
|
|
foreach ($assertion->rule as $i => $and_rules) {
|
|
|
|
|
foreach ($and_rules as $j => $rule) {
|
|
|
|
|
if (strpos($rule, 'scalar-class-constant(') === 0) {
|
|
|
|
|
$codebase = $source->getCodebase();
|
|
|
|
|
|
|
|
|
|
$assertion->rule[$i][$j] = \Psalm\Internal\Type\TypeExpander::expandUnion(
|
|
|
|
|
$codebase,
|
|
|
|
|
Type::parseString(substr($rule, 22, -1)),
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
null
|
|
|
|
|
)->getId();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 21:07:42 +02:00
|
|
|
|
if (is_int($assertion->var_id) && isset($expr->args[$assertion->var_id])) {
|
|
|
|
|
if ($assertion->var_id === 0) {
|
|
|
|
|
$var_name = $first_var_name;
|
|
|
|
|
} else {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-05-28 21:07:42 +02:00
|
|
|
|
$expr->args[$assertion->var_id]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
2018-06-01 04:46:22 +02:00
|
|
|
|
if ($prefix === $assertion->rule[0][0][0]) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$var_name] = [[substr($assertion->rule[0][0], 1)]];
|
2018-06-01 03:59:55 +02:00
|
|
|
|
} else {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$var_name] = [[$prefix . $assertion->rule[0][0]]];
|
2018-06-01 03:59:55 +02:00
|
|
|
|
}
|
2018-05-28 21:07:42 +02:00
|
|
|
|
}
|
2019-07-07 21:06:03 +02:00
|
|
|
|
} elseif ($assertion->var_id === '$this' && $expr instanceof PhpParser\Node\Expr\MethodCall) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_id = ExpressionIdentifier::getArrayVarId(
|
2019-07-07 21:06:03 +02:00
|
|
|
|
$expr->var,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
|
if ($prefix === $assertion->rule[0][0][0]) {
|
|
|
|
|
$if_types[$var_id] = [[substr($assertion->rule[0][0], 1)]];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_id] = [[$prefix . $assertion->rule[0][0]]];
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-10 15:16:32 +02:00
|
|
|
|
} elseif (\is_string($assertion->var_id)
|
2019-08-10 15:12:02 +02:00
|
|
|
|
&& $expr instanceof PhpParser\Node\Expr\MethodCall
|
|
|
|
|
) {
|
|
|
|
|
if ($prefix === $assertion->rule[0][0][0]) {
|
|
|
|
|
$if_types[$assertion->var_id] = [[substr($assertion->rule[0][0], 1)]];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$assertion->var_id] = [[$prefix . $assertion->rule[0][0]]];
|
|
|
|
|
}
|
2018-05-28 21:07:42 +02:00
|
|
|
|
}
|
2020-10-25 15:49:39 +01:00
|
|
|
|
|
|
|
|
|
if ($if_types) {
|
|
|
|
|
$anded_types[] = $if_types;
|
|
|
|
|
}
|
2018-05-28 21:07:42 +02:00
|
|
|
|
}
|
2018-07-11 17:22:07 +02:00
|
|
|
|
}
|
2018-05-28 21:07:42 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($if_false_assertions) {
|
2018-05-28 21:07:42 +02:00
|
|
|
|
$negated_prefix = !$negate ? '!' : '';
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
foreach ($if_false_assertions as $assertion) {
|
2020-10-25 15:49:39 +01:00
|
|
|
|
$if_types = [];
|
|
|
|
|
|
2020-08-12 23:03:41 +02:00
|
|
|
|
$assertion = clone $assertion;
|
|
|
|
|
|
|
|
|
|
foreach ($assertion->rule as $i => $and_rules) {
|
|
|
|
|
foreach ($and_rules as $j => $rule) {
|
|
|
|
|
if (strpos($rule, 'scalar-class-constant(') === 0) {
|
|
|
|
|
$codebase = $source->getCodebase();
|
|
|
|
|
|
|
|
|
|
$assertion->rule[$i][$j] = \Psalm\Internal\Type\TypeExpander::expandUnion(
|
|
|
|
|
$codebase,
|
|
|
|
|
Type::parseString(substr($rule, 22, -1)),
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
null
|
|
|
|
|
)->getId();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 21:07:42 +02:00
|
|
|
|
if (is_int($assertion->var_id) && isset($expr->args[$assertion->var_id])) {
|
|
|
|
|
if ($assertion->var_id === 0) {
|
|
|
|
|
$var_name = $first_var_name;
|
|
|
|
|
} else {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_name = ExpressionIdentifier::getArrayVarId(
|
2018-05-28 21:07:42 +02:00
|
|
|
|
$expr->args[$assertion->var_id]->value,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($var_name) {
|
2018-06-01 04:46:22 +02:00
|
|
|
|
if ($negated_prefix === $assertion->rule[0][0][0]) {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$var_name] = [[substr($assertion->rule[0][0], 1)]];
|
2018-06-01 03:59:55 +02:00
|
|
|
|
} else {
|
2018-06-08 19:53:42 +02:00
|
|
|
|
$if_types[$var_name] = [[$negated_prefix . $assertion->rule[0][0]]];
|
2018-06-01 03:59:55 +02:00
|
|
|
|
}
|
2018-05-28 21:07:42 +02:00
|
|
|
|
}
|
2019-07-07 21:06:03 +02:00
|
|
|
|
} elseif ($assertion->var_id === '$this' && $expr instanceof PhpParser\Node\Expr\MethodCall) {
|
2020-05-18 21:13:27 +02:00
|
|
|
|
$var_id = ExpressionIdentifier::getArrayVarId(
|
2019-07-07 21:06:03 +02:00
|
|
|
|
$expr->var,
|
|
|
|
|
$this_class_name,
|
|
|
|
|
$source
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
|
if ($negated_prefix === $assertion->rule[0][0][0]) {
|
|
|
|
|
$if_types[$var_id] = [[substr($assertion->rule[0][0], 1)]];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$var_id] = [[$negated_prefix . $assertion->rule[0][0]]];
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-10 15:16:32 +02:00
|
|
|
|
} elseif (\is_string($assertion->var_id)
|
2019-08-10 15:12:02 +02:00
|
|
|
|
&& $expr instanceof PhpParser\Node\Expr\MethodCall
|
|
|
|
|
) {
|
|
|
|
|
if ($prefix === $assertion->rule[0][0][0]) {
|
|
|
|
|
$if_types[$assertion->var_id] = [[substr($assertion->rule[0][0], 1)]];
|
|
|
|
|
} else {
|
|
|
|
|
$if_types[$assertion->var_id] = [[$negated_prefix . $assertion->rule[0][0]]];
|
|
|
|
|
}
|
2018-05-28 21:07:42 +02:00
|
|
|
|
}
|
2020-10-25 15:49:39 +01:00
|
|
|
|
|
|
|
|
|
if ($if_types) {
|
|
|
|
|
$anded_types[] = $if_types;
|
|
|
|
|
}
|
2018-05-28 21:07:42 +02:00
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
2017-10-23 01:53:53 +02:00
|
|
|
|
|
2020-10-25 15:49:39 +01:00
|
|
|
|
return $anded_types;
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-10-09 15:17:43 +02:00
|
|
|
|
* @return list<string>
|
2016-12-28 21:52:44 +01:00
|
|
|
|
*/
|
|
|
|
|
protected static function getInstanceOfTypes(
|
|
|
|
|
PhpParser\Node\Expr\Instanceof_ $stmt,
|
2020-09-07 01:36:47 +02:00
|
|
|
|
?string $this_class_name,
|
2018-02-23 21:39:33 +01:00
|
|
|
|
FileSource $source
|
2020-09-12 17:24:05 +02:00
|
|
|
|
): array {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name) {
|
2017-11-15 03:56:29 +01:00
|
|
|
|
if (!in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$instanceof_class = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2016-12-28 21:52:44 +01:00
|
|
|
|
$stmt->class,
|
2017-07-25 22:11:02 +02:00
|
|
|
|
$source->getAliases()
|
2016-12-28 21:52:44 +01:00
|
|
|
|
);
|
|
|
|
|
|
2020-01-17 16:25:05 +01:00
|
|
|
|
if ($source instanceof StatementsAnalyzer) {
|
|
|
|
|
$codebase = $source->getCodebase();
|
|
|
|
|
$instanceof_class = $codebase->classlikes->getUnAliasedName($instanceof_class);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-27 20:10:33 +01:00
|
|
|
|
return [$instanceof_class];
|
2018-01-23 20:46:46 +01:00
|
|
|
|
} elseif ($this_class_name
|
|
|
|
|
&& (in_array(strtolower($stmt->class->parts[0]), ['self', 'static'], true))
|
|
|
|
|
) {
|
2019-06-20 14:37:57 +02:00
|
|
|
|
if ($stmt->class->parts[0] === 'static') {
|
2020-03-24 23:58:15 +01:00
|
|
|
|
return ['=' . $this_class_name . '&static'];
|
2019-06-20 14:37:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-27 20:10:33 +01:00
|
|
|
|
return [$this_class_name];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
2019-11-25 17:44:54 +01:00
|
|
|
|
} elseif ($source instanceof StatementsAnalyzer) {
|
|
|
|
|
$stmt_class_type = $source->node_data->getType($stmt->class);
|
2019-01-27 20:10:33 +01:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($stmt_class_type) {
|
|
|
|
|
$literal_class_strings = [];
|
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($stmt_class_type->getAtomicTypes() as $atomic_type) {
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if ($atomic_type instanceof Type\Atomic\TLiteralClassString) {
|
|
|
|
|
$literal_class_strings[] = $atomic_type->value;
|
|
|
|
|
} elseif ($atomic_type instanceof Type\Atomic\TTemplateParamClass) {
|
|
|
|
|
$literal_class_strings[] = $atomic_type->param_name;
|
|
|
|
|
}
|
2019-01-27 20:10:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
return $literal_class_strings;
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-27 20:10:33 +01:00
|
|
|
|
return [];
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
2020-09-11 04:44:35 +02:00
|
|
|
|
|
2020-08-26 23:58:01 +02:00
|
|
|
|
protected static function hasNullVariable(
|
|
|
|
|
PhpParser\Node\Expr\BinaryOp $conditional,
|
|
|
|
|
FileSource $source
|
2020-09-04 22:26:33 +02:00
|
|
|
|
): ?int {
|
2018-02-07 19:57:45 +01:00
|
|
|
|
if ($conditional->right instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
&& strtolower($conditional->right->name->parts[0]) === 'null'
|
|
|
|
|
) {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 19:57:45 +01:00
|
|
|
|
if ($conditional->left instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
&& strtolower($conditional->left->name->parts[0]) === 'null'
|
|
|
|
|
) {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 23:58:01 +02:00
|
|
|
|
if ($source instanceof StatementsAnalyzer
|
|
|
|
|
&& ($right_type = $source->node_data->getType($conditional->right))
|
|
|
|
|
&& $right_type->isNull()
|
|
|
|
|
) {
|
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-28 21:52:44 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
public static function hasFalseVariable(PhpParser\Node\Expr\BinaryOp $conditional): ?int
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
2018-02-07 19:57:45 +01:00
|
|
|
|
if ($conditional->right instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
&& strtolower($conditional->right->name->parts[0]) === 'false'
|
|
|
|
|
) {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 19:57:45 +01:00
|
|
|
|
if ($conditional->left instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
&& strtolower($conditional->left->name->parts[0]) === 'false'
|
|
|
|
|
) {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
public static function hasTrueVariable(PhpParser\Node\Expr\BinaryOp $conditional): ?int
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
2018-02-07 19:57:45 +01:00
|
|
|
|
if ($conditional->right instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
&& strtolower($conditional->right->name->parts[0]) === 'true'
|
|
|
|
|
) {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 19:57:45 +01:00
|
|
|
|
if ($conditional->left instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
&& strtolower($conditional->left->name->parts[0]) === 'true'
|
|
|
|
|
) {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasEmptyArrayVariable(PhpParser\Node\Expr\BinaryOp $conditional): ?int
|
2019-03-02 21:18:29 +01:00
|
|
|
|
{
|
|
|
|
|
if ($conditional->right instanceof PhpParser\Node\Expr\Array_
|
|
|
|
|
&& !$conditional->right->items
|
|
|
|
|
) {
|
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($conditional->left instanceof PhpParser\Node\Expr\Array_
|
|
|
|
|
&& !$conditional->left->items
|
|
|
|
|
) {
|
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-28 21:52:44 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return false|int
|
|
|
|
|
*/
|
|
|
|
|
protected static function hasGetTypeCheck(PhpParser\Node\Expr\BinaryOp $conditional)
|
|
|
|
|
{
|
2019-09-19 21:07:49 +02:00
|
|
|
|
if ($conditional->right instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->right->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->right->name->parts[0]) === 'gettype'
|
|
|
|
|
&& $conditional->right->args
|
|
|
|
|
&& $conditional->left instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
) {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 21:07:49 +02:00
|
|
|
|
if ($conditional->left instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->left->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->left->name->parts[0]) === 'gettype'
|
|
|
|
|
&& $conditional->left->args
|
|
|
|
|
&& $conditional->right instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
) {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-03 01:15:47 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return false|int
|
|
|
|
|
*/
|
|
|
|
|
protected static function hasGetDebugTypeCheck(PhpParser\Node\Expr\BinaryOp $conditional)
|
|
|
|
|
{
|
|
|
|
|
if ($conditional->right instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->right->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->right->name->parts[0]) === 'get_debug_type'
|
|
|
|
|
&& $conditional->right->args
|
2020-10-14 23:30:08 +02:00
|
|
|
|
&& ($conditional->left instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
|| $conditional->left instanceof PhpParser\Node\Expr\ClassConstFetch)
|
2020-10-03 01:15:47 +02:00
|
|
|
|
) {
|
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($conditional->left instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->left->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->left->name->parts[0]) === 'get_debug_type'
|
|
|
|
|
&& $conditional->left->args
|
2020-10-14 23:30:08 +02:00
|
|
|
|
&& ($conditional->right instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|
|| $conditional->right instanceof PhpParser\Node\Expr\ClassConstFetch)
|
2020-10-03 01:15:47 +02:00
|
|
|
|
) {
|
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-23 01:53:53 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return false|int
|
|
|
|
|
*/
|
2019-11-25 17:44:54 +01:00
|
|
|
|
protected static function hasGetClassCheck(
|
|
|
|
|
PhpParser\Node\Expr\BinaryOp $conditional,
|
|
|
|
|
FileSource $source
|
|
|
|
|
) {
|
|
|
|
|
if (!$source instanceof StatementsAnalyzer) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 04:50:30 +02:00
|
|
|
|
$right_get_class = $conditional->right instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->right->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->right->name->parts[0]) === 'get_class';
|
|
|
|
|
|
|
|
|
|
$right_static_class = $conditional->right instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $conditional->right->class instanceof PhpParser\Node\Name
|
|
|
|
|
&& $conditional->right->class->parts === ['static']
|
|
|
|
|
&& $conditional->right->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& strtolower($conditional->right->name->name) === 'class';
|
|
|
|
|
|
2018-11-29 05:59:43 +01:00
|
|
|
|
$left_class_string = $conditional->left instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $conditional->left->class instanceof PhpParser\Node\Name
|
|
|
|
|
&& $conditional->left->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& strtolower($conditional->left->name->name) === 'class';
|
2018-07-18 04:50:30 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$left_type = $source->node_data->getType($conditional->left);
|
2019-07-06 04:57:38 +02:00
|
|
|
|
|
|
|
|
|
$left_class_string_t = false;
|
|
|
|
|
|
|
|
|
|
if ($left_type && $left_type->isSingle()) {
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($left_type->getAtomicTypes() as $type_part) {
|
2019-07-06 04:57:38 +02:00
|
|
|
|
if ($type_part instanceof Type\Atomic\TClassString) {
|
|
|
|
|
$left_class_string_t = true;
|
2020-09-20 14:55:28 +02:00
|
|
|
|
break;
|
2019-07-06 04:57:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (($right_get_class || $right_static_class) && ($left_class_string || $left_class_string_t)) {
|
2017-10-23 01:53:53 +02:00
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 04:50:30 +02:00
|
|
|
|
$left_get_class = $conditional->left instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->left->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->left->name->parts[0]) === 'get_class';
|
|
|
|
|
|
|
|
|
|
$left_static_class = $conditional->left instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $conditional->left->class instanceof PhpParser\Node\Name
|
|
|
|
|
&& $conditional->left->class->parts === ['static']
|
|
|
|
|
&& $conditional->left->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& strtolower($conditional->left->name->name) === 'class';
|
|
|
|
|
|
2018-11-29 05:59:43 +01:00
|
|
|
|
$right_class_string = $conditional->right instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $conditional->right->class instanceof PhpParser\Node\Name
|
|
|
|
|
&& $conditional->right->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& strtolower($conditional->right->name->name) === 'class';
|
2018-07-18 04:50:30 +02:00
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$right_type = $source->node_data->getType($conditional->right);
|
2019-07-06 04:57:38 +02:00
|
|
|
|
|
|
|
|
|
$right_class_string_t = false;
|
|
|
|
|
|
|
|
|
|
if ($right_type && $right_type->isSingle()) {
|
2020-01-04 18:20:26 +01:00
|
|
|
|
foreach ($right_type->getAtomicTypes() as $type_part) {
|
2019-07-06 04:57:38 +02:00
|
|
|
|
if ($type_part instanceof Type\Atomic\TClassString) {
|
|
|
|
|
$right_class_string_t = true;
|
2020-09-20 14:55:28 +02:00
|
|
|
|
break;
|
2019-07-06 04:57:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (($left_get_class || $left_static_class) && ($right_class_string || $right_class_string_t)) {
|
2017-10-23 01:53:53 +02:00
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-19 22:15:19 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return false|int
|
|
|
|
|
*/
|
2019-11-26 22:34:13 +01:00
|
|
|
|
protected static function hasNonEmptyCountEqualityCheck(
|
|
|
|
|
PhpParser\Node\Expr\BinaryOp $conditional,
|
|
|
|
|
?int &$min_count
|
|
|
|
|
) {
|
2018-12-19 22:15:19 +01:00
|
|
|
|
$left_count = $conditional->left instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->left->name instanceof PhpParser\Node\Name
|
2019-09-19 21:07:49 +02:00
|
|
|
|
&& strtolower($conditional->left->name->parts[0]) === 'count'
|
|
|
|
|
&& $conditional->left->args;
|
2018-12-19 22:15:19 +01:00
|
|
|
|
|
|
|
|
|
$operator_greater_than_or_equal =
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Equal
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
|
|
|
|
|
|
2019-11-26 22:34:13 +01:00
|
|
|
|
if ($left_count
|
|
|
|
|
&& $conditional->right instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|
&& $operator_greater_than_or_equal
|
|
|
|
|
&& $conditional->right->value >= (
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater
|
|
|
|
|
? 0
|
|
|
|
|
: 1
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$min_count = $conditional->right->value +
|
|
|
|
|
($conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater ? 1 : 0);
|
|
|
|
|
|
2018-12-19 22:15:19 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$right_count = $conditional->right instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->right->name instanceof PhpParser\Node\Name
|
2019-09-19 21:07:49 +02:00
|
|
|
|
&& strtolower($conditional->right->name->parts[0]) === 'count'
|
|
|
|
|
&& $conditional->right->args;
|
2018-12-19 22:15:19 +01:00
|
|
|
|
|
|
|
|
|
$operator_less_than_or_equal =
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Equal
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
|
|
|
|
|
|
2019-11-26 22:34:13 +01:00
|
|
|
|
if ($right_count
|
|
|
|
|
&& $conditional->left instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|
&& $operator_less_than_or_equal
|
|
|
|
|
&& $conditional->left->value >= (
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller ? 0 : 1
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$min_count = $conditional->left->value +
|
|
|
|
|
($conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller ? 1 : 0);
|
|
|
|
|
|
2020-08-30 17:32:01 +02:00
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return false|int
|
|
|
|
|
*/
|
|
|
|
|
protected static function hasLessThanCountEqualityCheck(
|
|
|
|
|
PhpParser\Node\Expr\BinaryOp $conditional,
|
|
|
|
|
?int &$max_count
|
|
|
|
|
) {
|
|
|
|
|
$left_count = $conditional->left instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->left->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->left->name->parts[0]) === 'count'
|
|
|
|
|
&& $conditional->left->args;
|
|
|
|
|
|
2020-09-12 22:13:13 +02:00
|
|
|
|
$operator_less_than_or_equal =
|
2020-08-30 17:32:01 +02:00
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\SmallerOrEqual
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller;
|
|
|
|
|
|
|
|
|
|
if ($left_count
|
|
|
|
|
&& $conditional->right instanceof PhpParser\Node\Scalar\LNumber
|
2020-09-12 22:13:13 +02:00
|
|
|
|
&& $operator_less_than_or_equal
|
2020-08-30 17:32:01 +02:00
|
|
|
|
) {
|
|
|
|
|
$max_count = $conditional->right->value -
|
2020-09-12 17:33:26 +02:00
|
|
|
|
($conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller ? 1 : 0);
|
2020-08-30 17:32:01 +02:00
|
|
|
|
|
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$right_count = $conditional->right instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->right->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->right->name->parts[0]) === 'count'
|
|
|
|
|
&& $conditional->right->args;
|
|
|
|
|
|
2020-09-12 22:13:13 +02:00
|
|
|
|
$operator_greater_than_or_equal =
|
2020-08-30 17:32:01 +02:00
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\GreaterOrEqual
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater;
|
|
|
|
|
|
|
|
|
|
if ($right_count
|
|
|
|
|
&& $conditional->left instanceof PhpParser\Node\Scalar\LNumber
|
2020-09-12 22:13:13 +02:00
|
|
|
|
&& $operator_greater_than_or_equal
|
2020-08-30 17:32:01 +02:00
|
|
|
|
) {
|
|
|
|
|
$max_count = $conditional->left->value -
|
2020-09-12 17:33:26 +02:00
|
|
|
|
($conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater ? 1 : 0);
|
2020-08-30 17:32:01 +02:00
|
|
|
|
|
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param PhpParser\Node\Expr\BinaryOp\NotIdentical|PhpParser\Node\Expr\BinaryOp\NotEqual $conditional
|
|
|
|
|
*
|
|
|
|
|
* @return false|int
|
|
|
|
|
*/
|
|
|
|
|
protected static function hasNotCountEqualityCheck(
|
|
|
|
|
PhpParser\Node\Expr\BinaryOp $conditional,
|
|
|
|
|
?int &$count
|
|
|
|
|
) {
|
|
|
|
|
$left_count = $conditional->left instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->left->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->left->name->parts[0]) === 'count'
|
|
|
|
|
&& $conditional->left->args;
|
|
|
|
|
|
|
|
|
|
if ($left_count && $conditional->right instanceof PhpParser\Node\Scalar\LNumber) {
|
|
|
|
|
$count = $conditional->right->value;
|
|
|
|
|
|
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$right_count = $conditional->right instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->right->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->right->name->parts[0]) === 'count'
|
|
|
|
|
&& $conditional->right->args;
|
|
|
|
|
|
|
|
|
|
if ($right_count && $conditional->left instanceof PhpParser\Node\Scalar\LNumber) {
|
|
|
|
|
$count = $conditional->left->value;
|
|
|
|
|
|
2019-02-21 23:17:10 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-27 00:29:17 +02:00
|
|
|
|
* @return false|int
|
|
|
|
|
*/
|
|
|
|
|
protected static function hasPositiveNumberCheck(
|
|
|
|
|
PhpParser\Node\Expr\BinaryOp $conditional,
|
|
|
|
|
?int &$min_count
|
|
|
|
|
) {
|
|
|
|
|
$operator_greater_than_or_equal =
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Equal
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
|
|
|
|
|
|
|
|
|
|
if ($conditional->right instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|
&& $operator_greater_than_or_equal
|
|
|
|
|
&& $conditional->right->value >= (
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater
|
|
|
|
|
? 0
|
|
|
|
|
: 1
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$min_count = $conditional->right->value +
|
|
|
|
|
($conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater ? 1 : 0);
|
|
|
|
|
|
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$operator_less_than_or_equal =
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Equal
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
|
|
|
|
|
|
|
|
|
|
if ($conditional->left instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|
&& $operator_less_than_or_equal
|
|
|
|
|
&& $conditional->left->value >= (
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller ? 0 : 1
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
$min_count = $conditional->left->value +
|
|
|
|
|
($conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller ? 1 : 0);
|
|
|
|
|
|
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-02-21 23:17:10 +01:00
|
|
|
|
* @return false|int
|
|
|
|
|
*/
|
|
|
|
|
protected static function hasReconcilableNonEmptyCountEqualityCheck(PhpParser\Node\Expr\BinaryOp $conditional)
|
|
|
|
|
{
|
|
|
|
|
$left_count = $conditional->left instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->left->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->left->name->parts[0]) === 'count';
|
|
|
|
|
|
|
|
|
|
$right_number = $conditional->right instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|
&& $conditional->right->value === (
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater ? 0 : 1);
|
|
|
|
|
|
|
|
|
|
$operator_greater_than_or_equal =
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Equal
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
|
|
|
|
|
|
|
|
|
|
if ($left_count && $right_number && $operator_greater_than_or_equal) {
|
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$right_count = $conditional->right instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|
&& $conditional->right->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($conditional->right->name->parts[0]) === 'count';
|
|
|
|
|
|
|
|
|
|
$left_number = $conditional->left instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|
&& $conditional->left->value === (
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller ? 0 : 1);
|
|
|
|
|
|
|
|
|
|
$operator_less_than_or_equal =
|
|
|
|
|
$conditional instanceof PhpParser\Node\Expr\BinaryOp\Identical
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Equal
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller
|
|
|
|
|
|| $conditional instanceof PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
|
|
|
|
|
|
|
|
|
|
if ($right_count && $left_number && $operator_less_than_or_equal) {
|
2018-12-19 22:15:19 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-28 21:52:44 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return false|int
|
|
|
|
|
*/
|
2019-11-25 17:44:54 +01:00
|
|
|
|
protected static function hasTypedValueComparison(
|
|
|
|
|
PhpParser\Node\Expr\BinaryOp $conditional,
|
|
|
|
|
FileSource $source
|
|
|
|
|
) {
|
|
|
|
|
if (!$source instanceof StatementsAnalyzer) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (($right_type = $source->node_data->getType($conditional->right))
|
2019-05-02 06:50:35 +02:00
|
|
|
|
&& ((!$conditional->right instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|
&& !$conditional->right instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
|
&& !$conditional->right instanceof PhpParser\Node\Expr\StaticPropertyFetch)
|
|
|
|
|
|| $conditional->left instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|
|| $conditional->left instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
|
|| $conditional->left instanceof PhpParser\Node\Expr\StaticPropertyFetch)
|
2020-01-04 18:20:26 +01:00
|
|
|
|
&& count($right_type->getAtomicTypes()) === 1
|
2019-11-25 17:44:54 +01:00
|
|
|
|
&& !$right_type->hasMixed()
|
2018-01-09 21:05:48 +01:00
|
|
|
|
) {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
|
if (($left_type = $source->node_data->getType($conditional->left))
|
2019-05-02 06:50:35 +02:00
|
|
|
|
&& !$conditional->left instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|
&& !$conditional->left instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
|
&& !$conditional->left instanceof PhpParser\Node\Expr\StaticPropertyFetch
|
2020-01-04 18:20:26 +01:00
|
|
|
|
&& count($left_type->getAtomicTypes()) === 1
|
2019-11-25 17:44:54 +01:00
|
|
|
|
&& !$left_type->hasMixed()
|
2018-01-09 21:05:48 +01:00
|
|
|
|
) {
|
2016-12-28 21:52:44 +01:00
|
|
|
|
return self::ASSIGNMENT_TO_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasNullCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && strtolower($stmt->name->parts[0]) === 'is_null') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-09-11 04:44:35 +02:00
|
|
|
|
|
2020-02-02 18:26:28 +01:00
|
|
|
|
protected static function hasIsACheck(
|
|
|
|
|
PhpParser\Node\Expr\FuncCall $stmt,
|
|
|
|
|
StatementsAnalyzer $source
|
2020-09-04 22:26:33 +02:00
|
|
|
|
): bool {
|
2018-01-23 21:46:14 +01:00
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name
|
2019-01-05 22:58:34 +01:00
|
|
|
|
&& (strtolower($stmt->name->parts[0]) === 'is_a'
|
|
|
|
|
|| strtolower($stmt->name->parts[0]) === 'is_subclass_of')
|
2018-01-23 21:46:14 +01:00
|
|
|
|
&& isset($stmt->args[1])
|
|
|
|
|
) {
|
2018-01-30 22:45:29 +01:00
|
|
|
|
$second_arg = $stmt->args[1]->value;
|
2018-01-23 21:46:14 +01:00
|
|
|
|
|
2018-01-30 22:45:29 +01:00
|
|
|
|
if ($second_arg instanceof PhpParser\Node\Scalar\String_
|
2018-01-23 21:46:14 +01:00
|
|
|
|
|| (
|
2018-01-30 22:45:29 +01:00
|
|
|
|
$second_arg instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|
&& $second_arg->class instanceof PhpParser\Node\Name
|
2018-04-17 18:16:25 +02:00
|
|
|
|
&& $second_arg->name instanceof PhpParser\Node\Identifier
|
|
|
|
|
&& strtolower($second_arg->name->name) === 'class'
|
2018-01-23 21:46:14 +01:00
|
|
|
|
)
|
2020-02-02 18:26:28 +01:00
|
|
|
|
|| (($second_arg_type = $source->node_data->getType($second_arg))
|
|
|
|
|
&& $second_arg_type->hasString())
|
2018-01-23 21:46:14 +01:00
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasArrayCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && strtolower($stmt->name->parts[0]) === 'is_array') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasStringCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && strtolower($stmt->name->parts[0]) === 'is_string') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasBoolCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && strtolower($stmt->name->parts[0]) === 'is_bool') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasObjectCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $stmt->name->parts === ['is_object']) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasNumericCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $stmt->name->parts === ['is_numeric']) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-07-09 14:31:43 +02:00
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasIterableCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2018-07-09 14:31:43 +02:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && strtolower($stmt->name->parts[0]) === 'is_iterable') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-11-12 18:03:55 +01:00
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasCountableCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2019-06-07 21:49:10 +02:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && strtolower($stmt->name->parts[0]) === 'is_countable') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-12 18:03:55 +01:00
|
|
|
|
/**
|
2019-05-28 19:16:09 +02:00
|
|
|
|
* @return 0|1|2
|
2018-11-12 18:03:55 +01:00
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasClassExistsCheck(PhpParser\Node\Expr\FuncCall $stmt): int
|
2019-05-21 02:57:59 +02:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($stmt->name->parts[0]) === 'class_exists'
|
|
|
|
|
) {
|
2019-05-26 22:26:47 +02:00
|
|
|
|
if (!isset($stmt->args[1])) {
|
2019-05-28 19:16:09 +02:00
|
|
|
|
return 2;
|
2019-05-26 19:16:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-26 22:26:47 +02:00
|
|
|
|
$second_arg = $stmt->args[1]->value;
|
2019-05-26 19:16:44 +02:00
|
|
|
|
|
|
|
|
|
if ($second_arg instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
&& strtolower($second_arg->name->parts[0]) === 'true'
|
|
|
|
|
) {
|
2019-05-28 19:16:09 +02:00
|
|
|
|
return 2;
|
2019-05-26 19:16:44 +02:00
|
|
|
|
}
|
2019-05-28 19:16:09 +02:00
|
|
|
|
|
|
|
|
|
return 1;
|
2019-05-21 02:57:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 19:16:09 +02:00
|
|
|
|
return 0;
|
2019-05-21 02:57:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 15:43:46 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return 0|1|2
|
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasTraitExistsCheck(PhpParser\Node\Expr\FuncCall $stmt): int
|
2019-05-31 15:43:46 +02:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& strtolower($stmt->name->parts[0]) === 'trait_exists'
|
|
|
|
|
) {
|
|
|
|
|
if (!isset($stmt->args[1])) {
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$second_arg = $stmt->args[1]->value;
|
|
|
|
|
|
|
|
|
|
if ($second_arg instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
&& strtolower($second_arg->name->parts[0]) === 'true'
|
|
|
|
|
) {
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasInterfaceExistsCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2018-11-12 18:03:55 +01:00
|
|
|
|
{
|
2019-05-19 21:56:04 +02:00
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name
|
2019-05-21 02:57:59 +02:00
|
|
|
|
&& strtolower($stmt->name->parts[0]) === 'interface_exists'
|
2019-05-19 21:56:04 +02:00
|
|
|
|
) {
|
2018-11-12 18:03:55 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasFunctionExistsCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2019-04-10 00:09:57 +02:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && strtolower($stmt->name->parts[0]) === 'function_exists') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasIntCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name &&
|
|
|
|
|
($stmt->name->parts === ['is_int'] ||
|
2017-05-25 04:07:49 +02:00
|
|
|
|
$stmt->name->parts === ['is_integer'] ||
|
2016-12-28 21:52:44 +01:00
|
|
|
|
$stmt->name->parts === ['is_long'])
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasFloatCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name &&
|
|
|
|
|
($stmt->name->parts === ['is_float'] ||
|
|
|
|
|
$stmt->name->parts === ['is_real'] ||
|
|
|
|
|
$stmt->name->parts === ['is_double'])
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasResourceCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $stmt->name->parts === ['is_resource']) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasScalarCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $stmt->name->parts === ['is_scalar']) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasCallableCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2016-12-28 21:52:44 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $stmt->name->parts === ['is_callable']) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-03-18 00:03:46 +01:00
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasInArrayCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2018-06-08 19:53:42 +02:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& $stmt->name->parts === ['in_array']
|
|
|
|
|
&& isset($stmt->args[2])
|
|
|
|
|
) {
|
|
|
|
|
$second_arg = $stmt->args[2]->value;
|
|
|
|
|
|
|
|
|
|
if ($second_arg instanceof PhpParser\Node\Expr\ConstFetch
|
|
|
|
|
&& strtolower($second_arg->name->parts[0]) === 'true'
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasNonEmptyCountCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2018-12-19 22:15:19 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name
|
|
|
|
|
&& $stmt->name->parts === ['count']
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
|
protected static function hasArrayKeyExistsCheck(PhpParser\Node\Expr\FuncCall $stmt): bool
|
2018-03-18 00:03:46 +01:00
|
|
|
|
{
|
|
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $stmt->name->parts === ['array_key_exists']) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-12-28 21:52:44 +01:00
|
|
|
|
}
|