Skip to content

Commit

Permalink
Merge pull request #6535 from orklah/SA
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
orklah authored Sep 25, 2021
2 parents f279f6c + a214b14 commit afbda6d
Show file tree
Hide file tree
Showing 30 changed files with 103 additions and 133 deletions.
3 changes: 3 additions & 0 deletions src/Psalm/FileBasedPluginAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Psalm\Internal\Scanner\FileScanner;
use SimpleXMLElement;

use function class_exists;
use function reset;

class FileBasedPluginAdapter implements Plugin\PluginEntryPointInterface
Expand Down Expand Up @@ -39,6 +40,8 @@ public function __invoke(Plugin\RegistrationInterface $registration, ?SimpleXMLE
/** @psalm-suppress UnresolvableInclude */
require_once($this->path);

\assert(class_exists($fq_class_name));

$registration->registerHooksFromClass($fq_class_name);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public function __construct(PhpParser\Node\Stmt\ClassLike $class, SourceAnalyzer

public function __destruct()
{
$this->source = null;
$this->file_analyzer = null;
unset($this->source);
unset($this->file_analyzer);
}

public function getMethodMutations(
Expand Down
3 changes: 1 addition & 2 deletions src/Psalm/Internal/Analyzer/FileAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,6 @@ public function getReturnType() : ?Type\Union

public function clearSourceBeforeDestruction() : void
{
/** @psalm-suppress PossiblyNullPropertyAssignmentValue */
$this->source = null;
unset($this->source);
}
}
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/SourceAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class SourceAnalyzer implements StatementsSource

public function __destruct()
{
$this->source = null;
unset($this->source);
}

public function getAliases(): Aliases
Expand Down
28 changes: 12 additions & 16 deletions src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@ public static function analyze(
$case_context->inside_conditional = true;

if (ExpressionAnalyzer::analyze($statements_analyzer, $case->cond, $case_context) === false) {
/** @psalm-suppress PossiblyNullPropertyAssignmentValue */
$case_scope->parent_context = null;
$case_context->case_scope = null;
$case_context->parent_context = null;
unset($case_scope->parent_context);
unset($case_context->case_scope);
unset($case_context->parent_context);

return false;
}
Expand Down Expand Up @@ -273,10 +272,9 @@ public static function analyze(
$switch_scope->leftover_statements = [$case_if_stmt];
}

/** @psalm-suppress PossiblyNullPropertyAssignmentValue */
$case_scope->parent_context = null;
$case_context->case_scope = null;
$case_context->parent_context = null;
unset($case_scope->parent_context);
unset($case_context->case_scope);
unset($case_context->parent_context);

$statements_analyzer->node_data = $old_node_data;

Expand Down Expand Up @@ -500,10 +498,9 @@ public static function analyze(
$case_exit_type,
$switch_scope
) === false) {
/** @psalm-suppress PossiblyNullPropertyAssignmentValue */
$case_scope->parent_context = null;
$case_context->case_scope = null;
$case_context->parent_context = null;
unset($case_scope->parent_context);
unset($case_context->case_scope);
unset($case_context->parent_context);

return false;
}
Expand Down Expand Up @@ -558,10 +555,9 @@ public static function analyze(
}
}

/** @psalm-suppress PossiblyNullPropertyAssignmentValue */
$case_scope->parent_context = null;
$case_context->case_scope = null;
$case_context->parent_context = null;
unset($case_scope->parent_context);
unset($case_context->case_scope);
unset($case_context->parent_context);

return null;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function analyze(
$existing_thrown_exceptions = $context->possibly_thrown_exceptions;

/**
* @var array<string, array<array-key, CodeLocation>>
* @var array<string, array<array-key, CodeLocation>> $context->possibly_thrown_exceptions
*/
$context->possibly_thrown_exceptions = [];

Expand Down Expand Up @@ -270,9 +270,6 @@ public static function analyze(
}
}

/**
* @var array<string, array<array-key, CodeLocation>>
*/
$catch_context->possibly_thrown_exceptions = [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function analyze(
$left_context->referenced_var_ids = [];
$left_context->assigned_var_ids = [];

/** @var list<string> */
/** @var list<string> $left_context->reconciled_expression_clauses */
$left_context->reconciled_expression_clauses = [];

if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->left, $left_context) === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ private static function getNumericalType($result): Type\Union
}

/**
* @param string[] &$invalid_left_messages
* @param string[] &$invalid_right_messages
* @param string[] $invalid_left_messages
* @param string[] $invalid_right_messages
*/
private static function analyzeOperands(
?StatementsSource $statements_source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class ConcatAnalyzer
{
/**
* @param Type\Union|null &$result_type
* @param Type\Union|null $result_type
*/
public static function analyze(
StatementsAnalyzer $statements_analyzer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent;
use Psalm\Storage\FunctionLikeParameter;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TCallable;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TList;
use Psalm\Type\Atomic;

use function array_merge;
use function count;
Expand Down Expand Up @@ -101,11 +98,11 @@ public static function checkArgumentMatches(
) {
/**
* @psalm-suppress PossiblyUndefinedStringArrayOffset
* @var TList|TArray
* @var Atomic\TList|Atomic\TArray
*/
$array_type = $param_type->getAtomicTypes()['array'];

if ($array_type instanceof TList) {
if ($array_type instanceof Atomic\TList) {
$param_type = $array_type->type_param;
} else {
$param_type = $array_type->type_params[1];
Expand Down Expand Up @@ -313,20 +310,20 @@ private static function checkFunctionLikeTypeMatches(
$arg_type_param = null;

foreach ($arg_type->getAtomicTypes() as $arg_atomic_type) {
if ($arg_atomic_type instanceof Type\Atomic\TArray
|| $arg_atomic_type instanceof Type\Atomic\TList
|| $arg_atomic_type instanceof Type\Atomic\TKeyedArray
if ($arg_atomic_type instanceof Atomic\TArray
|| $arg_atomic_type instanceof Atomic\TList
|| $arg_atomic_type instanceof Atomic\TKeyedArray
) {
if ($arg_atomic_type instanceof Type\Atomic\TKeyedArray) {
if ($arg_atomic_type instanceof Atomic\TKeyedArray) {
$arg_type_param = $arg_atomic_type->getGenericValueType();
} elseif ($arg_atomic_type instanceof Type\Atomic\TList) {
} elseif ($arg_atomic_type instanceof Atomic\TList) {
$arg_type_param = $arg_atomic_type->type_param;
} else {
$arg_type_param = $arg_atomic_type->type_params[1];
}
} elseif ($arg_atomic_type instanceof Type\Atomic\TIterable) {
} elseif ($arg_atomic_type instanceof Atomic\TIterable) {
$arg_type_param = $arg_atomic_type->type_params[1];
} elseif ($arg_atomic_type instanceof Type\Atomic\TNamedObject) {
} elseif ($arg_atomic_type instanceof Atomic\TNamedObject) {
ForeachAnalyzer::getKeyValueParamsForTraversableObject(
$arg_atomic_type,
$codebase,
Expand Down Expand Up @@ -457,12 +454,12 @@ private static function checkFunctionLikeTypeMatches(
if ($arg_type->hasArray()) {
/**
* @psalm-suppress PossiblyUndefinedStringArrayOffset
* @var Type\Atomic\TArray|Type\Atomic\TList|Type\Atomic\TKeyedArray|Type\Atomic\TClassStringMap
* @var Atomic\TArray|Atomic\TList|Atomic\TKeyedArray|Atomic\TClassStringMap
*/
$unpacked_atomic_array = $arg_type->getAtomicTypes()['array'];
$arg_key_allowed = true;

if ($unpacked_atomic_array instanceof Type\Atomic\TKeyedArray) {
if ($unpacked_atomic_array instanceof Atomic\TKeyedArray) {
if (!$allow_named_args && !$unpacked_atomic_array->getGenericKeyType()->isInt()) {
$arg_key_allowed = false;
}
Expand Down Expand Up @@ -493,9 +490,9 @@ private static function checkFunctionLikeTypeMatches(
} else {
$arg_type = Type::getMixed();
}
} elseif ($unpacked_atomic_array instanceof Type\Atomic\TList) {
} elseif ($unpacked_atomic_array instanceof Atomic\TList) {
$arg_type = $unpacked_atomic_array->type_param;
} elseif ($unpacked_atomic_array instanceof Type\Atomic\TClassStringMap) {
} elseif ($unpacked_atomic_array instanceof Atomic\TClassStringMap) {
$arg_type = Type::getMixed();
} else {
if (!$allow_named_args && !$unpacked_atomic_array->type_params[0]->isInt()) {
Expand Down Expand Up @@ -653,8 +650,7 @@ private static function checkFunctionLikeTypeMatches(
}

/**
* @param Type\Atomic\TKeyedArray|Type\Atomic\TArray|Type\Atomic\TList|Type\Atomic\TClassStringMap
* $unpacked_atomic_array
* @param Atomic\TKeyedArray|Atomic\TArray|Atomic\TList|Atomic\TClassStringMap $unpacked_atomic_array
* @return null|false
*/
public static function verifyType(
Expand Down Expand Up @@ -835,7 +831,7 @@ public static function verifyType(
// we do this replacement early because later we don't have access to the
// $statements_analyzer, which is necessary to understand string function names
foreach ($input_type->getAtomicTypes() as $key => $atomic_type) {
if (!$atomic_type instanceof Type\Atomic\TLiteralString
if (!$atomic_type instanceof Atomic\TLiteralString
|| \Psalm\Internal\Codebase\InternalCallMapHandler::inCallMap($atomic_type->value)
) {
continue;
Expand Down Expand Up @@ -902,7 +898,7 @@ public static function verifyType(
$potential_method_ids = [];

foreach ($input_type->getAtomicTypes() as $input_type_part) {
if ($input_type_part instanceof Type\Atomic\TKeyedArray) {
if ($input_type_part instanceof Atomic\TKeyedArray) {
$potential_method_id = CallableTypeComparator::getCallableMethodIdFromTKeyedArray(
$input_type_part,
$codebase,
Expand All @@ -913,7 +909,7 @@ public static function verifyType(
if ($potential_method_id && $potential_method_id !== 'not-callable') {
$potential_method_ids[] = $potential_method_id;
}
} elseif ($input_type_part instanceof Type\Atomic\TLiteralString
} elseif ($input_type_part instanceof Atomic\TLiteralString
&& strpos($input_type_part->value, '::')
) {
$parts = explode('::', $input_type_part->value);
Expand Down Expand Up @@ -1183,7 +1179,7 @@ private static function verifyExplicitParam(
$codebase = $statements_analyzer->getCodebase();

foreach ($param_type->getAtomicTypes() as $param_type_part) {
if ($param_type_part instanceof TClassString
if ($param_type_part instanceof Atomic\TClassString
&& $input_expr instanceof PhpParser\Node\Scalar\String_
&& $param_type->isSingle()
) {
Expand All @@ -1199,11 +1195,11 @@ private static function verifyExplicitParam(
) {
return;
}
} elseif ($param_type_part instanceof TArray
} elseif ($param_type_part instanceof Atomic\TArray
&& $input_expr instanceof PhpParser\Node\Expr\Array_
) {
foreach ($param_type_part->type_params[1]->getAtomicTypes() as $param_array_type_part) {
if ($param_array_type_part instanceof TClassString) {
if ($param_array_type_part instanceof Atomic\TClassString) {
foreach ($input_expr->items as $item) {
if ($item && $item->value instanceof PhpParser\Node\Scalar\String_) {
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
Expand All @@ -1222,7 +1218,7 @@ private static function verifyExplicitParam(
}
}
}
} elseif ($param_type_part instanceof TCallable) {
} elseif ($param_type_part instanceof Atomic\TCallable) {
$can_be_callable_like_array = false;
if ($param_type->hasArray()) {
/**
Expand All @@ -1231,11 +1227,11 @@ private static function verifyExplicitParam(
$param_array_type = $param_type->getAtomicTypes()['array'];

$row_type = null;
if ($param_array_type instanceof TList) {
if ($param_array_type instanceof Atomic\TList) {
$row_type = $param_array_type->type_param;
} elseif ($param_array_type instanceof TArray) {
} elseif ($param_array_type instanceof Atomic\TArray) {
$row_type = $param_array_type->type_params[1];
} elseif ($param_array_type instanceof Type\Atomic\TKeyedArray) {
} elseif ($param_array_type instanceof Atomic\TKeyedArray) {
$row_type = $param_array_type->getGenericArrayType()->type_params[1];
}

Expand Down Expand Up @@ -1350,8 +1346,7 @@ private static function verifyExplicitParam(
}

/**
* @param Type\Atomic\TKeyedArray|Type\Atomic\TArray|Type\Atomic\TList|Type\Atomic\TClassStringMap
* $unpacked_atomic_array
* @param Atomic\TKeyedArray|Atomic\TArray|Atomic\TList|Atomic\TClassStringMap $unpacked_atomic_array
*/
private static function coerceValueAfterGatekeeperArgument(
StatementsAnalyzer $statements_analyzer,
Expand All @@ -1372,9 +1367,9 @@ private static function coerceValueAfterGatekeeperArgument(
$input_type = clone $input_type;

foreach ($param_type->getAtomicTypes() as $param_atomic_type) {
if ($param_atomic_type instanceof Type\Atomic\TGenericObject) {
if ($param_atomic_type instanceof Atomic\TGenericObject) {
foreach ($input_type->getAtomicTypes() as $input_atomic_type) {
if ($input_atomic_type instanceof Type\Atomic\TGenericObject
if ($input_atomic_type instanceof Atomic\TGenericObject
&& $input_atomic_type->value === $param_atomic_type->value
) {
foreach ($input_atomic_type->type_params as $i => $type_param) {
Expand Down Expand Up @@ -1445,17 +1440,17 @@ private static function coerceValueAfterGatekeeperArgument(
}

if ($unpack) {
if ($unpacked_atomic_array instanceof Type\Atomic\TList) {
if ($unpacked_atomic_array instanceof Atomic\TList) {
$unpacked_atomic_array = clone $unpacked_atomic_array;
$unpacked_atomic_array->type_param = $input_type;

$context->vars_in_scope[$var_id] = new Type\Union([$unpacked_atomic_array]);
} elseif ($unpacked_atomic_array instanceof Type\Atomic\TArray) {
} elseif ($unpacked_atomic_array instanceof Atomic\TArray) {
$unpacked_atomic_array = clone $unpacked_atomic_array;
$unpacked_atomic_array->type_params[1] = $input_type;

$context->vars_in_scope[$var_id] = new Type\Union([$unpacked_atomic_array]);
} elseif ($unpacked_atomic_array instanceof Type\Atomic\TKeyedArray
} elseif ($unpacked_atomic_array instanceof Atomic\TKeyedArray
&& $unpacked_atomic_array->is_list
) {
$unpacked_atomic_array = $unpacked_atomic_array->getList();
Expand All @@ -1464,7 +1459,7 @@ private static function coerceValueAfterGatekeeperArgument(
$context->vars_in_scope[$var_id] = new Type\Union([$unpacked_atomic_array]);
} else {
$context->vars_in_scope[$var_id] = new Type\Union([
new TArray([
new Atomic\TArray([
Type::getInt(),
$input_type
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ private static function getIntersectionReturnType(
foreach ($intersection_types as $intersection_type) {
$intersection_result = clone $result;

/** @var ?Type\Union */
/** @var ?Type\Union $intersection_result->return_type */
$intersection_result->return_type = null;

self::analyze(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,7 @@ public static function getGenericParamForOffset(
}

/**
* @param PhpParser\Node\Scalar\String_|PhpParser\Node\Expr\Array_|PhpParser\Node\Expr\BinaryOp\Concat
* $callable_arg
* @param PhpParser\Node\Scalar\String_|PhpParser\Node\Expr\Array_|PhpParser\Node\Expr\BinaryOp\Concat $callable_arg
*
* @return list<non-empty-string>
*
Expand Down
4 changes: 0 additions & 4 deletions src/Psalm/Internal/Codebase/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,6 @@ private function fileExistsForClassLike(ClassLikes $classlikes, string $fq_class
return false;
}

if (isset($this->existing_classlikes_lc[$fq_class_name_lc])) {
throw new \InvalidArgumentException('Why are you asking about a builtin class?');
}

$composer_file_path = $this->config->getComposerFilePathForClassLike($fq_class_name);

if ($composer_file_path && file_exists($composer_file_path)) {
Expand Down
Loading

0 comments on commit afbda6d

Please sign in to comment.