Skip to content

Commit

Permalink
Merge pull request #6660 from orklah/cleanup4
Browse files Browse the repository at this point in the history
Code trimming
  • Loading branch information
orklah committed Oct 13, 2021
2 parents e17290a + ae193ca commit 64df00b
Show file tree
Hide file tree
Showing 53 changed files with 182 additions and 171 deletions.
20 changes: 11 additions & 9 deletions src/Psalm/Internal/Analyzer/CanAlias.php
Expand Up @@ -47,11 +47,13 @@ public function visitUse(PhpParser\Node\Stmt\Use_ $stmt): void

foreach ($stmt->uses as $use) {
$use_path = implode('\\', $use->name->parts);
$use_path_lc = strtolower($use_path);
$use_alias = $use->alias->name ?? $use->name->getLast();
$use_alias_lc = strtolower($use_alias);

switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $stmt->type) {
case PhpParser\Node\Stmt\Use_::TYPE_FUNCTION:
$this->aliased_functions[strtolower($use_alias)] = $use_path;
$this->aliased_functions[$use_alias_lc] = $use_path;
break;

case PhpParser\Node\Stmt\Use_::TYPE_CONSTANT:
Expand All @@ -67,15 +69,15 @@ public function visitUse(PhpParser\Node\Stmt\Use_ $stmt): void
);
if ($codebase->collect_locations) {
// register the path
$codebase->use_referencing_locations[strtolower($use_path)][] =
$codebase->use_referencing_locations[$use_path_lc][] =
new \Psalm\CodeLocation($this, $use);

$codebase->use_referencing_files[$this->getFilePath()][strtolower($use_path)] = true;
$codebase->use_referencing_files[$this->getFilePath()][$use_path_lc] = true;
}

