Skip to content

Commit

Permalink
classlike value is correct case so it must be checked in addition to …
Browse files Browse the repository at this point in the history
…the lowercase check
  • Loading branch information
kkmuffme committed Sep 8, 2022
1 parent 6849a2a commit f0c2595
Showing 1 changed file with 46 additions and 42 deletions.
88 changes: 46 additions & 42 deletions src/Psalm/Internal/Codebase/ClassLikes.php
Expand Up @@ -377,7 +377,7 @@ public function hasFullyQualifiedClassName(
|| !$this->classlike_storage_provider->has($fq_class_name_lc)
) {
if ((
!isset($this->existing_classes_lc[$fq_class_name_lc])
!isset($this->existing_classes_lc[$fq_class_name_lc])
|| $this->existing_classes_lc[$fq_class_name_lc]
)
&& !$this->classlike_storage_provider->has($fq_class_name_lc)
Expand Down Expand Up @@ -417,7 +417,7 @@ public function hasFullyQualifiedInterfaceName(
|| !$this->classlike_storage_provider->has($fq_class_name_lc)
) {
if ((
!isset($this->existing_classes_lc[$fq_class_name_lc])
!isset($this->existing_classes_lc[$fq_class_name_lc])
|| $this->existing_classes_lc[$fq_class_name_lc]
)
&& !$this->classlike_storage_provider->has($fq_class_name_lc)
Expand Down Expand Up @@ -484,7 +484,7 @@ public function hasFullyQualifiedEnumName(
|| !$this->classlike_storage_provider->has($fq_class_name_lc)
) {
if ((
!isset($this->existing_classes_lc[$fq_class_name_lc])
!isset($this->existing_classes_lc[$fq_class_name_lc])
|| $this->existing_classes_lc[$fq_class_name_lc]
)
&& !$this->classlike_storage_provider->has($fq_class_name_lc)
Expand Down Expand Up @@ -568,7 +568,7 @@ public function classOrInterfaceExists(
?string $calling_method_id = null
): bool {
return $this->classExists($fq_class_name, $code_location, $calling_fq_class_name, $calling_method_id)
|| $this->interfaceExists($fq_class_name, $code_location, $calling_fq_class_name, $calling_method_id);
|| $this->interfaceExists($fq_class_name, $code_location, $calling_fq_class_name, $calling_method_id);
}

/**
Expand All @@ -581,8 +581,8 @@ public function classOrInterfaceOrEnumExists(
?string $calling_method_id = null
): bool {
return $this->classExists($fq_class_name, $code_location, $calling_fq_class_name, $calling_method_id)
|| $this->interfaceExists($fq_class_name, $code_location, $calling_fq_class_name, $calling_method_id)
|| $this->enumExists($fq_class_name, $code_location, $calling_fq_class_name, $calling_method_id);
|| $this->interfaceExists($fq_class_name, $code_location, $calling_fq_class_name, $calling_method_id)
|| $this->enumExists($fq_class_name, $code_location, $calling_fq_class_name, $calling_method_id);
}

/**
Expand Down Expand Up @@ -750,7 +750,8 @@ public function classHasCorrectCasing(string $fq_class_name): bool
return true;
}

if (isset($this->classlike_aliases[strtolower($fq_class_name)])) {
if (isset($this->classlike_aliases[strtolower($fq_class_name)]) &&
$this->classlike_aliases[strtolower($fq_class_name)] === $fq_class_name) {
return true;
}

Expand All @@ -759,7 +760,8 @@ public function classHasCorrectCasing(string $fq_class_name): bool

public function interfaceHasCorrectCasing(string $fq_interface_name): bool
{
if (isset($this->classlike_aliases[strtolower($fq_interface_name)])) {
if (isset($this->classlike_aliases[strtolower($fq_interface_name)]) &&
$this->classlike_aliases[strtolower($fq_interface_name)] === $fq_interface_name) {
return true;
}

Expand All @@ -768,7 +770,8 @@ public function interfaceHasCorrectCasing(string $fq_interface_name): bool

public function enumHasCorrectCasing(string $fq_enum_name): bool
{
if (isset($this->classlike_aliases[strtolower($fq_enum_name)])) {
if (isset($this->classlike_aliases[strtolower($fq_enum_name)]) &&
$this->classlike_aliases[strtolower($fq_enum_name)] === $fq_enum_name) {
return true;
}

Expand All @@ -777,7 +780,8 @@ public function enumHasCorrectCasing(string $fq_enum_name): bool

public function traitHasCorrectCase(string $fq_trait_name): bool
{
if (isset($this->classlike_aliases[strtolower($fq_trait_name)])) {
if (isset($this->classlike_aliases[strtolower($fq_trait_name)]) &&
$this->classlike_aliases[strtolower($fq_trait_name)] === $fq_trait_name) {
return true;
}

Expand Down Expand Up @@ -907,7 +911,7 @@ public function consolidateAnalyzedData(Methods $methods, ?Progress $progress, b
foreach ($stmt->stmts as $namespace_stmt) {
if ($namespace_stmt instanceof PhpParser\Node\Stmt\Class_
&& strtolower((string) $stmt->name . '\\' . (string) $namespace_stmt->name)
=== $fq_class_name_lc
=== $fq_class_name_lc
) {
self::makeImmutable(
$namespace_stmt,
Expand All @@ -917,7 +921,7 @@ public function consolidateAnalyzedData(Methods $methods, ?Progress $progress, b
}
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\Class_
&& strtolower((string) $stmt->name) === $fq_class_name_lc
&& strtolower((string) $stmt->name) === $fq_class_name_lc
) {
self::makeImmutable(
$stmt,
Expand Down Expand Up @@ -1226,7 +1230,7 @@ public function handleClassLikeReferenceInMigration(
$destination_class = explode('::', $codebase->methods_to_move[$calling_method_id])[0];

$intended_fq_class_name = strtolower($calling_fq_class_name) === strtolower($fq_class_name)
&& isset($codebase->classes_to_move[strtolower($calling_fq_class_name)])
&& isset($codebase->classes_to_move[strtolower($calling_fq_class_name)])
? $destination_class
: $fq_class_name;

Expand Down Expand Up @@ -1308,7 +1312,7 @@ public function handleClassLikeReferenceInMigration(
$migrated_source_fqcln,
$was_self
)
. ($class_name_node instanceof PhpParser\Node\Scalar\MagicConst\Class_ ? '::class' : '')
. ($class_name_node instanceof PhpParser\Node\Scalar\MagicConst\Class_ ? '::class' : '')
);

FileManipulationBuffer::add($source->getFilePath(), $file_manipulations);
Expand Down Expand Up @@ -1603,7 +1607,7 @@ public function getConstantsForClass(string $class_name, int $visibility): array
$storage->constants,
function ($constant) {
return $constant->type
&& $constant->visibility === ClassLikeAnalyzer::VISIBILITY_PUBLIC;
&& $constant->visibility === ClassLikeAnalyzer::VISIBILITY_PUBLIC;
}
);
}
Expand All @@ -1613,8 +1617,8 @@ function ($constant) {
$storage->constants,
function ($constant) {
return $constant->type
&& ($constant->visibility === ClassLikeAnalyzer::VISIBILITY_PUBLIC
|| $constant->visibility === ClassLikeAnalyzer::VISIBILITY_PROTECTED);
&& ($constant->visibility === ClassLikeAnalyzer::VISIBILITY_PUBLIC
|| $constant->visibility === ClassLikeAnalyzer::VISIBILITY_PROTECTED);
}
);
}
Expand Down Expand Up @@ -1753,7 +1757,7 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
}

$has_variable_calls = $codebase->analyzer->hasMixedMemberName($method_name)
|| $codebase->analyzer->hasMixedMemberName(strtolower($classlike_storage->name . '::'));
|| $codebase->analyzer->hasMixedMemberName(strtolower($classlike_storage->name . '::'));

if (isset($classlike_storage->overridden_method_ids[$method_name])) {
foreach ($classlike_storage->overridden_method_ids[$method_name] as $parent_method_id) {
Expand Down Expand Up @@ -1814,8 +1818,8 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
if (!$has_parent_references) {
$issue = new PossiblyUnusedMethod(
'Cannot find ' . ($has_variable_calls ? 'explicit' : 'any')
. ' calls to method ' . $method_id
. ($has_variable_calls ? ' (but did find some potential callers)' : ''),
. ' calls to method ' . $method_id
. ($has_variable_calls ? ' (but did find some potential callers)' : ''),
$method_storage->location,
$method_id
);
Expand All @@ -1837,30 +1841,30 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
$issue,
$method_storage->suppressed_issues,
$method_storage->stmt_location
&& !$declaring_classlike_storage->is_trait
&& !$has_variable_calls
&& !$declaring_classlike_storage->is_trait
&& !$has_variable_calls
)) {
// fall through
}
}
} elseif (!isset($classlike_storage->declaring_method_ids['__call'])) {
$has_variable_calls = $codebase->analyzer->hasMixedMemberName(
strtolower($classlike_storage->name . '::')
) || $codebase->analyzer->hasMixedMemberName($method_name);
strtolower($classlike_storage->name . '::')
) || $codebase->analyzer->hasMixedMemberName($method_name);

if ($method_name === '__construct') {
$issue = new UnusedConstructor(
'Cannot find ' . ($has_variable_calls ? 'explicit' : 'any')
. ' calls to private constructor ' . $method_id
. ($has_variable_calls ? ' (but did find some potential callers)' : ''),
. ' calls to private constructor ' . $method_id
. ($has_variable_calls ? ' (but did find some potential callers)' : ''),
$method_location,
$method_id
);
} else {
$issue = new UnusedMethod(
'Cannot find ' . ($has_variable_calls ? 'explicit' : 'any')
. ' calls to private method ' . $method_id
. ($has_variable_calls ? ' (but did find some potential callers)' : ''),
. ' calls to private method ' . $method_id
. ($has_variable_calls ? ' (but did find some potential callers)' : ''),
$method_location,
$method_id
);
Expand All @@ -1883,8 +1887,8 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
$issue,
$method_storage->suppressed_issues,
$method_storage->stmt_location
&& !$declaring_classlike_storage->is_trait
&& !$has_variable_calls
&& !$declaring_classlike_storage->is_trait
&& !$has_variable_calls
)) {
// fall through
}
Expand Down Expand Up @@ -2051,9 +2055,9 @@ private function findPossibleMethodParamTypes(ClassLikeStorage $classlike_storag
);

$has_variable_calls = $codebase->analyzer->hasMixedMemberName(
$method_name
)
|| $codebase->analyzer->hasMixedMemberName(
$method_name
)
|| $codebase->analyzer->hasMixedMemberName(
strtolower($classlike_storage->name . '::')
);

Expand All @@ -2067,7 +2071,7 @@ private function findPossibleMethodParamTypes(ClassLikeStorage $classlike_storag
$param_name,
$possible_type,
$possible_type->from_docblock
&& $project_analyzer->only_replace_php_types_with_non_docblock_types
&& $project_analyzer->only_replace_php_types_with_non_docblock_types
);
}
} else {
Expand Down Expand Up @@ -2101,7 +2105,7 @@ private function checkPropertyReferences(ClassLikeStorage $classlike_storage): v
$property_references = $all_method_references[$referenced_property_name];

$property_constructor_referenced = isset($property_references[$constructor_name])
&& !$property_storage->is_static;
&& !$property_storage->is_static;
}
}

Expand All @@ -2116,7 +2120,7 @@ private function checkPropertyReferences(ClassLikeStorage $classlike_storage): v
$has_parent_references = isset($classlike_storage->overridden_property_ids[$property_name]);

$has_variable_calls = $codebase->analyzer->hasMixedMemberName('$' . $property_name)
|| $codebase->analyzer->hasMixedMemberName(strtolower($classlike_storage->name) . '::$');
|| $codebase->analyzer->hasMixedMemberName(strtolower($classlike_storage->name) . '::$');

foreach ($classlike_storage->parent_classes as $parent_method_fqcln) {
if ($codebase->analyzer->hasMixedMemberName(
Expand All @@ -2142,8 +2146,8 @@ private function checkPropertyReferences(ClassLikeStorage $classlike_storage): v
) {
$issue = new PossiblyUnusedProperty(
'Cannot find ' . ($has_variable_calls ? 'explicit' : 'any')
. ' references to property ' . $property_id
. ($has_variable_calls ? ' (but did find some potential references)' : ''),
. ' references to property ' . $property_id
. ($has_variable_calls ? ' (but did find some potential references)' : ''),
$property_storage->location
);

Expand Down Expand Up @@ -2171,8 +2175,8 @@ private function checkPropertyReferences(ClassLikeStorage $classlike_storage): v

$issue = new UnusedProperty(
'Cannot find ' . ($has_variable_calls ? 'explicit' : 'any')
. ' references to private property ' . $property_id
. ($has_variable_calls ? ' (but did find some potential references)' : ''),
. ' references to private property ' . $property_id
. ($has_variable_calls ? ' (but did find some potential references)' : ''),
$property_storage->location
);

Expand Down Expand Up @@ -2214,7 +2218,7 @@ public function registerMissingClassLike(string $fq_classlike_name_lc): void
public function isMissingClassLike(string $fq_classlike_name_lc): bool
{
return isset($this->existing_classlikes_lc[$fq_classlike_name_lc])
&& $this->existing_classlikes_lc[$fq_classlike_name_lc] === false;
&& $this->existing_classlikes_lc[$fq_classlike_name_lc] === false;
}

/**
Expand All @@ -2223,7 +2227,7 @@ public function isMissingClassLike(string $fq_classlike_name_lc): bool
public function doesClassLikeExist(string $fq_classlike_name_lc): bool
{
return isset($this->existing_classlikes_lc[$fq_classlike_name_lc])
&& $this->existing_classlikes_lc[$fq_classlike_name_lc];
&& $this->existing_classlikes_lc[$fq_classlike_name_lc];
}

public function forgetMissingClassLikes(): void
Expand Down

0 comments on commit f0c2595

Please sign in to comment.