2016-01-08 00:28:27 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2021-12-03 21:40:18 +01:00
|
|
|
use InvalidArgumentException;
|
2016-11-02 07:29:00 +01:00
|
|
|
use PhpParser;
|
2017-07-25 22:11:02 +02:00
|
|
|
use Psalm\Aliases;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Codebase;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Context;
|
2018-11-12 16:46:55 +01:00
|
|
|
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Provider\NodeDataProvider;
|
2022-01-26 18:46:02 +01:00
|
|
|
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
|
|
|
|
use Psalm\Internal\Type\TemplateResult;
|
|
|
|
use Psalm\Internal\Type\TemplateStandinTypeReplacer;
|
2017-01-02 01:09:17 +01:00
|
|
|
use Psalm\Issue\InaccessibleProperty;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Issue\InvalidClass;
|
2023-04-20 18:25:11 +02:00
|
|
|
use Psalm\Issue\InvalidExtendClass;
|
2022-01-26 18:46:02 +01:00
|
|
|
use Psalm\Issue\InvalidTemplateParam;
|
2018-02-17 17:36:20 +01:00
|
|
|
use Psalm\Issue\MissingDependency;
|
2022-01-26 18:46:02 +01:00
|
|
|
use Psalm\Issue\MissingTemplateParam;
|
2018-01-10 06:07:47 +01:00
|
|
|
use Psalm\Issue\ReservedWord;
|
2022-01-26 18:46:02 +01:00
|
|
|
use Psalm\Issue\TooManyTemplateParams;
|
2020-10-24 06:10:22 +02:00
|
|
|
use Psalm\Issue\UndefinedAttributeClass;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\UndefinedClass;
|
2019-05-16 00:41:26 +02:00
|
|
|
use Psalm\Issue\UndefinedDocblockClass;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\IssueBuffer;
|
2021-01-06 15:05:53 +01:00
|
|
|
use Psalm\Plugin\EventHandler\Event\AfterClassLikeExistenceCheckEvent;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\StatementsSource;
|
2016-12-30 18:41:14 +01:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Type;
|
2023-04-20 18:25:11 +02:00
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2022-01-26 18:46:02 +01:00
|
|
|
use Psalm\Type\Atomic\TTemplateParam;
|
2021-12-13 16:28:14 +01:00
|
|
|
use Psalm\Type\Union;
|
2021-12-03 21:40:18 +01:00
|
|
|
use UnexpectedValueException;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2022-01-26 18:46:02 +01:00
|
|
|
use function array_keys;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function array_pop;
|
2022-01-26 18:46:02 +01:00
|
|
|
use function array_search;
|
|
|
|
use function count;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function explode;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function gettype;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function implode;
|
|
|
|
use function in_array;
|
|
|
|
use function preg_match;
|
|
|
|
use function preg_replace;
|
|
|
|
use function strtolower;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-10-15 19:23:35 +02:00
|
|
|
abstract class ClassLikeAnalyzer extends SourceAnalyzer
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2020-09-20 18:54:46 +02:00
|
|
|
public const VISIBILITY_PUBLIC = 1;
|
|
|
|
public const VISIBILITY_PROTECTED = 2;
|
|
|
|
public const VISIBILITY_PRIVATE = 3;
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2020-09-20 18:54:46 +02:00
|
|
|
public const SPECIAL_TYPES = [
|
2017-01-02 21:31:18 +01:00
|
|
|
'int' => 'int',
|
2018-03-31 01:04:21 +02:00
|
|
|
'string' => 'string',
|
2017-01-02 21:31:18 +01:00
|
|
|
'float' => 'float',
|
|
|
|
'bool' => 'bool',
|
|
|
|
'false' => 'false',
|
|
|
|
'object' => 'object',
|
2021-10-13 19:37:47 +02:00
|
|
|
'never' => 'never',
|
2017-01-02 21:31:18 +01:00
|
|
|
'callable' => 'callable',
|
|
|
|
'array' => 'array',
|
|
|
|
'iterable' => 'iterable',
|
|
|
|
'null' => 'null',
|
|
|
|
'mixed' => 'mixed',
|
2016-11-02 07:29:00 +01:00
|
|
|
];
|
2016-07-25 06:31:29 +02:00
|
|
|
|
2020-09-20 18:54:46 +02:00
|
|
|
public const GETTYPE_TYPES = [
|
2017-09-11 17:52:34 +02:00
|
|
|
'boolean' => true,
|
|
|
|
'integer' => true,
|
|
|
|
'double' => true,
|
|
|
|
'string' => true,
|
|
|
|
'array' => true,
|
|
|
|
'object' => true,
|
|
|
|
'resource' => true,
|
2020-01-17 17:55:16 +01:00
|
|
|
'resource (closed)' => true,
|
2017-09-11 17:52:34 +02:00
|
|
|
'NULL' => true,
|
|
|
|
'unknown type' => true,
|
|
|
|
];
|
|
|
|
|
2022-12-14 01:52:54 +01:00
|
|
|
protected PhpParser\Node\Stmt\ClassLike $class;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
2022-12-14 01:52:54 +01:00
|
|
|
public FileAnalyzer $file_analyzer;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2022-12-14 01:52:54 +01:00
|
|
|
protected string $fq_class_name;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The parent class
|
|
|
|
*/
|
2022-12-14 01:52:54 +01:00
|
|
|
protected ?string $parent_fq_class_name = null;
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2022-12-14 01:52:54 +01:00
|
|
|
protected ClassLikeStorage $storage;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function __construct(PhpParser\Node\Stmt\ClassLike $class, SourceAnalyzer $source, string $fq_class_name)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->class = $class;
|
2016-11-13 00:51:48 +01:00
|
|
|
$this->source = $source;
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->file_analyzer = $source->getFileAnalyzer();
|
2016-11-07 23:29:51 +01:00
|
|
|
$this->fq_class_name = $fq_class_name;
|
2018-11-11 18:19:53 +01:00
|
|
|
$codebase = $source->getCodebase();
|
|
|
|
$this->storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
2017-01-07 21:57:25 +01:00
|
|
|
}
|
|
|
|
|
2018-11-18 17:39:14 +01:00
|
|
|
public function __destruct()
|
|
|
|
{
|
2021-09-25 18:11:54 +02:00
|
|
|
unset($this->source);
|
|
|
|
unset($this->file_analyzer);
|
2018-11-18 17:39:14 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
public function getMethodMutations(
|
2020-09-07 01:36:47 +02:00
|
|
|
string $method_name,
|
2017-01-12 03:37:53 +01:00
|
|
|
Context $context
|
2020-09-12 17:24:05 +02:00
|
|
|
): void {
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer = $this->getFileAnalyzer()->project_analyzer;
|
|
|
|
$codebase = $project_analyzer->getCodebase();
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
foreach ($this->class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
|
2018-04-17 18:16:25 +02:00
|
|
|
strtolower($stmt->name->name) === strtolower($method_name)
|
2017-01-12 03:37:53 +01:00
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$method_analyzer = new MethodAnalyzer($stmt, $this);
|
2017-01-12 03:37:53 +01:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$method_analyzer->analyze($context, new NodeDataProvider(), null, true);
|
2020-04-08 07:03:37 +02:00
|
|
|
|
|
|
|
$context->clauses = [];
|
2017-01-06 07:07:11 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
|
|
|
|
foreach ($stmt->traits as $trait) {
|
2017-01-12 03:37:53 +01:00
|
|
|
$fq_trait_name = self::getFQCLNFromNameObject(
|
2017-01-06 07:07:11 +01:00
|
|
|
$trait,
|
2022-12-18 17:15:15 +01:00
|
|
|
$this->source->getAliases(),
|
2017-01-06 07:07:11 +01:00
|
|
|
);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$trait_file_analyzer = $project_analyzer->getFileAnalyzerForClassLike($fq_trait_name);
|
2018-02-04 00:52:35 +01:00
|
|
|
$trait_node = $codebase->classlikes->getTraitNode($fq_trait_name);
|
2020-03-03 04:27:54 +01:00
|
|
|
$trait_storage = $codebase->classlike_storage_provider->get($fq_trait_name);
|
|
|
|
$trait_aliases = $trait_storage->aliases;
|
|
|
|
|
|
|
|
if ($trait_aliases === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$trait_analyzer = new TraitAnalyzer(
|
2018-01-21 18:44:46 +01:00
|
|
|
$trait_node,
|
2018-11-11 18:01:14 +01:00
|
|
|
$trait_file_analyzer,
|
2018-01-21 18:44:46 +01:00
|
|
|
$fq_trait_name,
|
2022-12-18 17:15:15 +01:00
|
|
|
$trait_aliases,
|
2018-01-21 18:44:46 +01:00
|
|
|
);
|
2017-01-12 03:37:53 +01:00
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
foreach ($trait_node->stmts as $trait_stmt) {
|
2017-01-12 03:37:53 +01:00
|
|
|
if ($trait_stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
|
2018-04-17 18:16:25 +02:00
|
|
|
strtolower($trait_stmt->name->name) === strtolower($method_name)
|
2017-01-12 03:37:53 +01:00
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$method_analyzer = new MethodAnalyzer($trait_stmt, $trait_analyzer);
|
2017-01-12 03:37:53 +01:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$actual_method_id = $method_analyzer->getMethodId();
|
2017-01-06 07:07:11 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
if ($context->self && $context->self !== $this->fq_class_name) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$analyzed_method_id = $method_analyzer->getMethodId($context->self);
|
2018-02-04 00:52:35 +01:00
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($analyzed_method_id);
|
2017-01-12 03:37:53 +01:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if ((string) $actual_method_id !== (string) $declaring_method_id) {
|
2017-01-12 03:37:53 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$method_analyzer->analyze(
|
|
|
|
$context,
|
2021-12-03 20:11:20 +01:00
|
|
|
new NodeDataProvider(),
|
2019-11-25 17:44:54 +01:00
|
|
|
null,
|
2022-12-18 17:15:15 +01:00
|
|
|
true,
|
2019-11-25 17:44:54 +01:00
|
|
|
);
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-30 03:06:21 +02:00
|
|
|
|
|
|
|
$trait_file_analyzer->clearSourceBeforeDestruction();
|
2017-01-06 07:07:11 +01:00
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function getFunctionLikeAnalyzer(string $method_name): ?MethodAnalyzer
|
2019-05-31 07:47:35 +02:00
|
|
|
{
|
|
|
|
foreach ($this->class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
|
|
|
|
strtolower($stmt->name->name) === strtolower($method_name)
|
|
|
|
) {
|
|
|
|
return new MethodAnalyzer($stmt, $this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 17:55:24 +02:00
|
|
|
return null;
|
2019-05-31 07:47:35 +02:00
|
|
|
}
|
|
|
|
|
2016-07-25 05:38:52 +02:00
|
|
|
/**
|
2016-11-05 01:10:59 +01:00
|
|
|
* @param array<string> $suppressed_issues
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-11-08 01:16:51 +01:00
|
|
|
public static function checkFullyQualifiedClassLikeName(
|
2018-01-02 02:04:03 +01:00
|
|
|
StatementsSource $statements_source,
|
2019-06-01 06:56:54 +02:00
|
|
|
string $fq_class_name,
|
2016-12-04 01:11:30 +01:00
|
|
|
CodeLocation $code_location,
|
2020-02-11 22:39:33 +01:00
|
|
|
?string $calling_fq_class_name,
|
2020-03-26 17:35:27 +01:00
|
|
|
?string $calling_method_id,
|
2017-03-02 18:19:18 +01:00
|
|
|
array $suppressed_issues,
|
2021-04-30 21:01:33 +02:00
|
|
|
?ClassLikeNameOptions $options = null
|
2020-09-13 22:39:06 +02:00
|
|
|
): ?bool {
|
2021-04-30 21:01:33 +02:00
|
|
|
if ($options === null) {
|
|
|
|
$options = new ClassLikeNameOptions();
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase = $statements_source->getCodebase();
|
2020-09-20 00:26:51 +02:00
|
|
|
if ($fq_class_name === '') {
|
2018-03-06 19:59:59 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedClass(
|
|
|
|
'Class or interface <empty string> does not exist',
|
2018-03-21 15:17:57 +01:00
|
|
|
$code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
'empty string',
|
2018-03-06 19:59:59 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$suppressed_issues,
|
2018-03-06 19:59:59 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-13 22:39:06 +02:00
|
|
|
return null;
|
2016-04-12 17:59:27 +02:00
|
|
|
}
|
|
|
|
|
2022-09-08 18:51:33 +02:00
|
|
|
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name, 1);
|
2016-03-17 19:06:01 +01:00
|
|
|
|
2018-05-21 06:46:56 +02:00
|
|
|
if (in_array($fq_class_name, ['callable', 'iterable', 'self', 'static', 'parent'], true)) {
|
2016-12-05 03:04:25 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:07:47 +01:00
|
|
|
if (preg_match(
|
2020-02-12 04:34:34 +01:00
|
|
|
'/(^|\\\)(int|float|bool|string|void|null|false|true|object|mixed)$/i',
|
2022-12-18 17:15:15 +01:00
|
|
|
$fq_class_name,
|
2019-08-26 06:08:18 +02:00
|
|
|
) || strtolower($fq_class_name) === 'resource'
|
2018-01-10 06:07:47 +01:00
|
|
|
) {
|
|
|
|
$class_name_parts = explode('\\', $fq_class_name);
|
|
|
|
$class_name = array_pop($class_name_parts);
|
|
|
|
|
2021-11-29 20:54:17 +01:00
|
|
|
IssueBuffer::maybeAdd(
|
2018-01-10 06:07:47 +01:00
|
|
|
new ReservedWord(
|
|
|
|
$class_name . ' is a reserved word',
|
2018-03-21 15:17:57 +01:00
|
|
|
$code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
$class_name,
|
2018-01-10 06:07:47 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$suppressed_issues,
|
2021-11-29 20:54:17 +01:00
|
|
|
);
|
2018-01-10 06:07:47 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-13 21:38:09 +02:00
|
|
|
$class_exists = $codebase->classlikes->classExists(
|
|
|
|
$fq_class_name,
|
2021-04-30 21:01:33 +02:00
|
|
|
!$options->inferred ? $code_location : null,
|
2020-03-26 17:35:27 +01:00
|
|
|
$calling_fq_class_name,
|
2022-12-18 17:15:15 +01:00
|
|
|
$calling_method_id,
|
2019-04-13 21:38:09 +02:00
|
|
|
);
|
2021-05-03 23:54:09 +02:00
|
|
|
|
2019-04-13 21:38:09 +02:00
|
|
|
$interface_exists = $codebase->classlikes->interfaceExists(
|
|
|
|
$fq_class_name,
|
2021-04-30 21:01:33 +02:00
|
|
|
!$options->inferred ? $code_location : null,
|
2020-03-26 17:35:27 +01:00
|
|
|
$calling_fq_class_name,
|
2022-12-18 17:15:15 +01:00
|
|
|
$calling_method_id,
|
2019-04-13 21:38:09 +02:00
|
|
|
);
|
2016-08-14 00:54:49 +02:00
|
|
|
|
2021-05-03 23:54:09 +02:00
|
|
|
$enum_exists = $codebase->classlikes->enumExists(
|
|
|
|
$fq_class_name,
|
|
|
|
!$options->inferred ? $code_location : null,
|
|
|
|
$calling_fq_class_name,
|
2022-12-18 17:15:15 +01:00
|
|
|
$calling_method_id,
|
2021-05-03 23:54:09 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!$class_exists
|
|
|
|
&& !($interface_exists && $options->allow_interface)
|
|
|
|
&& !($enum_exists && $options->allow_enum)
|
|
|
|
) {
|
2021-04-30 21:01:33 +02:00
|
|
|
if (!$options->allow_trait || !$codebase->classlikes->traitExists($fq_class_name, $code_location)) {
|
|
|
|
if ($options->from_docblock) {
|
2019-05-16 00:41:26 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedDocblockClass(
|
2021-05-03 23:54:09 +02:00
|
|
|
'Docblock-defined class, interface or enum named ' . $fq_class_name . ' does not exist',
|
2019-05-16 00:41:26 +02:00
|
|
|
$code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
$fq_class_name,
|
2019-05-16 00:41:26 +02:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$suppressed_issues,
|
2019-05-16 00:41:26 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-30 21:01:33 +02:00
|
|
|
} elseif ($options->from_attribute) {
|
2020-10-24 06:10:22 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedAttributeClass(
|
|
|
|
'Attribute class ' . $fq_class_name . ' does not exist',
|
|
|
|
$code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
$fq_class_name,
|
2020-10-24 06:10:22 +02:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$suppressed_issues,
|
2020-10-24 06:10:22 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-16 00:41:26 +02:00
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedClass(
|
2021-05-03 23:54:09 +02:00
|
|
|
'Class, interface or enum named ' . $fq_class_name . ' does not exist',
|
2019-05-16 00:41:26 +02:00
|
|
|
$code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
$fq_class_name,
|
2019-05-16 00:41:26 +02:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$suppressed_issues,
|
2019-05-16 00:41:26 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-09 04:32:57 +02:00
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-07-24 23:06:36 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$aliased_name = $codebase->classlikes->getUnAliasedName(
|
2022-12-18 17:15:15 +01:00
|
|
|
$fq_class_name,
|
2018-11-29 06:05:56 +01:00
|
|
|
);
|
|
|
|
|
2018-10-09 01:57:18 +02:00
|
|
|
try {
|
2020-02-15 02:54:26 +01:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($aliased_name);
|
2021-12-03 21:40:18 +01:00
|
|
|
} catch (InvalidArgumentException $e) {
|
2021-04-30 21:01:33 +02:00
|
|
|
if (!$options->inferred) {
|
2018-10-09 01:57:18 +02:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-02-14 19:34:16 +01:00
|
|
|
|
2023-04-20 18:25:11 +02:00
|
|
|
|
|
|
|
$classUnion = new Union([new TNamedObject($fq_class_name)]);
|
|
|
|
foreach ($class_storage->parent_classes as $parent_class) {
|
|
|
|
$parent_storage = $codebase->classlikes->getStorageFor($parent_class);
|
|
|
|
if ($parent_storage && $parent_storage->inheritors) {
|
|
|
|
if (!UnionTypeComparator::isContainedBy($codebase, $classUnion, $parent_storage->inheritors)) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new InvalidExtendClass(
|
|
|
|
'Class ' . $fq_class_name . ' is not an allowed inheritor of parent class ' . $parent_class,
|
|
|
|
$code_location,
|
|
|
|
$fq_class_name,
|
|
|
|
),
|
|
|
|
$suppressed_issues,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:22:40 +01:00
|
|
|
foreach ($class_storage->invalid_dependencies as $dependency_class_name => $_) {
|
2019-04-21 17:30:42 +02:00
|
|
|
// if the implemented/extended class is stubbed, it may not yet have
|
|
|
|
// been hydrated
|
|
|
|
if ($codebase->classlike_storage_provider->has($dependency_class_name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-14 19:34:16 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2018-02-17 17:36:20 +01:00
|
|
|
new MissingDependency(
|
2018-02-14 19:34:16 +01:00
|
|
|
$fq_class_name . ' depends on class or interface '
|
|
|
|
. $dependency_class_name . ' that does not exist',
|
2018-03-21 15:17:57 +01:00
|
|
|
$code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
$fq_class_name,
|
2018-02-14 19:34:16 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$suppressed_issues,
|
2018-02-14 19:34:16 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 21:01:33 +02:00
|
|
|
if (!$options->inferred) {
|
2021-05-03 23:54:09 +02:00
|
|
|
if (($class_exists && !$codebase->classHasCorrectCasing($fq_class_name))
|
|
|
|
|| ($interface_exists && !$codebase->interfaceHasCorrectCasing($fq_class_name))
|
|
|
|
|| ($enum_exists && !$codebase->classlikes->enumHasCorrectCasing($fq_class_name))
|
2020-02-15 02:54:26 +01:00
|
|
|
) {
|
2022-09-07 13:20:31 +02:00
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new InvalidClass(
|
|
|
|
'Class, interface or enum ' . $fq_class_name . ' has wrong casing',
|
|
|
|
$code_location,
|
2022-12-18 17:15:15 +01:00
|
|
|
$fq_class_name,
|
2022-09-07 13:20:31 +02:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$suppressed_issues,
|
2022-09-07 13:20:31 +02:00
|
|
|
);
|
2016-08-10 07:09:47 +02:00
|
|
|
}
|
2016-03-17 19:06:01 +01:00
|
|
|
}
|
|
|
|
|
2021-04-30 21:01:33 +02:00
|
|
|
if (!$options->inferred) {
|
2021-01-06 15:05:53 +01:00
|
|
|
$event = new AfterClassLikeExistenceCheckEvent(
|
|
|
|
$fq_class_name,
|
|
|
|
$code_location,
|
|
|
|
$statements_source,
|
|
|
|
$codebase,
|
2022-12-18 17:15:15 +01:00
|
|
|
[],
|
2021-01-06 15:05:53 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$codebase->config->eventDispatcher->dispatchAfterClassLikeExistenceCheck($event);
|
|
|
|
|
|
|
|
$file_manipulations = $event->getFileReplacements();
|
|
|
|
if ($file_manipulations) {
|
|
|
|
FileManipulationBuffer::add($code_location->file_path, $file_manipulations);
|
2018-01-02 03:17:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 23:06:36 +02:00
|
|
|
return true;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* Gets the fully-qualified class name from a Name object
|
2016-10-09 23:54:58 +02:00
|
|
|
*/
|
2018-10-27 05:04:38 +02:00
|
|
|
public static function getFQCLNFromNameObject(
|
|
|
|
PhpParser\Node\Name $class_name,
|
|
|
|
Aliases $aliases
|
2020-09-04 22:26:33 +02:00
|
|
|
): string {
|
2018-07-13 03:25:06 +02:00
|
|
|
/** @var string|null */
|
|
|
|
$resolved_name = $class_name->getAttribute('resolvedName');
|
|
|
|
|
|
|
|
if ($resolved_name) {
|
|
|
|
return $resolved_name;
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
if ($class_name instanceof PhpParser\Node\Name\FullyQualified) {
|
2016-02-18 21:05:13 +01:00
|
|
|
return implode('\\', $class_name->parts);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (in_array($class_name->parts[0], ['self', 'static', 'parent'], true)) {
|
|
|
|
return $class_name->parts[0];
|
|
|
|
}
|
|
|
|
|
2018-02-04 18:23:32 +01:00
|
|
|
return Type::getFQCLNFromString(
|
2017-01-06 07:07:11 +01:00
|
|
|
implode('\\', $class_name->parts),
|
2022-12-18 17:15:15 +01:00
|
|
|
$aliases,
|
2017-01-06 07:07:11 +01:00
|
|
|
);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
/**
|
2022-11-04 19:04:23 +01:00
|
|
|
* @psalm-mutation-free
|
2020-12-29 12:42:12 +01:00
|
|
|
* @return array<lowercase-string, string>
|
2016-11-13 00:51:48 +01:00
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getAliasedClassesFlipped(): array
|
2016-11-13 00:51:48 +01:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
if ($this->source instanceof NamespaceAnalyzer || $this->source instanceof FileAnalyzer) {
|
2016-11-13 17:24:46 +01:00
|
|
|
return $this->source->getAliasedClassesFlipped();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
2016-11-13 00:51:48 +01:00
|
|
|
}
|
|
|
|
|
2019-06-06 04:13:33 +02:00
|
|
|
/**
|
2022-11-04 19:04:23 +01:00
|
|
|
* @psalm-mutation-free
|
2019-06-06 04:13:33 +02:00
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getAliasedClassesFlippedReplaceable(): array
|
2019-06-06 04:13:33 +02:00
|
|
|
{
|
|
|
|
if ($this->source instanceof NamespaceAnalyzer || $this->source instanceof FileAnalyzer) {
|
|
|
|
return $this->source->getAliasedClassesFlippedReplaceable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-11-04 19:04:23 +01:00
|
|
|
/** @psalm-mutation-free */
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getFQCLN(): string
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-11-07 23:29:51 +01:00
|
|
|
return $this->fq_class_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:04:23 +01:00
|
|
|
/** @psalm-mutation-free */
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getClassName(): ?string
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2021-09-26 22:57:04 +02:00
|
|
|
return $this->class->name->name ?? null;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2018-12-18 05:29:27 +01:00
|
|
|
/**
|
2022-11-04 19:04:23 +01:00
|
|
|
* @psalm-mutation-free
|
2021-12-13 16:28:14 +01:00
|
|
|
* @return array<string, array<string, Union>>|null
|
2018-12-18 05:29:27 +01:00
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getTemplateTypeMap(): ?array
|
2018-12-18 05:29:27 +01:00
|
|
|
{
|
|
|
|
return $this->storage->template_types;
|
|
|
|
}
|
|
|
|
|
2022-11-04 19:04:23 +01:00
|
|
|
/** @psalm-mutation-free */
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getParentFQCLN(): ?string
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2017-01-07 20:35:07 +01:00
|
|
|
return $this->parent_fq_class_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:04:23 +01:00
|
|
|
/** @psalm-mutation-free */
|
2020-09-04 22:26:33 +02:00
|
|
|
public function isStatic(): bool
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
/**
|
|
|
|
* Gets the Psalm type from a particular value
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2021-12-13 16:28:14 +01:00
|
|
|
public static function getTypeFromValue($value): Union
|
2016-11-21 03:49:06 +01:00
|
|
|
{
|
|
|
|
switch (gettype($value)) {
|
|
|
|
case 'boolean':
|
2018-05-31 22:49:01 +02:00
|
|
|
if ($value) {
|
|
|
|
return Type::getTrue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Type::getFalse();
|
2016-11-21 03:49:06 +01:00
|
|
|
|
|
|
|
case 'integer':
|
2018-05-31 22:49:01 +02:00
|
|
|
return Type::getInt(false, $value);
|
2016-11-21 03:49:06 +01:00
|
|
|
|
|
|
|
case 'double':
|
2018-05-31 22:49:01 +02:00
|
|
|
return Type::getFloat($value);
|
2016-11-21 03:49:06 +01:00
|
|
|
|
|
|
|
case 'string':
|
2018-05-31 22:49:01 +02:00
|
|
|
return Type::getString($value);
|
2016-11-21 03:49:06 +01:00
|
|
|
|
|
|
|
case 'array':
|
|
|
|
return Type::getArray();
|
|
|
|
|
|
|
|
case 'NULL':
|
|
|
|
return Type::getNull();
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
/**
|
2019-06-16 15:12:32 +02:00
|
|
|
* @param string[] $suppressed_issues
|
2017-01-02 01:09:17 +01:00
|
|
|
*/
|
|
|
|
public static function checkPropertyVisibility(
|
2020-09-07 01:36:47 +02:00
|
|
|
string $property_id,
|
2019-03-01 14:57:10 +01:00
|
|
|
Context $context,
|
2018-11-06 03:57:36 +01:00
|
|
|
SourceAnalyzer $source,
|
2017-01-02 01:09:17 +01:00
|
|
|
CodeLocation $code_location,
|
2018-01-13 07:52:46 +01:00
|
|
|
array $suppressed_issues,
|
2020-09-07 01:36:47 +02:00
|
|
|
bool $emit_issues = true
|
2020-09-04 22:26:33 +02:00
|
|
|
): ?bool {
|
2020-09-22 06:44:31 +02:00
|
|
|
[$fq_class_name, $property_name] = explode('::$', $property_id);
|
2019-03-01 14:57:10 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase = $source->getCodebase();
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
if ($codebase->properties->property_visibility_provider->has($fq_class_name)) {
|
|
|
|
$property_visible = $codebase->properties->property_visibility_provider->isPropertyVisible(
|
|
|
|
$source,
|
|
|
|
$fq_class_name,
|
|
|
|
$property_name,
|
2020-01-06 00:53:24 +01:00
|
|
|
true,
|
2020-08-31 22:40:16 +02:00
|
|
|
$context,
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2019-03-01 14:57:10 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($property_visible !== null) {
|
|
|
|
return $property_visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 06:05:56 +01:00
|
|
|
$declaring_property_class = $codebase->properties->getDeclaringClassForProperty(
|
2019-03-01 14:57:10 +01:00
|
|
|
$property_id,
|
2022-12-18 17:15:15 +01:00
|
|
|
true,
|
2018-11-29 06:05:56 +01:00
|
|
|
);
|
|
|
|
$appearing_property_class = $codebase->properties->getAppearingClassForProperty(
|
2019-03-01 14:57:10 +01:00
|
|
|
$property_id,
|
2022-12-18 17:15:15 +01:00
|
|
|
true,
|
2018-11-29 06:05:56 +01:00
|
|
|
);
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
if (!$declaring_property_class || !$appearing_property_class) {
|
2021-12-03 21:40:18 +01:00
|
|
|
throw new UnexpectedValueException(
|
2022-12-18 17:15:15 +01:00
|
|
|
'Appearing/Declaring classes are not defined for ' . $property_id,
|
2017-01-02 01:09:17 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the calling class is the same, we know the property exists, so it must be visible
|
2019-03-01 14:57:10 +01:00
|
|
|
if ($appearing_property_class === $context->self) {
|
2018-01-13 07:52:46 +01:00
|
|
|
return $emit_issues ? null : true;
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if ($source->getSource() instanceof TraitAnalyzer
|
|
|
|
&& strtolower($declaring_property_class) === strtolower((string) $source->getFQCLN())
|
|
|
|
) {
|
2018-01-13 07:52:46 +01:00
|
|
|
return $emit_issues ? null : true;
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:19:53 +01:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($declaring_property_class);
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-11-24 18:10:30 +01:00
|
|
|
if (!isset($class_storage->properties[$property_name])) {
|
2021-12-03 21:40:18 +01:00
|
|
|
throw new UnexpectedValueException('$storage should not be null for ' . $property_id);
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:10:30 +01:00
|
|
|
$storage = $class_storage->properties[$property_name];
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
switch ($storage->visibility) {
|
|
|
|
case self::VISIBILITY_PUBLIC:
|
2018-01-13 07:52:46 +01:00
|
|
|
return $emit_issues ? null : true;
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
case self::VISIBILITY_PRIVATE:
|
2022-12-17 05:00:34 +01:00
|
|
|
if ($emit_issues) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access private property ' . $property_id . ' from context ' . $context->self,
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2022-12-17 05:00:34 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$suppressed_issues,
|
2022-12-17 05:00:34 +01:00
|
|
|
);
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2021-09-26 22:19:42 +02:00
|
|
|
return null;
|
2017-01-02 01:09:17 +01:00
|
|
|
case self::VISIBILITY_PROTECTED:
|
2019-03-01 14:57:10 +01:00
|
|
|
if (!$context->self) {
|
2022-12-17 05:00:34 +01:00
|
|
|
if ($emit_issues) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access protected property ' . $property_id,
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2022-12-17 05:00:34 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$suppressed_issues,
|
2022-12-17 05:00:34 +01:00
|
|
|
);
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
if ($codebase->classExtends($appearing_property_class, $context->self)) {
|
2018-01-13 07:52:46 +01:00
|
|
|
return $emit_issues ? null : true;
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
if (!$codebase->classExtends($context->self, $appearing_property_class)) {
|
2022-12-17 05:00:34 +01:00
|
|
|
if ($emit_issues) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access protected property ' . $property_id . ' from context ' . $context->self,
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2022-12-17 05:00:34 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$suppressed_issues,
|
2022-12-17 05:00:34 +01:00
|
|
|
);
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
2018-01-13 07:52:46 +01:00
|
|
|
|
|
|
|
return null;
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 07:52:46 +01:00
|
|
|
return $emit_issues ? null : true;
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 18:46:02 +01:00
|
|
|
protected function checkTemplateParams(
|
|
|
|
Codebase $codebase,
|
|
|
|
ClassLikeStorage $storage,
|
|
|
|
ClassLikeStorage $parent_storage,
|
|
|
|
CodeLocation $code_location,
|
|
|
|
int $given_param_count
|
|
|
|
): void {
|
|
|
|
$expected_param_count = $parent_storage->template_types === null
|
|
|
|
? 0
|
|
|
|
: count($parent_storage->template_types);
|
|
|
|
|
|
|
|
if ($expected_param_count > $given_param_count) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new MissingTemplateParam(
|
|
|
|
$storage->name . ' has missing template params when extending ' . $parent_storage->name
|
|
|
|
. ', expecting ' . $expected_param_count,
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2022-01-26 18:46:02 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues(),
|
2022-01-26 18:46:02 +01:00
|
|
|
);
|
|
|
|
} elseif ($expected_param_count < $given_param_count) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new TooManyTemplateParams(
|
|
|
|
$storage->name . ' has too many template params when extending ' . $parent_storage->name
|
|
|
|
. ', expecting ' . $expected_param_count,
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2022-01-26 18:46:02 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues(),
|
2022-01-26 18:46:02 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage_param_count = ($storage->template_types ? count($storage->template_types) : 0);
|
|
|
|
|
|
|
|
if ($parent_storage->enforce_template_inheritance
|
|
|
|
&& $expected_param_count !== $storage_param_count
|
|
|
|
) {
|
|
|
|
if ($expected_param_count > $storage_param_count) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new MissingTemplateParam(
|
|
|
|
$storage->name . ' requires the same number of template params as ' . $parent_storage->name
|
|
|
|
. ' but saw ' . $storage_param_count,
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2022-01-26 18:46:02 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues(),
|
2022-01-26 18:46:02 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new TooManyTemplateParams(
|
|
|
|
$storage->name . ' requires the same number of template params as ' . $parent_storage->name
|
|
|
|
. ' but saw ' . $storage_param_count,
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2022-01-26 18:46:02 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues(),
|
2022-01-26 18:46:02 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parent_storage->template_types && $storage->template_extended_params) {
|
|
|
|
$i = 0;
|
|
|
|
|
|
|
|
$previous_extended = [];
|
|
|
|
|
|
|
|
foreach ($parent_storage->template_types as $template_name => $type_map) {
|
|
|
|
// declares the variables
|
|
|
|
foreach ($type_map as $declaring_class => $template_type) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($storage->template_extended_params[$parent_storage->name][$template_name])) {
|
|
|
|
$extended_type = $storage->template_extended_params[$parent_storage->name][$template_name];
|
|
|
|
|
|
|
|
if (isset($parent_storage->template_covariants[$i])
|
|
|
|
&& !$parent_storage->template_covariants[$i]
|
|
|
|
) {
|
|
|
|
foreach ($extended_type->getAtomicTypes() as $t) {
|
|
|
|
if ($t instanceof TTemplateParam
|
|
|
|
&& $storage->template_types
|
|
|
|
&& $storage->template_covariants
|
|
|
|
&& ($local_offset
|
|
|
|
= array_search($t->param_name, array_keys($storage->template_types)))
|
|
|
|
!== false
|
|
|
|
&& !empty($storage->template_covariants[$local_offset])
|
|
|
|
) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new InvalidTemplateParam(
|
|
|
|
'Cannot extend an invariant template param ' . $template_name
|
|
|
|
. ' into a covariant context',
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2022-01-26 18:46:02 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues(),
|
2022-01-26 18:46:02 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parent_storage->enforce_template_inheritance) {
|
|
|
|
foreach ($extended_type->getAtomicTypes() as $t) {
|
|
|
|
if (!$t instanceof TTemplateParam
|
|
|
|
|| !isset($storage->template_types[$t->param_name])
|
|
|
|
) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new InvalidTemplateParam(
|
|
|
|
'Cannot extend a strictly-enforced parent template param '
|
|
|
|
. $template_name
|
|
|
|
. ' with a non-template type',
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2022-01-26 18:46:02 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues(),
|
2022-01-26 18:46:02 +01:00
|
|
|
);
|
|
|
|
} elseif ($storage->template_types[$t->param_name][$storage->name]->getId()
|
|
|
|
!== $template_type->getId()
|
|
|
|
) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new InvalidTemplateParam(
|
|
|
|
'Cannot extend a strictly-enforced parent template param '
|
|
|
|
. $template_name
|
|
|
|
. ' with constraint ' . $template_type->getId()
|
|
|
|
. ' with a child template param ' . $t->param_name
|
|
|
|
. ' with different constraint '
|
|
|
|
. $storage->template_types[$t->param_name][$storage->name]->getId(),
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2022-01-26 18:46:02 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues(),
|
2022-01-26 18:46:02 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$template_type->isMixed()) {
|
|
|
|
$template_result = new TemplateResult(
|
|
|
|
$previous_extended ?: [],
|
2022-12-18 17:15:15 +01:00
|
|
|
[],
|
2022-01-26 18:46:02 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$template_type_copy = TemplateStandinTypeReplacer::replace(
|
2022-11-04 19:04:23 +01:00
|
|
|
$template_type,
|
2022-01-26 18:46:02 +01:00
|
|
|
$template_result,
|
|
|
|
$codebase,
|
|
|
|
null,
|
|
|
|
$extended_type,
|
|
|
|
null,
|
2022-12-18 17:15:15 +01:00
|
|
|
null,
|
2022-01-26 18:46:02 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!UnionTypeComparator::isContainedBy($codebase, $extended_type, $template_type_copy)) {
|
|
|
|
IssueBuffer::maybeAdd(
|
|
|
|
new InvalidTemplateParam(
|
|
|
|
'Extended template param ' . $template_name
|
|
|
|
. ' expects type ' . $template_type_copy->getId()
|
|
|
|
. ', type ' . $extended_type->getId() . ' given',
|
2022-12-18 17:15:15 +01:00
|
|
|
$code_location,
|
2022-01-26 18:46:02 +01:00
|
|
|
),
|
2022-12-18 17:15:15 +01:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues(),
|
2022-01-26 18:46:02 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$previous_extended[$template_name] = [
|
2022-12-18 17:15:15 +01:00
|
|
|
$declaring_class => $extended_type,
|
2022-01-26 18:46:02 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$previous_extended[$template_name] = [
|
2022-12-18 17:15:15 +01:00
|
|
|
$declaring_class => $extended_type,
|
2022-01-26 18:46:02 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2018-02-12 04:49:19 +01:00
|
|
|
* @return array<string, string>
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2020-09-07 01:36:47 +02:00
|
|
|
public static function getClassesForFile(Codebase $codebase, string $file_path): array
|
2016-10-05 01:23:38 +02:00
|
|
|
{
|
2017-07-29 21:05:06 +02:00
|
|
|
try {
|
2018-10-07 02:11:19 +02:00
|
|
|
return $codebase->file_storage_provider->get($file_path)->classlikes_in_file;
|
2021-12-03 21:40:18 +01:00
|
|
|
} catch (InvalidArgumentException $e) {
|
2017-07-29 21:05:06 +02:00
|
|
|
return [];
|
|
|
|
}
|
2016-10-05 01:23:38 +02:00
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function getFileAnalyzer(): FileAnalyzer
|
2017-07-29 21:05:06 +02:00
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
return $this->file_analyzer;
|
2017-07-29 21:05:06 +02:00
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|