mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +01:00
Fix #3872 - detect namespace violations in non-methods
This commit is contained in:
parent
7f6b8e0172
commit
e398535f9f
@ -169,22 +169,21 @@ class NamespaceAnalyzer extends SourceAnalyzer implements StatementsSource
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $namespace Generally a namespace, but may also be a fully qualified class name (FQCN)_.
|
||||
* @param string $className Generally a FQCN, but may be a FQCN
|
||||
*
|
||||
* Returns true if $className is the same as, or starts with $namespace, in a case-insensitive comparison.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isWithin(string $className, string $namespace): bool
|
||||
public static function isWithin(string $calling_namespace, string $namespace): bool
|
||||
{
|
||||
if ($namespace === '') {
|
||||
return true; // required to prevent a warning from strpos with empty needle in PHP < 8
|
||||
}
|
||||
$className = strtolower(trim($className, '\\') . '\\');
|
||||
|
||||
$calling_namespace = strtolower(trim($calling_namespace, '\\') . '\\');
|
||||
$namespace = strtolower(trim($namespace, '\\') . '\\');
|
||||
|
||||
return $className === $namespace || strpos($className, $namespace) === 0;
|
||||
return $calling_namespace === $namespace
|
||||
|| strpos($calling_namespace, $namespace) === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -668,6 +668,7 @@ class AtomicMethodCallAnalyzer extends CallAnalyzer
|
||||
$codebase,
|
||||
$context,
|
||||
$method_id,
|
||||
$statements_analyzer->getNamespace(),
|
||||
$name_code_location,
|
||||
$statements_analyzer->getSuppressedIssues()
|
||||
);
|
||||
|
@ -21,6 +21,7 @@ class MethodCallProhibitionAnalyzer
|
||||
Codebase $codebase,
|
||||
Context $context,
|
||||
\Psalm\Internal\MethodIdentifier $method_id,
|
||||
?string $namespace,
|
||||
CodeLocation $code_location,
|
||||
array $suppressed_issues
|
||||
) {
|
||||
@ -48,11 +49,10 @@ class MethodCallProhibitionAnalyzer
|
||||
}
|
||||
}
|
||||
|
||||
if ($context->self
|
||||
&& !$context->collect_initializations
|
||||
if (!$context->collect_initializations
|
||||
&& !$context->collect_mutations
|
||||
) {
|
||||
if (!NamespaceAnalyzer::isWithin($context->self, $storage->internal)) {
|
||||
if (!NamespaceAnalyzer::isWithin($namespace ?: '', $storage->internal)) {
|
||||
if (IssueBuffer::accepts(
|
||||
new InternalMethod(
|
||||
'The method ' . $codebase_methods->getCasedMethodId($method_id)
|
||||
|
@ -849,6 +849,7 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
|
||||
$codebase,
|
||||
$context,
|
||||
$method_id,
|
||||
$statements_analyzer->getNamespace(),
|
||||
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
||||
$statements_analyzer->getSuppressedIssues()
|
||||
) === false) {
|
||||
|
Loading…
Reference in New Issue
Block a user