Skip to content

Commit

Permalink
Simplify lots of usage checks
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jun 25, 2021
1 parent 67d68a5 commit 19cc4cb
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 62 deletions.
11 changes: 11 additions & 0 deletions src/Psalm/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,15 @@ public function mergeFunctionExceptions(
$this->possibly_thrown_exceptions[$possibly_thrown_exception][$hash] = $codelocation;
}
}

public function insideUse(): bool
{
return $this->inside_assignment
|| $this->inside_return
|| $this->inside_call
|| $this->inside_general_use
|| $this->inside_conditional
|| $this->inside_throw
|| $this->inside_isset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -935,12 +935,7 @@ public static function verifyType(
$statements_analyzer,
$statements_analyzer->getFilePath(),
true,
$context->inside_return
|| $context->inside_call
|| $context->inside_general_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
$context->insideUse()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -992,11 +992,7 @@ private static function checkFunctionCallPurity(
* If a function is pure, and has the return type of 'no-return',
* it's okay to dismiss it's return value.
*/
if (!$context->inside_assignment
&& !$context->inside_call
&& !$context->inside_return
&& !$context->inside_general_use
&& !$context->inside_throw
if (!$context->insideUse()
&& !self::callUsesByReferenceArguments($function_call_info, $stmt)
&& !(
$function_call_info->function_storage &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,7 @@ public static function analyze(
: null,
$statements_analyzer->getFilePath(),
false,
$context->inside_return
|| $context->inside_call
|| $context->inside_general_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
$context->insideUse()
);

$fake_method_exists = false;
Expand Down Expand Up @@ -327,12 +322,7 @@ public static function analyze(
: null,
$statements_analyzer->getFilePath(),
true,
$context->inside_return
|| $context->inside_call
|| $context->inside_general_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
$context->insideUse()
)
) {
$new_call_context = MissingMethodCallHandler::handleMagicMethod(
Expand Down Expand Up @@ -734,12 +724,7 @@ private static function handleTemplatedMixins(
: null,
$statements_analyzer->getFilePath(),
true,
$context->inside_return
|| $context->inside_call
|| $context->inside_general_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
$context->insideUse()
)) {
$lhs_type_part = clone $lhs_type_part_new;
$class_storage = $mixin_class_storage;
Expand Down Expand Up @@ -807,12 +792,7 @@ private static function handleRegularMixins(
: null,
$statements_analyzer->getFilePath(),
true,
$context->inside_return
|| $context->inside_call
|| $context->inside_general_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
$context->insideUse()
)) {
$mixin_declaring_class_storage = $codebase->classlike_storage_provider->get(
$class_storage->mixin_declaring_fqcln
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ public static function analyze(

$context->inside_call = true;

$was_inside_general_use = $context->inside_general_use;
$context->inside_general_use = true;

$existing_stmt_var_type = null;

if (!$real_method_call) {
Expand All @@ -70,7 +67,6 @@ public static function analyze(
}

$context->inside_call = $was_inside_call;
$context->inside_general_use = $was_inside_general_use;

if ($stmt->var instanceof PhpParser\Node\Expr\Variable) {
if (is_string($stmt->var->name) && $stmt->var->name === 'this' && !$statements_analyzer->getFQCLN()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,7 @@ private static function handleNamedCall(
$statements_analyzer,
$statements_analyzer->getFilePath(),
false,
$context->inside_return
|| $context->inside_call
|| $context->inside_general_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
$context->insideUse()
);

$fake_method_exists = false;
Expand Down Expand Up @@ -348,12 +343,7 @@ private static function handleNamedCall(
: null,
$statements_analyzer->getFilePath(),
true,
$context->inside_return
|| $context->inside_call
|| $context->inside_general_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
$context->insideUse()
)) {
$mixin_candidates = [];
foreach ($class_storage->templatedMixins as $mixin_candidate) {
Expand Down Expand Up @@ -473,12 +463,7 @@ private static function handleNamedCall(
: null,
$statements_analyzer->getFilePath(),
true,
$context->inside_return
|| $context->inside_call
|| $context->inside_general_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
$context->insideUse()
)) {
if ($codebase->methods->return_type_provider->has($fq_class_name)) {
$return_type_candidate = $codebase->methods->return_type_provider->getReturnType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,12 @@ private static function addDataFlowToVariable(

if ($statements_analyzer->data_flow_graph
&& $codebase->find_unused_variables
&& ($context->inside_call
|| $context->inside_return
|| $context->inside_conditional
&& ($context->inside_return
|| $context->inside_call
|| $context->inside_general_use
|| $context->inside_isset
|| $context->inside_throw)
|| $context->inside_conditional
|| $context->inside_throw
|| $context->inside_isset)
) {
if (!$stmt_type->parent_nodes) {
$assignment_node = DataFlowNode::getForAssignment(
Expand Down

0 comments on commit 19cc4cb

Please sign in to comment.