if ($codebase->alter_code) {
if (isset($codebase->class_transforms[strtolower($use_path)])) {
$new_fq_class_name = $codebase->class_transforms[strtolower($use_path)];
if (isset($codebase->class_transforms[$use_path_lc])) {
$new_fq_class_name = $codebase->class_transforms[$use_path_lc];

$file_manipulations = [];

Expand All @@ -88,12 +90,12 @@ public function visitUse(PhpParser\Node\Stmt\Use_ $stmt): void
FileManipulationBuffer::add($this->getFilePath(), $file_manipulations);
}

$this->aliased_classes_flipped_replaceable[strtolower($use_path)] = $use_alias;
$this->aliased_classes_flipped_replaceable[$use_path_lc] = $use_alias;
}

$this->aliased_classes[strtolower($use_alias)] = $use_path;
$this->aliased_class_locations[strtolower($use_alias)] = new CodeLocation($this, $stmt);
$this->aliased_classes_flipped[strtolower($use_path)] = $use_alias;
$this->aliased_classes[$use_alias_lc] = $use_path;
$this->aliased_class_locations[$use_alias_lc] = new CodeLocation($this, $stmt);
$this->aliased_classes_flipped[$use_path_lc] = $use_alias;
break;
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Expand Up @@ -1348,7 +1348,7 @@ private function analyzeTraitUse(
if (IssueBuffer::accepts(
new UndefinedTrait(
'Trait ' . $fq_trait_name . ' does not exist',
new CodeLocation($previous_trait_analyzer ?: $this, $trait_name)
new CodeLocation($previous_trait_analyzer ?? $this, $trait_name)
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
Expand All @@ -1362,7 +1362,7 @@ private function analyzeTraitUse(
if (IssueBuffer::accepts(
new UndefinedTrait(
'Trait ' . $fq_trait_name . ' has wrong casing',
new CodeLocation($previous_trait_analyzer ?: $this, $trait_name)
new CodeLocation($previous_trait_analyzer ?? $this, $trait_name)
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
Expand All @@ -1379,7 +1379,7 @@ private function analyzeTraitUse(
if (IssueBuffer::accepts(
new DeprecatedTrait(
'Trait ' . $fq_trait_name . ' is deprecated',
new CodeLocation($previous_trait_analyzer ?: $this, $trait_name)
new CodeLocation($previous_trait_analyzer ?? $this, $trait_name)
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
Expand All @@ -1398,7 +1398,7 @@ private function analyzeTraitUse(
new ExtensionRequirementViolation(
$fq_trait_name . ' requires using class to extend ' . $extension_requirement
. ', but ' . $storage->name . ' does not',
new CodeLocation($previous_trait_analyzer ?: $this, $trait_name)
new CodeLocation($previous_trait_analyzer ?? $this, $trait_name)
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
Expand All @@ -1416,7 +1416,7 @@ private function analyzeTraitUse(
new ImplementationRequirementViolation(
$fq_trait_name . ' requires using class to implement '
. $implementation_requirement . ', but ' . $storage->name . ' does not',
new CodeLocation($previous_trait_analyzer ?: $this, $trait_name)
new CodeLocation($previous_trait_analyzer ?? $this, $trait_name)
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
Expand All @@ -1429,7 +1429,7 @@ private function analyzeTraitUse(
if (IssueBuffer::accepts(
new MutableDependency(
$storage->name . ' is marked @psalm-immutable but ' . $fq_trait_name . ' is not',
new CodeLocation($previous_trait_analyzer ?: $this, $trait_name)
new CodeLocation($previous_trait_analyzer ?? $this, $trait_name)
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
Expand Down Expand Up @@ -2422,7 +2422,7 @@ private function checkImplementedInterfaces(
MethodComparator::compare(
$codebase,
null,
$implementer_classlike_storage ?: $storage,
$implementer_classlike_storage ?? $storage,
$interface_storage,
$implementer_method_storage,
$interface_method_storage,
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Analyzer/FileAnalyzer.php
Expand Up @@ -192,8 +192,8 @@ public function analyze(
foreach ($leftover_stmts as $leftover_stmt) {
if ($leftover_stmt instanceof PhpParser\Node\Stmt\Return_) {
if ($leftover_stmt->expr) {
$this->return_type = $statements_analyzer->node_data->getType($leftover_stmt->expr)
?: Type::getMixed();
$this->return_type =
$statements_analyzer->node_data->getType($leftover_stmt->expr) ?? Type::getMixed();
} else {
$this->return_type = Type::getVoid();
}
Expand Down
Expand Up @@ -279,8 +279,8 @@ private static function processYieldTypes(
new Atomic\TGenericObject(
'Generator',
[
$key_type ?: Type::getMixed(),
$value_type ?: Type::getMixed(),
$key_type ?? Type::getMixed(),
$value_type ?? Type::getMixed(),
Type::getMixed(),
$return_types ? Type::combineUnionTypeArray($return_types, null) : Type::getVoid()
]
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/MethodComparator.php
Expand Up @@ -67,7 +67,7 @@ public static function compare(
$cased_guide_method_id = $guide_classlike_storage->name . '::' . $guide_method_storage->cased_name;

$codebase->methods->file_reference_provider->addMethodDependencyToClassMember(
strtolower((string)($implementer_declaring_method_id ?: $implementer_method_id)),
strtolower((string)($implementer_declaring_method_id ?? $implementer_method_id)),
strtolower($guide_classlike_storage->name . '::' . $guide_method_storage->cased_name)
);

Expand Down
7 changes: 4 additions & 3 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Expand Up @@ -763,6 +763,7 @@ public function interpretRefactors() : void
);
}

$source_lc = strtolower($source);
if (strtolower($source_parts[0]) !== strtolower($destination_parts[0])) {
$source_method_storage = $this->codebase->methods->getStorage($source_method_id);
$destination_class_storage
Expand All @@ -779,12 +780,12 @@ public function interpretRefactors() : void
);
}

$this->codebase->methods_to_move[strtolower($source)]= $destination;
$this->codebase->methods_to_move[$source_lc]= $destination;
} else {
$this->codebase->methods_to_rename[strtolower($source)] = $destination_parts[1];
$this->codebase->methods_to_rename[$source_lc] = $destination_parts[1];
}

$this->codebase->call_transforms[strtolower($source) . '\((.*\))'] = $destination . '($1)';
$this->codebase->call_transforms[$source_lc . '\((.*\))'] = $destination . '($1)';
continue;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php
Expand Up @@ -251,7 +251,7 @@ public static function analyze(
}

if ($stmt->keyVar instanceof PhpParser\Node\Expr\Variable && is_string($stmt->keyVar->name)) {
$key_type = $key_type ?: Type::getMixed();
$key_type = $key_type ?? Type::getMixed();

AssignmentAnalyzer::analyze(
$statements_analyzer,
Expand All @@ -264,7 +264,7 @@ public static function analyze(
);
}

$value_type = $value_type ?: Type::getMixed();
$value_type = $value_type ?? Type::getMixed();

if ($stmt->byRef) {
$value_type->by_ref = true;
Expand Down Expand Up @@ -549,7 +549,7 @@ public static function checkIteratorType(
$intersection_value_type,
$value_type_part,
$codebase
) ?: Type::getMixed();
) ?? Type::getMixed();
}

if (!$intersection_key_type) {
Expand All @@ -559,7 +559,7 @@ public static function checkIteratorType(
$intersection_key_type,
$key_type_part,
$codebase
) ?: Type::getMixed();
) ?? Type::getMixed();
}
}

Expand Down Expand Up @@ -816,7 +816,7 @@ public static function handleIterable(
$statements_analyzer->removeSuppressedIssues(['PossiblyUndefinedMethod']);
}

$iterator_class_type = $statements_analyzer->node_data->getType($fake_method_call) ?: null;
$iterator_class_type = $statements_analyzer->node_data->getType($fake_method_call) ?? null;

$statements_analyzer->node_data = $old_data_provider;

Expand Down Expand Up @@ -1071,7 +1071,7 @@ private static function getFakeMethodCallType(
$statements_analyzer->removeSuppressedIssues(['PossiblyUndefinedMethod']);
}

$iterator_class_type = $statements_analyzer->node_data->getType($fake_method_call) ?: null;
$iterator_class_type = $statements_analyzer->node_data->getType($fake_method_call) ?? null;

$statements_analyzer->node_data = $old_data_provider;

Expand Down
Expand Up @@ -49,7 +49,7 @@ public static function analyze(
) {
$switch_var_id = '$__tmp_switch__' . (int) $stmt->cond->getAttribute('startFilePos');

$condition_type = $statements_analyzer->node_data->getType($stmt->cond) ?: Type::getMixed();
$condition_type = $statements_analyzer->node_data->getType($stmt->cond) ?? Type::getMixed();

$context->vars_in_scope[$switch_var_id] = $condition_type;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/Statements/EchoAnalyzer.php
Expand Up @@ -70,7 +70,7 @@ public static function analyze(

if (ArgumentAnalyzer::verifyType(
$statements_analyzer,
$expr_type ?: Type::getMixed(),
$expr_type ?? Type::getMixed(),
Type::getString(),
null,
'echo',
Expand Down
Expand Up @@ -115,9 +115,9 @@ public static function analyze(

if ($array_creation_info->all_list) {
if (empty($array_creation_info->item_key_atomic_types)) {
$array_type = new Type\Atomic\TList($item_value_type ?: Type::getMixed());
$array_type = new Type\Atomic\TList($item_value_type ?? Type::getMixed());
} else {
$array_type = new Type\Atomic\TNonEmptyList($item_value_type ?: Type::getMixed());
$array_type = new Type\Atomic\TNonEmptyList($item_value_type ?? Type::getMixed());
$array_type->count = count($array_creation_info->property_types);
}

Expand Down Expand Up @@ -207,7 +207,7 @@ public static function analyze(

$array_type = new Type\Atomic\TNonEmptyArray([
$item_key_type && !$item_key_type->hasMixed() ? $item_key_type : Type::getArrayKey(),
$item_value_type ?: Type::getMixed(),
$item_value_type ?? Type::getMixed(),
]);

$array_type->count = count($array_creation_info->property_types);
Expand Down
Expand Up @@ -95,7 +95,7 @@ public static function updateArrayType(

$codebase = $statements_analyzer->getCodebase();

$root_type = $statements_analyzer->node_data->getType($root_array_expr) ?: Type::getMixed();
$root_type = $statements_analyzer->node_data->getType($root_array_expr) ?? Type::getMixed();

if ($root_type->hasMixed()) {
if (ExpressionAnalyzer::analyze(
Expand Down Expand Up @@ -735,7 +735,7 @@ private static function analyzeNestedArrayAssignment(
$statements_analyzer,
$child_stmt,
$array_type,
$child_stmt_dim_type ?: Type::getInt(),
$child_stmt_dim_type ?? Type::getInt(),
true,
$array_var_id,
$context,
Expand Down Expand Up @@ -906,7 +906,7 @@ private static function analyzeNestedArrayAssignment(
self::taintArrayAssignment(
$statements_analyzer,
$child_stmt,
$statements_analyzer->node_data->getType($child_stmt->var) ?: Type::getMixed(),
$statements_analyzer->node_data->getType($child_stmt->var) ?? Type::getMixed(),
$new_child_type,
$parent_array_var_id,
$key_values
Expand Down
Expand Up @@ -118,7 +118,7 @@ public static function analyze(

$assigned_properties = [
new AssignedProperty(
$class_property_type ?: Type::getMixed(),
$class_property_type ?? Type::getMixed(),
$property_id,
$assignment_value_type
)
Expand Down Expand Up @@ -194,7 +194,7 @@ public static function analyze(
. ' parent type `' . $assignment_type->getId() . '` provided',
new CodeLocation(
$statements_analyzer->getSource(),
$assignment_value ?: $stmt,
$assignment_value ?? $stmt,
$context->include_location
),
$assigned_property->id
Expand All @@ -210,7 +210,7 @@ public static function analyze(
. ' parent type \'' . $assignment_type->getId() . '\' provided',
new CodeLocation(
$statements_analyzer->getSource(),
$assignment_value ?: $stmt,
$assignment_value ?? $stmt,
$context->include_location
),
$assigned_property->id
Expand All @@ -229,7 +229,7 @@ public static function analyze(
. '\'' . $assignment_type . '\' provided with a __toString method',
new CodeLocation(
$statements_analyzer->getSource(),
$assignment_value ?: $stmt,
$assignment_value ?? $stmt,
$context->include_location
)
),
Expand Down Expand Up @@ -266,7 +266,7 @@ public static function analyze(
'\' cannot be assigned nullable type \'' . $assignment_type . '\'',
new CodeLocation(
$statements_analyzer->getSource(),
$assignment_value ?: $stmt,
$assignment_value ?? $stmt,
$context->include_location
),
$assigned_property->id
Expand All @@ -288,7 +288,7 @@ public static function analyze(
'\' cannot be assigned possibly false type \'' . $assignment_type . '\'',
new CodeLocation(
$statements_analyzer->getSource(),
$assignment_value ?: $stmt,
$assignment_value ?? $stmt,
$context->include_location
),
$assigned_property->id
Expand All @@ -309,7 +309,7 @@ public static function analyze(
'\' cannot be assigned type \'' . $assignment_value_type->getId() . '\'',
new CodeLocation(
$statements_analyzer->getSource(),
$assignment_value ?: $stmt,
$assignment_value ?? $stmt,
$context->include_location
),
$property_id
Expand All @@ -326,7 +326,7 @@ public static function analyze(
$assignment_value_type->getId() . '\'',
new CodeLocation(
$statements_analyzer->getSource(),
$assignment_value ?: $stmt,
$assignment_value ?? $stmt,
$context->include_location
),
$property_id
Expand Down

0 comments on commit 64df00b

Please sign in to comment.