1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Check whether constructor is internal on new call (#5843)

Fixes vimeo/psalm#5841
This commit is contained in:
Bruce Weirdan 2021-05-28 16:44:07 +03:00 committed by GitHub
parent 2a5afe3d03
commit 0c77ccc238
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -16,6 +16,7 @@ use Psalm\Issue\DeprecatedClass;
use Psalm\Issue\ImpureMethodCall;
use Psalm\Issue\InterfaceInstantiation;
use Psalm\Issue\InternalClass;
use Psalm\Issue\InternalMethod;
use Psalm\Issue\InvalidStringClass;
use Psalm\Issue\MixedMethodCall;
use Psalm\Issue\TooManyArguments;
@ -379,6 +380,25 @@ class NewAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\CallAna
if ($declaring_method_id) {
$method_storage = $codebase->methods->getStorage($declaring_method_id);
$namespace = $statements_analyzer->getNamespace() ?: '';
if (!NamespaceAnalyzer::isWithin(
$namespace,
$method_storage->internal
)) {
if (IssueBuffer::accepts(
new InternalMethod(
'Constructor ' . $codebase->methods->getCasedMethodId($declaring_method_id)
. ' is internal to ' . $method_storage->internal
. ' but called from ' . ($namespace ?: 'root namespace'),
new CodeLocation($statements_analyzer, $stmt),
(string) $method_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
if (!$method_storage->external_mutation_free && !$context->inside_throw) {
if ($context->pure) {
if (IssueBuffer::accepts(

View File

@ -957,6 +957,21 @@ class InternalAnnotationTest extends TestCase
',
'error_message' => 'psalm-internal annotation used without specifying namespace',
],
'internalConstructor' => [
'<?php
namespace A {
class C {
/** @internal */
public function __construct() {}
}
}
namespace B {
use A\C;
new C;
}
',
'error_message' => 'InternalMethod',
],
];
}
}