2016-08-13 20:20:46 +02:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer;
|
2016-08-13 20:20:46 +02:00
|
|
|
|
|
|
|
use PhpParser;
|
2018-05-30 19:08:56 +02:00
|
|
|
use Psalm\Aliases;
|
2020-08-10 16:50:37 +02:00
|
|
|
use Psalm\DocComment;
|
|
|
|
use Psalm\Exception\DocblockParseException;
|
2020-03-12 04:15:15 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Call\ClassTemplateParamCollector;
|
2020-07-08 18:03:12 +02:00
|
|
|
use Psalm\Internal\FileManipulation\PropertyDocblockManipulator;
|
2020-04-17 07:16:15 +02:00
|
|
|
use Psalm\Internal\Type\UnionTemplateHandler;
|
2020-07-22 01:40:35 +02:00
|
|
|
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
|
2018-10-07 04:58:21 +02:00
|
|
|
use Psalm\Codebase;
|
2018-01-14 19:08:24 +01:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2018-01-23 15:09:43 +01:00
|
|
|
use Psalm\Issue\DeprecatedClass;
|
|
|
|
use Psalm\Issue\DeprecatedInterface;
|
2018-08-10 19:25:25 +02:00
|
|
|
use Psalm\Issue\DeprecatedTrait;
|
2018-01-14 19:08:24 +01:00
|
|
|
use Psalm\Issue\InaccessibleMethod;
|
2018-12-02 00:37:49 +01:00
|
|
|
use Psalm\Issue\InternalClass;
|
2020-06-13 06:29:59 +02:00
|
|
|
use Psalm\Issue\InvalidExtendClass;
|
2019-01-25 21:31:17 +01:00
|
|
|
use Psalm\Issue\InvalidTemplateParam;
|
2019-03-04 00:13:15 +01:00
|
|
|
use Psalm\Issue\MethodSignatureMismatch;
|
2018-01-14 19:08:24 +01:00
|
|
|
use Psalm\Issue\MissingConstructor;
|
2019-09-22 04:52:30 +02:00
|
|
|
use Psalm\Issue\MissingImmutableAnnotation;
|
2018-01-14 19:08:24 +01:00
|
|
|
use Psalm\Issue\MissingPropertyType;
|
2019-01-24 23:55:03 +01:00
|
|
|
use Psalm\Issue\MissingTemplateParam;
|
2020-02-22 16:04:38 +01:00
|
|
|
use Psalm\Issue\MutableDependency;
|
2018-03-04 18:24:50 +01:00
|
|
|
use Psalm\Issue\OverriddenPropertyAccess;
|
2018-01-14 19:08:24 +01:00
|
|
|
use Psalm\Issue\PropertyNotSetInConstructor;
|
|
|
|
use Psalm\Issue\ReservedWord;
|
2019-01-25 21:31:17 +01:00
|
|
|
use Psalm\Issue\TooManyTemplateParams;
|
2019-03-07 17:16:40 +01:00
|
|
|
use Psalm\Issue\UndefinedClass;
|
|
|
|
use Psalm\Issue\UndefinedInterface;
|
2018-01-14 19:08:24 +01:00
|
|
|
use Psalm\Issue\UndefinedTrait;
|
|
|
|
use Psalm\Issue\UnimplementedAbstractMethod;
|
|
|
|
use Psalm\Issue\UnimplementedInterfaceMethod;
|
|
|
|
use Psalm\IssueBuffer;
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\StatementsSource;
|
2018-01-26 17:50:29 +01:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2018-05-12 00:35:02 +02:00
|
|
|
use Psalm\Storage\FunctionLikeParameter;
|
2018-01-14 19:08:24 +01:00
|
|
|
use Psalm\Type;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function preg_replace;
|
|
|
|
use function preg_match;
|
|
|
|
use function explode;
|
|
|
|
use function array_pop;
|
|
|
|
use function strtolower;
|
|
|
|
use function implode;
|
|
|
|
use function substr;
|
|
|
|
use function array_map;
|
|
|
|
use function array_shift;
|
|
|
|
use function str_replace;
|
|
|
|
use function count;
|
|
|
|
use function array_search;
|
|
|
|
use function array_keys;
|
2020-08-05 21:49:19 +02:00
|
|
|
use function array_merge;
|
2020-08-10 18:43:19 +02:00
|
|
|
use function array_filter;
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class ClassAnalyzer extends ClassLikeAnalyzer
|
2016-08-13 20:20:46 +02:00
|
|
|
{
|
2020-07-08 01:32:44 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, Type\Union>
|
|
|
|
*/
|
|
|
|
public $inferred_property_types = [];
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function __construct(PhpParser\Node\Stmt\Class_ $class, SourceAnalyzer $source, ?string $fq_class_name)
|
2016-08-13 20:20:46 +02:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
if (!$fq_class_name) {
|
|
|
|
$fq_class_name = self::getAnonymousClassName($class, $source->getFilePath());
|
2016-10-21 02:54:17 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
parent::__construct($class, $source, $fq_class_name);
|
2016-08-14 00:54:49 +02:00
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
if (!$this->class instanceof PhpParser\Node\Stmt\Class_) {
|
|
|
|
throw new \InvalidArgumentException('Bad');
|
|
|
|
}
|
|
|
|
|
2016-08-14 01:44:24 +02:00
|
|
|
if ($this->class->extends) {
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->parent_fq_class_name = self::getFQCLNFromNameObject(
|
2016-11-02 07:29:00 +01:00
|
|
|
$this->class->extends,
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->source->getAliases()
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-08-14 01:44:24 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2016-08-14 01:44:24 +02:00
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public static function getAnonymousClassName(PhpParser\Node\Stmt\Class_ $class, string $file_path): string
|
2017-07-25 22:11:02 +02:00
|
|
|
{
|
2018-05-25 10:21:54 +02:00
|
|
|
return preg_replace('/[^A-Za-z0-9]/', '_', $file_path)
|
2018-06-06 21:32:03 +02:00
|
|
|
. '_' . $class->getLine() . '_' . (int)$class->getAttribute('startFilePos');
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2018-01-14 19:08:24 +01:00
|
|
|
/**
|
|
|
|
* @return null|false
|
|
|
|
*/
|
|
|
|
public function analyze(
|
2020-09-07 01:36:47 +02:00
|
|
|
?Context $class_context = null,
|
|
|
|
?Context $global_context = null
|
2018-01-14 19:08:24 +01:00
|
|
|
) {
|
2018-04-13 17:28:15 +02:00
|
|
|
$class = $this->class;
|
|
|
|
|
|
|
|
if (!$class instanceof PhpParser\Node\Stmt\Class_) {
|
2018-01-14 19:08:24 +01:00
|
|
|
throw new \LogicException('Something went badly wrong');
|
|
|
|
}
|
|
|
|
|
|
|
|
$fq_class_name = $class_context && $class_context->self ? $class_context->self : $this->fq_class_name;
|
|
|
|
|
2018-02-14 17:21:43 +01:00
|
|
|
$storage = $this->storage;
|
|
|
|
|
2018-09-05 03:22:05 +02:00
|
|
|
if ($storage->has_visitor_issues) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-26 06:08:18 +02:00
|
|
|
if ($class->name
|
|
|
|
&& (preg_match(
|
2020-02-12 04:34:34 +01:00
|
|
|
'/(^|\\\)(int|float|bool|string|void|null|false|true|object|mixed)$/i',
|
2019-08-26 06:08:18 +02:00
|
|
|
$fq_class_name
|
|
|
|
) || strtolower($fq_class_name) === 'resource')
|
|
|
|
) {
|
2018-01-14 19:08:24 +01:00
|
|
|
$class_name_parts = explode('\\', $fq_class_name);
|
|
|
|
$class_name = array_pop($class_name_parts);
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ReservedWord(
|
|
|
|
$class_name . ' is a reserved word',
|
|
|
|
new CodeLocation(
|
|
|
|
$this,
|
2018-04-17 18:16:25 +02:00
|
|
|
$class->name,
|
2018-01-14 19:08:24 +01:00
|
|
|
null,
|
|
|
|
true
|
2018-03-21 15:17:57 +01:00
|
|
|
),
|
|
|
|
$class_name
|
2018-01-14 19:08:24 +01:00
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2018-01-14 19:08:24 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer = $this->file_analyzer->project_analyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase = $this->getCodebase();
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
if ($codebase->alter_code && $class->name && $codebase->classes_to_move) {
|
|
|
|
if (isset($codebase->classes_to_move[strtolower($this->fq_class_name)])) {
|
|
|
|
$destination_class = $codebase->classes_to_move[strtolower($this->fq_class_name)];
|
|
|
|
|
|
|
|
$source_class_parts = explode('\\', $this->fq_class_name);
|
|
|
|
$destination_class_parts = explode('\\', $destination_class);
|
|
|
|
|
|
|
|
array_pop($source_class_parts);
|
|
|
|
array_pop($destination_class_parts);
|
|
|
|
|
|
|
|
$source_ns = implode('\\', $source_class_parts);
|
|
|
|
$destination_ns = implode('\\', $destination_class_parts);
|
|
|
|
|
|
|
|
if (strtolower($source_ns) !== strtolower($destination_ns)) {
|
|
|
|
if ($storage->namespace_name_location) {
|
|
|
|
$bounds = $storage->namespace_name_location->getSelectionBounds();
|
|
|
|
|
|
|
|
$file_manipulations = [
|
|
|
|
new \Psalm\FileManipulation(
|
|
|
|
$bounds[0],
|
|
|
|
$bounds[1],
|
|
|
|
$destination_ns
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
|
|
|
\Psalm\Internal\FileManipulation\FileManipulationBuffer::add(
|
|
|
|
$this->getFilePath(),
|
|
|
|
$file_manipulations
|
|
|
|
);
|
|
|
|
} elseif (!$source_ns) {
|
2019-06-05 17:15:52 +02:00
|
|
|
$first_statement_pos = $this->getFileAnalyzer()->getFirstStatementOffset();
|
|
|
|
|
|
|
|
if ($first_statement_pos === -1) {
|
|
|
|
$first_statement_pos = (int) $class->getAttribute('startFilePos');
|
|
|
|
}
|
2019-06-04 22:36:32 +02:00
|
|
|
|
|
|
|
$file_manipulations = [
|
|
|
|
new \Psalm\FileManipulation(
|
2019-06-05 17:15:52 +02:00
|
|
|
$first_statement_pos,
|
|
|
|
$first_statement_pos,
|
2019-06-04 22:36:32 +02:00
|
|
|
'namespace ' . $destination_ns . ';' . "\n\n",
|
|
|
|
true
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
|
|
|
\Psalm\Internal\FileManipulation\FileManipulationBuffer::add(
|
|
|
|
$this->getFilePath(),
|
|
|
|
$file_manipulations
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$codebase->classlikes->handleClassLikeReferenceInMigration(
|
|
|
|
$codebase,
|
|
|
|
$this,
|
|
|
|
$class->name,
|
|
|
|
$this->fq_class_name,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-29 00:54:55 +01:00
|
|
|
foreach ($storage->docblock_issues as $docblock_issue) {
|
|
|
|
IssueBuffer::add($docblock_issue);
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:19:53 +01:00
|
|
|
$classlike_storage_provider = $codebase->classlike_storage_provider;
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-04-13 17:28:15 +02:00
|
|
|
$parent_fq_class_name = $this->parent_fq_class_name;
|
|
|
|
|
|
|
|
if ($class->extends) {
|
|
|
|
if (!$parent_fq_class_name) {
|
2018-01-14 19:08:24 +01:00
|
|
|
throw new \UnexpectedValueException('Parent class should be filled in for ' . $fq_class_name);
|
|
|
|
}
|
|
|
|
|
2018-04-13 17:28:15 +02:00
|
|
|
$parent_reference_location = new CodeLocation($this, $class->extends);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
|
|
|
if (self::checkFullyQualifiedClassLikeName(
|
2018-11-06 03:57:36 +01:00
|
|
|
$this->getSource(),
|
2018-04-13 17:28:15 +02:00
|
|
|
$parent_fq_class_name,
|
2018-01-14 19:08:24 +01:00
|
|
|
$parent_reference_location,
|
2020-02-11 22:39:33 +01:00
|
|
|
null,
|
2020-03-26 17:35:27 +01:00
|
|
|
null,
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues(),
|
2018-01-14 19:08:24 +01:00
|
|
|
false
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-23 15:09:43 +01:00
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
if ($codebase->alter_code && $codebase->classes_to_move) {
|
|
|
|
$codebase->classlikes->handleClassLikeReferenceInMigration(
|
|
|
|
$codebase,
|
|
|
|
$this,
|
|
|
|
$class->extends,
|
|
|
|
$parent_fq_class_name,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-23 15:09:43 +01:00
|
|
|
try {
|
2018-04-13 17:28:15 +02:00
|
|
|
$parent_class_storage = $classlike_storage_provider->get($parent_fq_class_name);
|
2018-01-23 15:09:43 +01:00
|
|
|
|
2019-03-07 17:16:40 +01:00
|
|
|
$code_location = new CodeLocation(
|
|
|
|
$this,
|
|
|
|
$class->extends,
|
|
|
|
$class_context ? $class_context->include_location : null,
|
|
|
|
true
|
|
|
|
);
|
2018-01-23 15:09:43 +01:00
|
|
|
|
2019-03-07 17:16:40 +01:00
|
|
|
if ($parent_class_storage->is_trait || $parent_class_storage->is_interface) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedClass(
|
|
|
|
$parent_fq_class_name . ' is not a class',
|
|
|
|
$code_location,
|
|
|
|
$parent_fq_class_name . ' as class'
|
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2019-03-07 17:16:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 06:29:59 +02:00
|
|
|
if ($parent_class_storage->final) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidExtendClass(
|
|
|
|
'Class ' . $fq_class_name . ' may not inherit from final class ' . $parent_fq_class_name,
|
|
|
|
$code_location,
|
|
|
|
$fq_class_name
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:16:40 +01:00
|
|
|
if ($parent_class_storage->deprecated) {
|
2018-01-23 15:09:43 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedClass(
|
2018-04-13 17:28:15 +02:00
|
|
|
$parent_fq_class_name . ' is marked deprecated',
|
2019-06-06 20:48:33 +02:00
|
|
|
$code_location,
|
|
|
|
$parent_fq_class_name
|
2018-01-23 15:09:43 +01:00
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2018-01-23 15:09:43 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
2018-12-02 00:37:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:32:54 +02:00
|
|
|
if (! NamespaceAnalyzer::isWithin($fq_class_name, $parent_class_storage->internal)) {
|
2019-05-10 00:33:27 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InternalClass(
|
2020-07-24 21:38:56 +02:00
|
|
|
$parent_fq_class_name . ' is internal to ' . $parent_class_storage->internal
|
|
|
|
. ' but called from ' . $fq_class_name,
|
2019-06-06 20:48:33 +02:00
|
|
|
$code_location,
|
|
|
|
$parent_fq_class_name
|
2019-05-10 00:33:27 +02:00
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2019-05-10 00:33:27 +02:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 04:52:30 +02:00
|
|
|
if ($parent_class_storage->external_mutation_free
|
|
|
|
&& !$storage->external_mutation_free
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingImmutableAnnotation(
|
|
|
|
$parent_fq_class_name . ' is marked immutable, but '
|
|
|
|
. $fq_class_name . ' is not marked immutable',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 16:04:38 +01:00
|
|
|
if ($storage->mutation_free
|
|
|
|
&& !$parent_class_storage->mutation_free
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MutableDependency(
|
|
|
|
$fq_class_name . ' is marked immutable but ' . $parent_fq_class_name . ' is not',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 21:51:43 +01:00
|
|
|
if ($codebase->store_node_types) {
|
2018-11-02 04:40:36 +01:00
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$this->getFilePath(),
|
|
|
|
$class->extends,
|
2019-06-21 23:10:35 +02:00
|
|
|
$codebase->classlikes->classExists($parent_fq_class_name)
|
|
|
|
? $parent_fq_class_name
|
|
|
|
: '*' . implode('\\', $class->extends->parts)
|
2018-11-02 04:40:36 +01:00
|
|
|
);
|
|
|
|
}
|
2019-01-24 23:55:03 +01:00
|
|
|
|
2019-01-25 21:31:17 +01:00
|
|
|
$code_location = new CodeLocation(
|
|
|
|
$this,
|
|
|
|
$class->name ?: $class,
|
|
|
|
$class_context ? $class_context->include_location : null,
|
|
|
|
true
|
|
|
|
);
|
2019-01-24 23:55:03 +01:00
|
|
|
|
2019-01-28 05:12:40 +01:00
|
|
|
if ($storage->template_type_extends_count !== null) {
|
|
|
|
$this->checkTemplateParams(
|
|
|
|
$codebase,
|
|
|
|
$storage,
|
|
|
|
$parent_class_storage,
|
|
|
|
$code_location,
|
|
|
|
$storage->template_type_extends_count
|
|
|
|
);
|
|
|
|
}
|
2018-01-23 15:09:43 +01:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2018-04-13 17:28:15 +02:00
|
|
|
foreach ($class->implements as $interface_name) {
|
2018-01-14 19:08:24 +01:00
|
|
|
$fq_interface_name = self::getFQCLNFromNameObject(
|
|
|
|
$interface_name,
|
|
|
|
$this->source->getAliases()
|
|
|
|
);
|
|
|
|
|
2019-06-21 23:10:35 +02:00
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$this->getFilePath(),
|
|
|
|
$interface_name,
|
|
|
|
$codebase->classlikes->interfaceExists($fq_interface_name)
|
|
|
|
? $fq_interface_name
|
|
|
|
: '*' . implode('\\', $interface_name->parts)
|
|
|
|
);
|
|
|
|
|
2018-01-14 19:08:24 +01:00
|
|
|
$interface_location = new CodeLocation($this, $interface_name);
|
|
|
|
|
|
|
|
if (self::checkFullyQualifiedClassLikeName(
|
|
|
|
$this,
|
|
|
|
$fq_interface_name,
|
|
|
|
$interface_location,
|
2020-02-11 22:39:33 +01:00
|
|
|
null,
|
2020-03-26 17:35:27 +01:00
|
|
|
null,
|
2018-01-14 19:08:24 +01:00
|
|
|
$this->getSuppressedIssues(),
|
|
|
|
false
|
|
|
|
) === false) {
|
2019-06-21 23:10:35 +02:00
|
|
|
continue;
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
2018-11-02 04:40:36 +01:00
|
|
|
|
2019-02-24 07:33:25 +01:00
|
|
|
if ($codebase->store_node_types && $fq_class_name) {
|
2018-11-02 04:40:36 +01:00
|
|
|
$bounds = $interface_location->getSelectionBounds();
|
|
|
|
|
|
|
|
$codebase->analyzer->addOffsetReference(
|
|
|
|
$this->getFilePath(),
|
|
|
|
$bounds[0],
|
|
|
|
$bounds[1],
|
|
|
|
$fq_interface_name
|
|
|
|
);
|
|
|
|
}
|
2019-01-25 16:10:22 +01:00
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
$codebase->classlikes->handleClassLikeReferenceInMigration(
|
|
|
|
$codebase,
|
|
|
|
$this,
|
|
|
|
$interface_name,
|
|
|
|
$fq_interface_name,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_interface_name_lc = strtolower($fq_interface_name);
|
|
|
|
|
2019-01-25 16:10:22 +01:00
|
|
|
try {
|
2020-02-15 02:54:26 +01:00
|
|
|
$interface_storage = $classlike_storage_provider->get($fq_interface_name_lc);
|
2019-01-25 16:10:22 +01:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-25 21:31:17 +01:00
|
|
|
$code_location = new CodeLocation(
|
|
|
|
$this,
|
|
|
|
$interface_name,
|
|
|
|
$class_context ? $class_context->include_location : null,
|
|
|
|
true
|
|
|
|
);
|
2019-01-25 16:10:22 +01:00
|
|
|
|
2019-03-07 17:16:40 +01:00
|
|
|
if (!$interface_storage->is_interface) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedInterface(
|
2019-03-07 17:30:05 +01:00
|
|
|
$fq_interface_name . ' is not an interface',
|
2019-06-06 20:48:33 +02:00
|
|
|
$code_location,
|
|
|
|
$fq_interface_name
|
2019-03-07 17:16:40 +01:00
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2019-03-07 17:16:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if (isset($storage->template_type_implements_count[$fq_interface_name_lc])) {
|
|
|
|
$expected_param_count = $storage->template_type_implements_count[$fq_interface_name_lc];
|
2019-01-28 05:12:40 +01:00
|
|
|
|
|
|
|
$this->checkTemplateParams(
|
|
|
|
$codebase,
|
|
|
|
$storage,
|
|
|
|
$interface_storage,
|
|
|
|
$code_location,
|
|
|
|
$expected_param_count
|
|
|
|
);
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 03:54:00 +01:00
|
|
|
if ($storage->template_types) {
|
|
|
|
foreach ($storage->template_types as $param_name => $_) {
|
|
|
|
$fq_classlike_name = Type::getFQCLNFromString(
|
|
|
|
$param_name,
|
|
|
|
$this->getAliases()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($codebase->classOrInterfaceExists($fq_classlike_name)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ReservedWord(
|
|
|
|
'Cannot use ' . $param_name . ' as template name since the class already exists',
|
|
|
|
new CodeLocation($this, $this->class),
|
|
|
|
'resource'
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 21:49:19 +02:00
|
|
|
if (($storage->templatedMixins || $storage->namedMixins)
|
|
|
|
&& $storage->mixin_declaring_fqcln === $storage->name) {
|
|
|
|
/** @var non-empty-array<int, Type\Atomic\TTemplateParam|Type\Atomic\TNamedObject> $mixins */
|
|
|
|
$mixins = array_merge($storage->templatedMixins, $storage->namedMixins);
|
|
|
|
$union = new Type\Union($mixins);
|
2020-05-25 06:19:46 +02:00
|
|
|
$union->check(
|
|
|
|
$this,
|
|
|
|
new CodeLocation(
|
|
|
|
$this,
|
|
|
|
$class->name ?: $class,
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:43:22 +01:00
|
|
|
if ($storage->template_type_extends) {
|
|
|
|
foreach ($storage->template_type_extends as $type_map) {
|
|
|
|
foreach ($type_map as $atomic_type) {
|
|
|
|
$atomic_type->check(
|
|
|
|
$this,
|
|
|
|
new CodeLocation(
|
|
|
|
$this,
|
|
|
|
$class->name ?: $class,
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:28:15 +01:00
|
|
|
if ($storage->invalid_dependencies) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:08:24 +01:00
|
|
|
$class_interfaces = $storage->class_implements;
|
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
foreach ($class_interfaces as $interface_name) {
|
|
|
|
try {
|
|
|
|
$interface_storage = $classlike_storage_provider->get($interface_name);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
$code_location = new CodeLocation(
|
|
|
|
$this,
|
|
|
|
$class->name ? $class->name : $class,
|
|
|
|
$class_context ? $class_context->include_location : null,
|
|
|
|
true
|
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
if ($interface_storage->deprecated) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedInterface(
|
|
|
|
$interface_name . ' is marked deprecated',
|
|
|
|
$code_location,
|
|
|
|
$interface_name
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
2018-01-23 15:09:43 +01:00
|
|
|
}
|
2020-01-21 17:46:51 +01:00
|
|
|
}
|
2018-01-23 15:09:43 +01:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
if ($interface_storage->external_mutation_free
|
|
|
|
&& !$storage->external_mutation_free
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingImmutableAnnotation(
|
|
|
|
$interface_name . ' is marked immutable, but '
|
|
|
|
. $fq_class_name . ' is not marked immutable',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
2019-09-23 02:42:31 +02:00
|
|
|
}
|
2020-01-21 17:46:51 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
foreach ($interface_storage->methods as $interface_method_name_lc => $interface_method_storage) {
|
2020-01-21 17:46:51 +01:00
|
|
|
if ($interface_method_storage->visibility === self::VISIBILITY_PUBLIC) {
|
|
|
|
$implementer_declaring_method_id = $codebase->methods->getDeclaringMethodId(
|
2020-02-15 02:54:26 +01:00
|
|
|
new \Psalm\Internal\MethodIdentifier(
|
|
|
|
$this->fq_class_name,
|
|
|
|
$interface_method_name_lc
|
|
|
|
)
|
2020-01-21 17:46:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$implementer_method_storage = null;
|
|
|
|
$implementer_classlike_storage = null;
|
2019-09-23 02:42:31 +02:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
if ($implementer_declaring_method_id) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$implementer_fq_class_name = $implementer_declaring_method_id->fq_class_name;
|
2020-01-21 17:46:51 +01:00
|
|
|
$implementer_method_storage = $codebase->methods->getStorage(
|
|
|
|
$implementer_declaring_method_id
|
2018-01-14 19:08:24 +01:00
|
|
|
);
|
2020-01-21 17:46:51 +01:00
|
|
|
$implementer_classlike_storage = $classlike_storage_provider->get(
|
|
|
|
$implementer_fq_class_name
|
|
|
|
);
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
if (!$implementer_method_storage) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnimplementedInterfaceMethod(
|
2020-02-15 02:54:26 +01:00
|
|
|
'Method ' . $interface_method_name_lc . ' is not defined on class ' .
|
2020-01-21 17:46:51 +01:00
|
|
|
$storage->name,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
return null;
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
$implementer_appearing_method_id = $codebase->methods->getAppearingMethodId(
|
2020-02-15 02:54:26 +01:00
|
|
|
new \Psalm\Internal\MethodIdentifier(
|
|
|
|
$this->fq_class_name,
|
|
|
|
$interface_method_name_lc
|
|
|
|
)
|
2020-01-21 17:46:51 +01:00
|
|
|
);
|
2019-01-16 16:59:06 +01:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
$implementer_visibility = $implementer_method_storage->visibility;
|
2019-01-16 16:59:06 +01:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
if ($implementer_appearing_method_id
|
|
|
|
&& $implementer_appearing_method_id !== $implementer_declaring_method_id
|
|
|
|
) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$appearing_fq_class_name = $implementer_appearing_method_id->fq_class_name;
|
|
|
|
$appearing_method_name = $implementer_appearing_method_id->method_name;
|
2019-01-16 16:59:06 +01:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
$appearing_class_storage = $classlike_storage_provider->get(
|
|
|
|
$appearing_fq_class_name
|
|
|
|
);
|
2019-01-16 16:59:06 +01:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
if (isset($appearing_class_storage->trait_visibility_map[$appearing_method_name])) {
|
|
|
|
$implementer_visibility
|
|
|
|
= $appearing_class_storage->trait_visibility_map[$appearing_method_name];
|
2019-01-16 16:59:06 +01:00
|
|
|
}
|
2020-01-21 17:46:51 +01:00
|
|
|
}
|
2019-01-16 16:59:06 +01:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
if ($implementer_visibility !== self::VISIBILITY_PUBLIC) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InaccessibleMethod(
|
|
|
|
'Interface-defined method ' . $implementer_method_storage->cased_name
|
|
|
|
. ' must be public in ' . $storage->name,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
return null;
|
|
|
|
}
|
2019-03-04 00:13:15 +01:00
|
|
|
|
2020-01-21 17:46:51 +01:00
|
|
|
if ($interface_method_storage->is_static && !$implementer_method_storage->is_static) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Method ' . $implementer_method_storage->cased_name
|
|
|
|
. ' should be static like '
|
|
|
|
. $storage->name . '::' . $interface_method_storage->cased_name,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$implementer_method_storage->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
2020-01-21 17:46:51 +01:00
|
|
|
|
2020-02-06 21:07:57 +01:00
|
|
|
if ($storage->abstract && $implementer_method_storage === $interface_method_storage) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-12 04:38:49 +01:00
|
|
|
MethodComparator::compare(
|
2020-01-21 17:46:51 +01:00
|
|
|
$codebase,
|
2020-08-14 21:25:21 +02:00
|
|
|
null,
|
2020-01-21 17:46:51 +01:00
|
|
|
$implementer_classlike_storage ?: $storage,
|
|
|
|
$interface_storage,
|
|
|
|
$implementer_method_storage,
|
|
|
|
$interface_method_storage,
|
|
|
|
$this->fq_class_name,
|
|
|
|
$implementer_visibility,
|
|
|
|
$code_location,
|
|
|
|
$implementer_method_storage->suppressed_issues,
|
|
|
|
false
|
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$class_context) {
|
|
|
|
$class_context = new Context($this->fq_class_name);
|
2018-04-13 17:28:15 +02:00
|
|
|
$class_context->parent = $parent_fq_class_name;
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2018-08-28 23:42:39 +02:00
|
|
|
if ($global_context) {
|
|
|
|
$class_context->strict_types = $global_context->strict_types;
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:08:24 +01:00
|
|
|
if ($this->leftover_stmts) {
|
2019-11-25 17:44:54 +01:00
|
|
|
(new StatementsAnalyzer(
|
|
|
|
$this,
|
|
|
|
new \Psalm\Internal\Provider\NodeDataProvider()
|
|
|
|
))->analyze(
|
|
|
|
$this->leftover_stmts,
|
|
|
|
$class_context
|
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$storage->abstract) {
|
2018-05-29 11:08:56 +02:00
|
|
|
foreach ($storage->declaring_method_ids as $declaring_method_id) {
|
2018-02-04 00:52:35 +01:00
|
|
|
$method_storage = $codebase->methods->getStorage($declaring_method_id);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$declaring_class_name = $declaring_method_id->fq_class_name;
|
|
|
|
$method_name_lc = $declaring_method_id->method_name;
|
2018-01-14 19:08:24 +01:00
|
|
|
|
|
|
|
if ($method_storage->abstract) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnimplementedAbstractMethod(
|
2020-02-15 02:54:26 +01:00
|
|
|
'Method ' . $method_name_lc . ' is not defined on class ' .
|
2018-01-14 19:08:24 +01:00
|
|
|
$this->fq_class_name . ', defined abstract in ' . $declaring_class_name,
|
|
|
|
new CodeLocation(
|
|
|
|
$this,
|
2018-04-17 18:16:25 +02:00
|
|
|
$class->name ? $class->name : $class,
|
2018-01-14 19:08:24 +01:00
|
|
|
$class_context->include_location,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2018-01-14 19:08:24 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 01:46:38 +01:00
|
|
|
self::addContextProperties(
|
|
|
|
$this,
|
|
|
|
$storage,
|
|
|
|
$class_context,
|
|
|
|
$this->fq_class_name,
|
2020-08-10 16:50:37 +02:00
|
|
|
$this->parent_fq_class_name,
|
|
|
|
$class->stmts
|
2020-03-22 01:46:38 +01:00
|
|
|
);
|
2019-01-19 20:26:27 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$constructor_analyzer = null;
|
2018-06-25 21:02:46 +02:00
|
|
|
$member_stmts = [];
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-04-13 17:28:15 +02:00
|
|
|
foreach ($class->stmts as $stmt) {
|
2018-01-14 19:08:24 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$method_analyzer = $this->analyzeClassMethod(
|
2018-01-14 19:08:24 +01:00
|
|
|
$stmt,
|
2018-01-26 17:50:29 +01:00
|
|
|
$storage,
|
2018-01-14 19:08:24 +01:00
|
|
|
$this,
|
|
|
|
$class_context,
|
|
|
|
$global_context
|
|
|
|
);
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($stmt->name->name === '__construct') {
|
2018-11-11 18:01:14 +01:00
|
|
|
$constructor_analyzer = $method_analyzer;
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
|
2018-05-30 18:23:53 +02:00
|
|
|
if ($this->analyzeTraitUse(
|
2018-05-30 19:08:56 +02:00
|
|
|
$this->source->getAliases(),
|
2018-05-30 18:23:53 +02:00
|
|
|
$stmt,
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer,
|
2018-05-30 18:23:53 +02:00
|
|
|
$storage,
|
|
|
|
$class_context,
|
|
|
|
$global_context,
|
2018-11-11 18:01:14 +01:00
|
|
|
$constructor_analyzer
|
2018-05-30 18:23:53 +02:00
|
|
|
) === false) {
|
|
|
|
return false;
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
2018-01-29 03:43:26 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
|
|
|
|
foreach ($stmt->props as $prop) {
|
|
|
|
if ($prop->default) {
|
2018-06-25 21:02:46 +02:00
|
|
|
$member_stmts[] = $stmt;
|
2018-01-29 03:43:26 +01:00
|
|
|
}
|
2019-06-04 06:32:19 +02:00
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
if ($codebase->alter_code) {
|
|
|
|
$property_id = strtolower($this->fq_class_name) . '::$' . $prop->name;
|
|
|
|
|
|
|
|
$property_storage = $codebase->properties->getStorage($property_id);
|
|
|
|
|
|
|
|
if ($property_storage->type
|
|
|
|
&& $property_storage->type_location
|
|
|
|
&& $property_storage->type_location !== $property_storage->signature_type_location
|
|
|
|
) {
|
2020-05-11 04:45:01 +02:00
|
|
|
$replace_type = \Psalm\Internal\Type\TypeExpander::expandUnion(
|
2019-06-04 22:36:32 +02:00
|
|
|
$codebase,
|
|
|
|
$property_storage->type,
|
|
|
|
$this->getFQCLN(),
|
|
|
|
$this->getFQCLN(),
|
|
|
|
$this->getParentFQCLN()
|
|
|
|
);
|
|
|
|
|
|
|
|
$codebase->classlikes->handleDocblockTypeInMigration(
|
|
|
|
$codebase,
|
|
|
|
$this,
|
|
|
|
$replace_type,
|
|
|
|
$property_storage->type_location,
|
|
|
|
null
|
2019-06-04 06:32:19 +02:00
|
|
|
);
|
|
|
|
}
|
2019-06-04 22:36:32 +02:00
|
|
|
|
|
|
|
foreach ($codebase->properties_to_rename as $original_property_id => $new_property_name) {
|
|
|
|
if ($property_id === $original_property_id) {
|
|
|
|
$file_manipulations = [
|
|
|
|
new \Psalm\FileManipulation(
|
|
|
|
(int) $prop->name->getAttribute('startFilePos'),
|
|
|
|
(int) $prop->name->getAttribute('endFilePos') + 1,
|
|
|
|
'$' . $new_property_name
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
|
|
|
\Psalm\Internal\FileManipulation\FileManipulationBuffer::add(
|
|
|
|
$this->getFilePath(),
|
|
|
|
$file_manipulations
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-06-04 06:32:19 +02:00
|
|
|
}
|
2018-01-29 03:43:26 +01:00
|
|
|
}
|
2018-06-25 21:02:46 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
|
2018-06-26 00:02:05 +02:00
|
|
|
$member_stmts[] = $stmt;
|
2019-06-04 17:14:49 +02:00
|
|
|
|
|
|
|
foreach ($stmt->consts as $const) {
|
|
|
|
$const_id = strtolower($this->fq_class_name) . '::' . $const->name;
|
|
|
|
|
|
|
|
foreach ($codebase->class_constants_to_rename as $original_const_id => $new_const_name) {
|
|
|
|
if ($const_id === $original_const_id) {
|
|
|
|
$file_manipulations = [
|
|
|
|
new \Psalm\FileManipulation(
|
|
|
|
(int) $const->name->getAttribute('startFilePos'),
|
|
|
|
(int) $const->name->getAttribute('endFilePos') + 1,
|
|
|
|
$new_const_name
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
|
|
|
\Psalm\Internal\FileManipulation\FileManipulationBuffer::add(
|
|
|
|
$this->getFilePath(),
|
|
|
|
$file_manipulations
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer = new StatementsAnalyzer($this, new \Psalm\Internal\Provider\NodeDataProvider());
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->analyze($member_stmts, $class_context, $global_context, true);
|
2018-01-29 03:43:26 +01:00
|
|
|
|
2018-01-14 19:08:24 +01:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
$this->checkPropertyInitialization(
|
|
|
|
$codebase,
|
|
|
|
$config,
|
|
|
|
$storage,
|
|
|
|
$class_context,
|
|
|
|
$global_context,
|
2018-11-11 18:01:14 +01:00
|
|
|
$constructor_analyzer
|
2018-10-07 04:58:21 +02:00
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
foreach ($class->stmts as $stmt) {
|
2019-01-11 23:28:57 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Property && !isset($stmt->type)) {
|
2019-03-01 14:57:10 +01:00
|
|
|
$this->checkForMissingPropertyType($this, $stmt, $class_context);
|
2018-10-07 04:58:21 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
|
|
|
|
foreach ($stmt->traits as $trait) {
|
|
|
|
$fq_trait_name = self::getFQCLNFromNameObject(
|
|
|
|
$trait,
|
|
|
|
$this->source->getAliases()
|
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2019-02-10 22:32:30 +01:00
|
|
|
try {
|
|
|
|
$trait_file_analyzer = $project_analyzer->getFileAnalyzerForClassLike($fq_trait_name);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-03 04:27:54 +01:00
|
|
|
$trait_storage = $codebase->classlike_storage_provider->get($fq_trait_name);
|
2018-10-07 04:58:21 +02:00
|
|
|
$trait_node = $codebase->classlikes->getTraitNode($fq_trait_name);
|
2020-03-03 04:27:54 +01:00
|
|
|
$trait_aliases = $trait_storage->aliases;
|
|
|
|
|
|
|
|
if ($trait_aliases === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$trait_analyzer = new TraitAnalyzer(
|
2018-10-07 04:58:21 +02:00
|
|
|
$trait_node,
|
2018-11-11 18:01:14 +01:00
|
|
|
$trait_file_analyzer,
|
2018-10-07 04:58:21 +02:00
|
|
|
$fq_trait_name,
|
|
|
|
$trait_aliases
|
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_trait_name_lc = strtolower($fq_trait_name);
|
|
|
|
|
|
|
|
if (isset($storage->template_type_uses_count[$fq_trait_name_lc])) {
|
|
|
|
$expected_param_count = $storage->template_type_uses_count[$fq_trait_name_lc];
|
2019-01-28 05:12:40 +01:00
|
|
|
|
|
|
|
$this->checkTemplateParams(
|
|
|
|
$codebase,
|
|
|
|
$storage,
|
|
|
|
$trait_storage,
|
|
|
|
new CodeLocation(
|
|
|
|
$this,
|
|
|
|
$trait
|
|
|
|
),
|
|
|
|
$expected_param_count
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
foreach ($trait_node->stmts as $trait_stmt) {
|
|
|
|
if ($trait_stmt instanceof PhpParser\Node\Stmt\Property) {
|
2019-03-01 14:57:10 +01:00
|
|
|
$this->checkForMissingPropertyType($trait_analyzer, $trait_stmt, $class_context);
|
2018-10-07 04:58:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-02 07:55:24 +02:00
|
|
|
|
|
|
|
$trait_file_analyzer->clearSourceBeforeDestruction();
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
2018-10-07 04:58:21 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-30 21:13:25 +01:00
|
|
|
|
2020-02-02 00:21:32 +01:00
|
|
|
$pseudo_methods = $storage->pseudo_methods + $storage->pseudo_static_methods;
|
|
|
|
|
|
|
|
foreach ($pseudo_methods as $pseudo_method_name => $pseudo_method_storage) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$pseudo_method_id = new \Psalm\Internal\MethodIdentifier(
|
|
|
|
$this->fq_class_name,
|
|
|
|
$pseudo_method_name
|
|
|
|
);
|
2018-11-30 21:13:25 +01:00
|
|
|
|
|
|
|
$overridden_method_ids = $codebase->methods->getOverriddenMethodIds($pseudo_method_id);
|
|
|
|
|
|
|
|
if ($overridden_method_ids
|
|
|
|
&& $pseudo_method_name !== '__construct'
|
|
|
|
&& $pseudo_method_storage->location
|
|
|
|
) {
|
|
|
|
foreach ($overridden_method_ids as $overridden_method_id) {
|
|
|
|
$parent_method_storage = $codebase->methods->getStorage($overridden_method_id);
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$overridden_fq_class_name = $overridden_method_id->fq_class_name;
|
2018-11-30 21:13:25 +01:00
|
|
|
|
|
|
|
$parent_storage = $classlike_storage_provider->get($overridden_fq_class_name);
|
|
|
|
|
2020-03-12 04:38:49 +01:00
|
|
|
MethodComparator::compare(
|
2018-11-30 21:13:25 +01:00
|
|
|
$codebase,
|
2020-08-14 21:25:21 +02:00
|
|
|
null,
|
2018-11-30 21:13:25 +01:00
|
|
|
$storage,
|
|
|
|
$parent_storage,
|
|
|
|
$pseudo_method_storage,
|
|
|
|
$parent_method_storage,
|
2019-01-16 21:08:11 +01:00
|
|
|
$this->fq_class_name,
|
2019-01-16 16:59:06 +01:00
|
|
|
$pseudo_method_storage->visibility ?: 0,
|
2019-12-20 19:18:26 +01:00
|
|
|
$storage->location ?: $pseudo_method_storage->location,
|
2018-11-30 21:13:25 +01:00
|
|
|
$storage->suppressed_issues,
|
|
|
|
true,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-09 14:28:12 +01:00
|
|
|
|
|
|
|
$plugin_classes = $codebase->config->after_classlike_checks;
|
|
|
|
|
|
|
|
if ($plugin_classes) {
|
|
|
|
$file_manipulations = [];
|
|
|
|
|
|
|
|
foreach ($plugin_classes as $plugin_fq_class_name) {
|
|
|
|
if ($plugin_fq_class_name::afterStatementAnalysis(
|
|
|
|
$class,
|
|
|
|
$storage,
|
2019-11-25 17:44:54 +01:00
|
|
|
$this,
|
2019-01-09 14:28:12 +01:00
|
|
|
$codebase,
|
|
|
|
$file_manipulations
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file_manipulations) {
|
|
|
|
\Psalm\Internal\FileManipulation\FileManipulationBuffer::add(
|
|
|
|
$this->getFilePath(),
|
|
|
|
$file_manipulations
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-10-07 04:58:21 +02:00
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2020-03-22 01:46:38 +01:00
|
|
|
public static function addContextProperties(
|
|
|
|
StatementsSource $statements_source,
|
|
|
|
ClassLikeStorage $storage,
|
|
|
|
Context $class_context,
|
|
|
|
string $fq_class_name,
|
2020-08-10 16:50:37 +02:00
|
|
|
?string $parent_fq_class_name,
|
|
|
|
array $stmts = []
|
2020-03-22 01:46:38 +01:00
|
|
|
) : void {
|
|
|
|
$codebase = $statements_source->getCodebase();
|
|
|
|
|
|
|
|
foreach ($storage->appearing_property_ids as $property_name => $appearing_property_id) {
|
|
|
|
$property_class_name = $codebase->properties->getDeclaringClassForProperty(
|
|
|
|
$appearing_property_id,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($property_class_name === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$property_class_storage = $codebase->classlike_storage_provider->get($property_class_name);
|
|
|
|
|
|
|
|
$property_storage = $property_class_storage->properties[$property_name];
|
|
|
|
|
|
|
|
if (isset($storage->overridden_property_ids[$property_name])) {
|
|
|
|
foreach ($storage->overridden_property_ids[$property_name] as $overridden_property_id) {
|
2020-09-02 06:17:41 +02:00
|
|
|
[$guide_class_name] = explode('::$', $overridden_property_id);
|
2020-03-22 01:53:46 +01:00
|
|
|
$guide_class_storage = $codebase->classlike_storage_provider->get($guide_class_name);
|
2020-03-22 01:46:38 +01:00
|
|
|
$guide_property_storage = $guide_class_storage->properties[$property_name];
|
|
|
|
|
|
|
|
if ($property_storage->visibility > $guide_property_storage->visibility
|
|
|
|
&& $property_storage->location
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new OverriddenPropertyAccess(
|
|
|
|
'Property ' . $guide_class_storage->name . '::$' . $property_name
|
|
|
|
. ' has different access level than '
|
|
|
|
. $storage->name . '::$' . $property_name,
|
|
|
|
$property_storage->location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($property_storage->type) {
|
|
|
|
$property_type = clone $property_storage->type;
|
|
|
|
|
|
|
|
if (!$property_type->isMixed()
|
|
|
|
&& !$property_storage->has_default
|
|
|
|
&& !($property_type->isNullable() && $property_type->from_docblock)
|
|
|
|
) {
|
|
|
|
$property_type->initialized = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$property_type = Type::getMixed();
|
|
|
|
|
|
|
|
if (!$property_storage->has_default) {
|
|
|
|
$property_type->initialized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$property_type_location = $property_storage->type_location;
|
|
|
|
|
|
|
|
$fleshed_out_type = !$property_type->isMixed()
|
2020-05-11 04:45:01 +02:00
|
|
|
? \Psalm\Internal\Type\TypeExpander::expandUnion(
|
2020-03-22 01:46:38 +01:00
|
|
|
$codebase,
|
|
|
|
$property_type,
|
|
|
|
$fq_class_name,
|
|
|
|
$fq_class_name,
|
2020-07-25 23:26:07 +02:00
|
|
|
$parent_fq_class_name,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
$storage->final
|
2020-03-22 01:46:38 +01:00
|
|
|
)
|
|
|
|
: $property_type;
|
|
|
|
|
|
|
|
$class_template_params = ClassTemplateParamCollector::collect(
|
|
|
|
$codebase,
|
|
|
|
$property_class_storage,
|
2020-07-25 23:26:07 +02:00
|
|
|
$storage,
|
2020-03-22 01:46:38 +01:00
|
|
|
null,
|
|
|
|
new Type\Atomic\TNamedObject($fq_class_name),
|
|
|
|
'$this'
|
|
|
|
);
|
|
|
|
|
|
|
|
$template_result = new \Psalm\Internal\Type\TemplateResult(
|
|
|
|
$class_template_params ?: [],
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($class_template_params) {
|
2020-04-17 07:16:15 +02:00
|
|
|
$fleshed_out_type = UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
2020-03-22 01:46:38 +01:00
|
|
|
$fleshed_out_type,
|
|
|
|
$template_result,
|
|
|
|
$codebase,
|
|
|
|
null,
|
|
|
|
null,
|
2020-03-27 23:31:48 +01:00
|
|
|
null,
|
|
|
|
$class_context->self
|
2020-03-22 01:46:38 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($property_type_location && !$fleshed_out_type->isMixed()) {
|
2020-08-10 18:43:19 +02:00
|
|
|
$stmt = array_filter($stmts, function ($stmt) use ($property_name) {
|
2020-08-10 16:50:37 +02:00
|
|
|
return $stmt instanceof PhpParser\Node\Stmt\Property
|
|
|
|
&& isset($stmt->props[0]->name->name)
|
|
|
|
&& $stmt->props[0]->name->name === $property_name;
|
|
|
|
});
|
|
|
|
|
|
|
|
$suppressed = [];
|
2020-08-10 18:43:19 +02:00
|
|
|
if (count($stmt) > 0) {
|
2020-08-10 16:50:37 +02:00
|
|
|
/** @var PhpParser\Node\Stmt\Property $stmt */
|
|
|
|
$stmt = array_pop($stmt);
|
|
|
|
|
|
|
|
$docComment = $stmt->getDocComment();
|
2020-08-10 18:43:19 +02:00
|
|
|
if ($docComment) {
|
2020-08-10 16:50:37 +02:00
|
|
|
try {
|
|
|
|
$docBlock = DocComment::parsePreservingLength($docComment);
|
|
|
|
$suppressed = $docBlock->tags['psalm-suppress'] ?? [];
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
// do nothing to keep original behavior
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 01:46:38 +01:00
|
|
|
$fleshed_out_type->check(
|
|
|
|
$statements_source,
|
|
|
|
$property_type_location,
|
2020-08-10 16:50:37 +02:00
|
|
|
$storage->suppressed_issues + $statements_source->getSuppressedIssues() + $suppressed,
|
2020-03-22 01:46:38 +01:00
|
|
|
[],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($property_storage->is_static) {
|
|
|
|
$property_id = $fq_class_name . '::$' . $property_name;
|
|
|
|
|
|
|
|
$class_context->vars_in_scope[$property_id] = $fleshed_out_type;
|
|
|
|
} else {
|
|
|
|
$class_context->vars_in_scope['$this->' . $property_name] = $fleshed_out_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($storage->pseudo_property_get_types as $property_name => $property_type) {
|
2020-04-16 06:22:00 +02:00
|
|
|
$property_name = substr($property_name, 1);
|
|
|
|
|
|
|
|
if (isset($class_context->vars_in_scope['$this->' . $property_name])) {
|
|
|
|
$fleshed_out_type = !$property_type->isMixed()
|
2020-05-11 04:45:01 +02:00
|
|
|
? \Psalm\Internal\Type\TypeExpander::expandUnion(
|
2020-04-16 06:22:00 +02:00
|
|
|
$codebase,
|
|
|
|
$property_type,
|
|
|
|
$fq_class_name,
|
|
|
|
$fq_class_name,
|
|
|
|
$parent_fq_class_name
|
|
|
|
)
|
|
|
|
: $property_type;
|
2020-03-22 01:46:38 +01:00
|
|
|
|
2020-04-16 06:22:00 +02:00
|
|
|
$class_context->vars_in_scope['$this->' . $property_name] = $fleshed_out_type;
|
|
|
|
}
|
2020-03-22 01:46:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function checkPropertyInitialization(
|
|
|
|
Codebase $codebase,
|
|
|
|
Config $config,
|
|
|
|
ClassLikeStorage $storage,
|
|
|
|
Context $class_context,
|
2020-09-07 01:36:47 +02:00
|
|
|
?Context $global_context = null,
|
|
|
|
?MethodAnalyzer $constructor_analyzer = null
|
2018-10-07 04:58:21 +02:00
|
|
|
) {
|
|
|
|
if (!$config->reportIssueInFile('PropertyNotSetInConstructor', $this->getFilePath())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($storage->declaring_method_ids['__construct'])
|
|
|
|
&& !$config->reportIssueInFile('MissingConstructor', $this->getFilePath())
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fq_class_name = $class_context->self ? $class_context->self : $this->fq_class_name;
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_class_name_lc = strtolower($fq_class_name);
|
2018-10-07 04:58:21 +02:00
|
|
|
|
|
|
|
$included_file_path = $this->getFilePath();
|
|
|
|
|
2018-11-02 02:52:39 +01:00
|
|
|
$method_already_analyzed = $codebase->analyzer->isMethodAlreadyAnalyzed(
|
2018-10-07 04:58:21 +02:00
|
|
|
$included_file_path,
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_class_name_lc . '::__construct',
|
2018-10-07 04:58:21 +02:00
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2019-06-13 22:15:29 +02:00
|
|
|
if ($method_already_analyzed && !$codebase->diff_methods) {
|
2019-06-13 22:28:21 +02:00
|
|
|
// this can happen when re-analysing a class that has been include()d inside another
|
2019-06-13 22:15:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
/** @var PhpParser\Node\Stmt\Class_ */
|
|
|
|
$class = $this->class;
|
|
|
|
$classlike_storage_provider = $codebase->classlike_storage_provider;
|
2020-02-15 02:54:26 +01:00
|
|
|
$class_storage = $classlike_storage_provider->get($fq_class_name_lc);
|
2018-10-07 04:58:21 +02:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$constructor_appearing_fqcln = $fq_class_name_lc;
|
2018-10-07 04:58:21 +02:00
|
|
|
|
|
|
|
$uninitialized_variables = [];
|
|
|
|
$uninitialized_properties = [];
|
2019-08-13 19:38:15 +02:00
|
|
|
$uninitialized_typed_properties = [];
|
2020-04-21 06:04:47 +02:00
|
|
|
$uninitialized_private_properties = false;
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
foreach ($storage->appearing_property_ids as $property_name => $appearing_property_id) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$property_class_name = $codebase->properties->getDeclaringClassForProperty(
|
2019-03-01 14:57:10 +01:00
|
|
|
$appearing_property_id,
|
|
|
|
true
|
2018-11-29 06:05:56 +01:00
|
|
|
);
|
2020-02-15 02:54:26 +01:00
|
|
|
|
|
|
|
if ($property_class_name === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-14 07:27:51 +01:00
|
|
|
$property_class_storage = $classlike_storage_provider->get($property_class_name);
|
2018-10-07 04:58:21 +02:00
|
|
|
|
|
|
|
$property = $property_class_storage->properties[$property_name];
|
|
|
|
|
|
|
|
$property_is_initialized = isset($property_class_storage->initialized_properties[$property_name]);
|
|
|
|
|
|
|
|
if ($property->is_static) {
|
|
|
|
continue;
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2019-08-11 06:22:28 +02:00
|
|
|
if ($property->has_default || $property_is_initialized) {
|
2018-10-07 04:58:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2019-08-11 06:22:28 +02:00
|
|
|
if ($property->type && $property->type->isNullable() && $property->type->from_docblock) {
|
2018-10-07 04:58:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2019-06-13 21:25:55 +02:00
|
|
|
if ($codebase->diff_methods && $method_already_analyzed && $property->location) {
|
2020-09-02 06:17:41 +02:00
|
|
|
[$start, $end] = $property->location->getSelectionBounds();
|
2019-06-13 21:25:55 +02:00
|
|
|
|
|
|
|
$existing_issues = $codebase->analyzer->getExistingIssuesForFile(
|
|
|
|
$this->getFilePath(),
|
|
|
|
$start,
|
|
|
|
$end,
|
|
|
|
'PropertyNotSetInConstructor'
|
|
|
|
);
|
|
|
|
|
2019-06-13 22:15:29 +02:00
|
|
|
if ($existing_issues) {
|
2020-01-22 03:07:44 +01:00
|
|
|
IssueBuffer::addIssues([$this->getFilePath() => $existing_issues]);
|
2019-06-13 22:15:29 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-06-13 21:25:55 +02:00
|
|
|
}
|
2019-06-13 22:15:29 +02:00
|
|
|
|
2020-02-03 22:43:17 +01:00
|
|
|
if ($property->location) {
|
|
|
|
$codebase->analyzer->removeExistingDataForFile(
|
|
|
|
$this->getFilePath(),
|
|
|
|
$property->location->raw_file_start,
|
|
|
|
$property->location->raw_file_end,
|
|
|
|
'PropertyNotSetInConstructor'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-13 22:15:29 +02:00
|
|
|
$codebase->file_reference_provider->addMethodReferenceToMissingClassMember(
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_class_name_lc . '::__construct',
|
2019-06-13 22:15:29 +02:00
|
|
|
strtolower($property_class_name) . '::$' . $property_name
|
|
|
|
);
|
|
|
|
|
2020-04-21 06:04:47 +02:00
|
|
|
if ($property->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE) {
|
|
|
|
$uninitialized_private_properties = true;
|
|
|
|
}
|
|
|
|
|
2019-06-13 22:15:29 +02:00
|
|
|
$uninitialized_variables[] = '$this->' . $property_name;
|
|
|
|
$uninitialized_properties[$property_class_name . '::$' . $property_name] = $property;
|
2019-08-13 19:38:15 +02:00
|
|
|
|
|
|
|
if ($property->type && !$property->type->isMixed()) {
|
|
|
|
$uninitialized_typed_properties[$property_class_name . '::$' . $property_name] = $property;
|
|
|
|
}
|
2018-10-07 04:58:21 +02:00
|
|
|
}
|
|
|
|
|
2019-06-13 22:15:29 +02:00
|
|
|
if (!$uninitialized_properties) {
|
2018-10-07 04:58:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
if (!$storage->abstract
|
2018-11-11 18:01:14 +01:00
|
|
|
&& !$constructor_analyzer
|
2018-10-07 04:58:21 +02:00
|
|
|
&& isset($storage->declaring_method_ids['__construct'])
|
2019-09-18 20:21:06 +02:00
|
|
|
&& isset($storage->appearing_method_ids['__construct'])
|
2018-10-07 04:58:21 +02:00
|
|
|
&& $class->extends
|
|
|
|
) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$constructor_declaring_fqcln = $storage->declaring_method_ids['__construct']->fq_class_name;
|
|
|
|
$constructor_appearing_fqcln = $storage->appearing_method_ids['__construct']->fq_class_name;
|
2018-10-07 04:58:21 +02:00
|
|
|
|
|
|
|
$constructor_class_storage = $classlike_storage_provider->get($constructor_declaring_fqcln);
|
|
|
|
|
|
|
|
// ignore oldstyle constructors and classes without any declared properties
|
|
|
|
if ($constructor_class_storage->user_defined
|
2018-11-02 03:46:14 +01:00
|
|
|
&& !$constructor_class_storage->stubbed
|
2018-10-07 04:58:21 +02:00
|
|
|
&& isset($constructor_class_storage->methods['__construct'])
|
|
|
|
) {
|
|
|
|
$constructor_storage = $constructor_class_storage->methods['__construct'];
|
|
|
|
|
|
|
|
$fake_constructor_params = array_map(
|
2019-06-14 22:53:40 +02:00
|
|
|
function (FunctionLikeParameter $param) : PhpParser\Node\Param {
|
2018-10-07 04:58:21 +02:00
|
|
|
$fake_param = (new PhpParser\Builder\Param($param->name));
|
|
|
|
if ($param->signature_type) {
|
|
|
|
/** @psalm-suppress DeprecatedMethod */
|
2019-01-12 16:52:23 +01:00
|
|
|
$fake_param->setTypeHint((string)$param->signature_type);
|
2018-10-07 04:58:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $fake_param->getNode();
|
|
|
|
},
|
|
|
|
$constructor_storage->params
|
|
|
|
);
|
|
|
|
|
|
|
|
$fake_constructor_stmt_args = array_map(
|
2019-06-14 22:53:40 +02:00
|
|
|
function (FunctionLikeParameter $param) : PhpParser\Node\Arg {
|
2018-10-07 04:58:21 +02:00
|
|
|
return new PhpParser\Node\Arg(new PhpParser\Node\Expr\Variable($param->name));
|
|
|
|
},
|
|
|
|
$constructor_storage->params
|
|
|
|
);
|
|
|
|
|
|
|
|
$fake_constructor_stmts = [
|
|
|
|
new PhpParser\Node\Stmt\Expression(
|
|
|
|
new PhpParser\Node\Expr\StaticCall(
|
2019-02-10 21:01:10 +01:00
|
|
|
new PhpParser\Node\Name\FullyQualified($constructor_declaring_fqcln),
|
2018-04-17 18:16:25 +02:00
|
|
|
new PhpParser\Node\Identifier('__construct'),
|
2018-10-07 04:58:21 +02:00
|
|
|
$fake_constructor_stmt_args,
|
2018-01-14 19:08:24 +01:00
|
|
|
[
|
2019-02-10 21:01:10 +01:00
|
|
|
'startLine' => $class->extends->getLine(),
|
2018-10-07 04:58:21 +02:00
|
|
|
'startFilePos' => $class->extends->getAttribute('startFilePos'),
|
|
|
|
'endFilePos' => $class->extends->getAttribute('endFilePos'),
|
2019-04-08 17:01:16 +02:00
|
|
|
'comments' => [new PhpParser\Comment\Doc(
|
|
|
|
'/** @psalm-suppress InaccessibleMethod */',
|
|
|
|
$class->extends->getLine(),
|
2019-04-09 03:51:18 +02:00
|
|
|
(int) $class->extends->getAttribute('startFilePos')
|
2019-04-08 17:01:16 +02:00
|
|
|
)],
|
2018-01-14 19:08:24 +01:00
|
|
|
]
|
2019-04-08 17:01:16 +02:00
|
|
|
),
|
|
|
|
[
|
|
|
|
'startLine' => $class->extends->getLine(),
|
|
|
|
'startFilePos' => $class->extends->getAttribute('startFilePos'),
|
|
|
|
'endFilePos' => $class->extends->getAttribute('endFilePos'),
|
|
|
|
'comments' => [new PhpParser\Comment\Doc(
|
|
|
|
'/** @psalm-suppress InaccessibleMethod */',
|
|
|
|
$class->extends->getLine(),
|
2019-04-09 03:51:18 +02:00
|
|
|
(int) $class->extends->getAttribute('startFilePos')
|
2019-04-08 17:01:16 +02:00
|
|
|
)],
|
|
|
|
]
|
2018-10-07 04:58:21 +02:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
$fake_stmt = new PhpParser\Node\Stmt\ClassMethod(
|
|
|
|
new PhpParser\Node\Identifier('__construct'),
|
|
|
|
[
|
|
|
|
'type' => PhpParser\Node\Stmt\Class_::MODIFIER_PUBLIC,
|
|
|
|
'params' => $fake_constructor_params,
|
|
|
|
'stmts' => $fake_constructor_stmts,
|
2019-02-10 21:01:10 +01:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'startLine' => $class->extends->getLine(),
|
|
|
|
'startFilePos' => $class->extends->getAttribute('startFilePos'),
|
|
|
|
'endFilePos' => $class->extends->getAttribute('endFilePos'),
|
2018-10-07 04:58:21 +02:00
|
|
|
]
|
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
$codebase->analyzer->disableMixedCounts();
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2020-03-28 19:45:58 +01:00
|
|
|
$was_collecting_initializations = $class_context->collect_initializations;
|
|
|
|
|
|
|
|
$class_context->collect_initializations = true;
|
2020-04-21 06:04:47 +02:00
|
|
|
$class_context->collect_nonprivate_initializations = !$uninitialized_private_properties;
|
2020-03-28 19:45:58 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$constructor_analyzer = $this->analyzeClassMethod(
|
2018-10-07 04:58:21 +02:00
|
|
|
$fake_stmt,
|
|
|
|
$storage,
|
|
|
|
$this,
|
|
|
|
$class_context,
|
|
|
|
$global_context,
|
|
|
|
true
|
|
|
|
);
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2020-03-28 19:45:58 +01:00
|
|
|
$class_context->collect_initializations = $was_collecting_initializations;
|
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
$codebase->analyzer->enableMixedCounts();
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if ($constructor_analyzer) {
|
2018-10-07 04:58:21 +02:00
|
|
|
$method_context = clone $class_context;
|
|
|
|
$method_context->collect_initializations = true;
|
2020-04-21 06:04:47 +02:00
|
|
|
$method_context->collect_nonprivate_initializations = !$uninitialized_private_properties;
|
2018-10-07 04:58:21 +02:00
|
|
|
$method_context->self = $fq_class_name;
|
2020-03-25 14:18:49 +01:00
|
|
|
|
|
|
|
$this_atomic_object_type = new Type\Atomic\TNamedObject($fq_class_name);
|
2020-07-25 23:26:07 +02:00
|
|
|
$this_atomic_object_type->was_static = !$storage->final;
|
2020-03-25 14:18:49 +01:00
|
|
|
|
|
|
|
$method_context->vars_in_scope['$this'] = new Type\Union([$this_atomic_object_type]);
|
2018-10-07 04:58:21 +02:00
|
|
|
$method_context->vars_possibly_in_scope['$this'] = true;
|
2020-03-26 17:35:27 +01:00
|
|
|
$method_context->calling_method_id = strtolower($fq_class_name) . '::__construct';
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$constructor_analyzer->analyze(
|
|
|
|
$method_context,
|
|
|
|
new \Psalm\Internal\Provider\NodeDataProvider(),
|
|
|
|
$global_context,
|
|
|
|
true
|
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2020-07-08 17:46:55 +02:00
|
|
|
foreach ($uninitialized_properties as $property_id => $property_storage) {
|
2020-09-02 06:17:41 +02:00
|
|
|
[, $property_name] = explode('::$', $property_id);
|
2019-01-20 17:20:39 +01:00
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
if (!isset($method_context->vars_in_scope['$this->' . $property_name])) {
|
2019-06-01 01:49:24 +02:00
|
|
|
$end_type = Type::getVoid();
|
|
|
|
$end_type->initialized = false;
|
|
|
|
} else {
|
|
|
|
$end_type = $method_context->vars_in_scope['$this->' . $property_name];
|
2018-10-07 04:58:21 +02:00
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
$constructor_class_property_storage = $property_storage;
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2019-04-14 18:48:59 +02:00
|
|
|
$error_location = $property_storage->location;
|
|
|
|
|
|
|
|
if ($storage->declaring_property_ids[$property_name] !== $fq_class_name) {
|
2019-06-01 06:56:54 +02:00
|
|
|
$error_location = $storage->location ?: $storage->stmt_location;
|
2019-04-14 18:48:59 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if ($fq_class_name_lc !== $constructor_appearing_fqcln
|
2019-01-20 17:20:39 +01:00
|
|
|
&& $property_storage->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE
|
|
|
|
) {
|
2019-03-20 00:43:12 +01:00
|
|
|
$a_class_storage = $classlike_storage_provider->get(
|
|
|
|
$end_type->initialized_class ?: $constructor_appearing_fqcln
|
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
if (!isset($a_class_storage->declaring_property_ids[$property_name])) {
|
|
|
|
$constructor_class_property_storage = null;
|
|
|
|
} else {
|
|
|
|
$declaring_property_class = $a_class_storage->declaring_property_ids[$property_name];
|
|
|
|
$constructor_class_property_storage = $classlike_storage_provider
|
|
|
|
->get($declaring_property_class)
|
|
|
|
->properties[$property_name];
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
if ($property_storage->location
|
2019-04-14 18:48:59 +02:00
|
|
|
&& $error_location
|
2018-10-07 04:58:21 +02:00
|
|
|
&& (!$end_type->initialized || $property_storage !== $constructor_class_property_storage)
|
|
|
|
) {
|
2020-07-08 17:46:55 +02:00
|
|
|
if ($property_storage->type) {
|
|
|
|
$expected_visibility = $uninitialized_private_properties
|
|
|
|
? 'private or final '
|
|
|
|
: '';
|
2020-04-21 06:04:47 +02:00
|
|
|
|
2020-07-08 17:46:55 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PropertyNotSetInConstructor(
|
|
|
|
'Property ' . $class_storage->name . '::$' . $property_name
|
|
|
|
. ' is not defined in constructor of '
|
|
|
|
. $this->fq_class_name . ' and in any ' . $expected_visibility
|
|
|
|
. 'methods called in the constructor',
|
|
|
|
$error_location,
|
|
|
|
$property_id
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
} elseif (!$property_storage->has_default) {
|
|
|
|
if (isset($this->inferred_property_types[$property_name])) {
|
|
|
|
$this->inferred_property_types[$property_name]->addType(new Type\Atomic\TNull());
|
|
|
|
$this->inferred_property_types[$property_name]->setFromDocblock();
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 02:52:39 +01:00
|
|
|
$codebase->analyzer->setAnalyzedMethod(
|
|
|
|
$included_file_path,
|
2020-02-15 02:54:26 +01:00
|
|
|
$fq_class_name_lc . '::__construct',
|
2018-11-02 02:52:39 +01:00
|
|
|
true
|
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-10-07 04:58:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:38:15 +02:00
|
|
|
if (!$storage->abstract && $uninitialized_typed_properties) {
|
2020-09-10 23:41:45 +02:00
|
|
|
foreach ($uninitialized_typed_properties as $uninitialized_property) {
|
|
|
|
if ($uninitialized_property->location) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingConstructor(
|
|
|
|
$class_storage->name . ' has an uninitialized variable ' . $uninitialized_variables[0] .
|
|
|
|
', but no constructor',
|
|
|
|
$uninitialized_property->location
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 18:23:53 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
private function analyzeTraitUse(
|
2018-05-30 19:08:56 +02:00
|
|
|
Aliases $aliases,
|
2018-05-30 18:23:53 +02:00
|
|
|
PhpParser\Node\Stmt\TraitUse $stmt,
|
2018-11-11 18:01:14 +01:00
|
|
|
ProjectAnalyzer $project_analyzer,
|
2018-05-30 18:23:53 +02:00
|
|
|
ClassLikeStorage $storage,
|
|
|
|
Context $class_context,
|
2020-09-07 01:36:47 +02:00
|
|
|
?Context $global_context = null,
|
|
|
|
?MethodAnalyzer &$constructor_analyzer = null,
|
|
|
|
?TraitAnalyzer $previous_trait_analyzer = null
|
2018-05-30 18:23:53 +02:00
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase = $this->getCodebase();
|
2018-05-30 18:23:53 +02:00
|
|
|
|
|
|
|
$previous_context_include_location = $class_context->include_location;
|
|
|
|
|
|
|
|
foreach ($stmt->traits as $trait_name) {
|
2019-04-13 21:38:09 +02:00
|
|
|
$trait_location = new CodeLocation($this, $trait_name, null, true);
|
2018-05-30 18:23:53 +02:00
|
|
|
$class_context->include_location = new CodeLocation($this, $trait_name, null, true);
|
|
|
|
|
|
|
|
$fq_trait_name = self::getFQCLNFromNameObject(
|
|
|
|
$trait_name,
|
2018-05-30 19:08:56 +02:00
|
|
|
$aliases
|
2018-05-30 18:23:53 +02:00
|
|
|
);
|
|
|
|
|
2019-04-13 21:38:09 +02:00
|
|
|
if (!$codebase->classlikes->hasFullyQualifiedTraitName($fq_trait_name, $trait_location)) {
|
2018-05-30 18:23:53 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedTrait(
|
|
|
|
'Trait ' . $fq_trait_name . ' does not exist',
|
2020-07-21 20:15:26 +02:00
|
|
|
new CodeLocation($previous_trait_analyzer ?: $this, $trait_name)
|
2018-05-30 18:23:53 +02:00
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2018-05-30 18:23:53 +02:00
|
|
|
)) {
|
2020-09-05 00:10:14 +02:00
|
|
|
// fall through
|
2018-05-30 18:23:53 +02:00
|
|
|
}
|
2020-09-04 22:50:10 +02:00
|
|
|
|
2020-09-05 00:10:14 +02:00
|
|
|
return false;
|
2018-05-30 18:23:53 +02:00
|
|
|
} else {
|
|
|
|
if (!$codebase->traitHasCorrectCase($fq_trait_name)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedTrait(
|
|
|
|
'Trait ' . $fq_trait_name . ' has wrong casing',
|
2020-07-21 20:15:26 +02:00
|
|
|
new CodeLocation($previous_trait_analyzer ?: $this, $trait_name)
|
2018-05-30 18:23:53 +02:00
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2018-05-30 18:23:53 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-29 07:21:00 +01:00
|
|
|
$fq_trait_name_resolved = $codebase->classlikes->getUnAliasedName($fq_trait_name);
|
|
|
|
$trait_storage = $codebase->classlike_storage_provider->get($fq_trait_name_resolved);
|
2018-08-10 19:25:25 +02:00
|
|
|
|
|
|
|
if ($trait_storage->deprecated) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DeprecatedTrait(
|
|
|
|
'Trait ' . $fq_trait_name . ' is deprecated',
|
2020-07-21 20:15:26 +02:00
|
|
|
new CodeLocation($previous_trait_analyzer ?: $this, $trait_name)
|
2018-08-10 19:25:25 +02:00
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2018-08-10 19:25:25 +02:00
|
|
|
)) {
|
2018-11-09 20:18:46 +01:00
|
|
|
// fall through
|
2018-08-10 19:25:25 +02:00
|
|
|
}
|
2020-02-22 16:04:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($storage->mutation_free && !$trait_storage->mutation_free) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MutableDependency(
|
|
|
|
$storage->name . ' is marked immutable but ' . $fq_trait_name . ' is not',
|
2020-07-21 20:15:26 +02:00
|
|
|
new CodeLocation($previous_trait_analyzer ?: $this, $trait_name)
|
2020-02-22 16:04:38 +01:00
|
|
|
),
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2018-08-10 19:25:25 +02:00
|
|
|
}
|
|
|
|
|
2019-11-29 07:21:00 +01:00
|
|
|
$trait_file_analyzer = $project_analyzer->getFileAnalyzerForClassLike($fq_trait_name_resolved);
|
|
|
|
$trait_node = $codebase->classlikes->getTraitNode($fq_trait_name_resolved);
|
2020-03-03 04:27:54 +01:00
|
|
|
$trait_aliases = $trait_storage->aliases;
|
|
|
|
if ($trait_aliases === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$trait_analyzer = new TraitAnalyzer(
|
2018-05-30 18:23:53 +02:00
|
|
|
$trait_node,
|
2018-11-11 18:01:14 +01:00
|
|
|
$trait_file_analyzer,
|
2019-11-29 07:21:00 +01:00
|
|
|
$fq_trait_name_resolved,
|
2018-05-30 18:23:53 +02:00
|
|
|
$trait_aliases
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($trait_node->stmts as $trait_stmt) {
|
|
|
|
if ($trait_stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$trait_method_analyzer = $this->analyzeClassMethod(
|
2018-05-30 18:23:53 +02:00
|
|
|
$trait_stmt,
|
|
|
|
$storage,
|
2018-11-11 18:01:14 +01:00
|
|
|
$trait_analyzer,
|
2018-05-30 18:23:53 +02:00
|
|
|
$class_context,
|
|
|
|
$global_context
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($trait_stmt->name->name === '__construct') {
|
2018-11-11 18:01:14 +01:00
|
|
|
$constructor_analyzer = $trait_method_analyzer;
|
2018-05-30 18:23:53 +02:00
|
|
|
}
|
|
|
|
} elseif ($trait_stmt instanceof PhpParser\Node\Stmt\TraitUse) {
|
|
|
|
if ($this->analyzeTraitUse(
|
2018-05-30 19:08:56 +02:00
|
|
|
$trait_aliases,
|
2018-05-30 18:23:53 +02:00
|
|
|
$trait_stmt,
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer,
|
2018-05-30 18:23:53 +02:00
|
|
|
$storage,
|
|
|
|
$class_context,
|
|
|
|
$global_context,
|
2020-07-21 20:15:26 +02:00
|
|
|
$constructor_analyzer,
|
|
|
|
$trait_analyzer
|
2018-05-30 18:23:53 +02:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 07:55:24 +02:00
|
|
|
|
|
|
|
$trait_file_analyzer->clearSourceBeforeDestruction();
|
2018-05-30 18:23:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_context->include_location = $previous_context_include_location;
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:08:24 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-02-12 16:46:45 +01:00
|
|
|
private function checkForMissingPropertyType(
|
|
|
|
StatementsSource $source,
|
2019-03-01 14:57:10 +01:00
|
|
|
PhpParser\Node\Stmt\Property $stmt,
|
|
|
|
Context $context
|
2018-02-12 16:46:45 +01:00
|
|
|
) {
|
2018-01-14 19:08:24 +01:00
|
|
|
$comment = $stmt->getDocComment();
|
|
|
|
|
|
|
|
if (!$comment || !$comment->getText()) {
|
2018-02-12 16:46:45 +01:00
|
|
|
$fq_class_name = $source->getFQCLN();
|
2018-04-17 18:16:25 +02:00
|
|
|
$property_name = $stmt->props[0]->name->name;
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase = $this->getCodebase();
|
2018-02-09 00:14:28 +01:00
|
|
|
|
2019-01-19 16:09:26 +01:00
|
|
|
$property_id = $fq_class_name . '::$' . $property_name;
|
|
|
|
|
2018-02-09 00:14:28 +01:00
|
|
|
$declaring_property_class = $codebase->properties->getDeclaringClassForProperty(
|
2019-03-01 14:57:10 +01:00
|
|
|
$property_id,
|
|
|
|
true
|
2018-01-14 19:08:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!$declaring_property_class) {
|
2020-03-09 19:24:19 +01:00
|
|
|
return;
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$fq_class_name = $declaring_property_class;
|
|
|
|
|
2019-01-19 16:09:26 +01:00
|
|
|
// gets inherited property type
|
2019-03-01 14:57:10 +01:00
|
|
|
$class_property_type = $codebase->properties->getPropertyType($property_id, false, $source, $context);
|
2019-01-19 16:09:26 +01:00
|
|
|
|
2020-07-08 20:32:16 +02:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
|
|
$property_storage = $class_storage->properties[$property_name];
|
|
|
|
|
|
|
|
if ($class_property_type && ($property_storage->type_location || !$codebase->alter_code)) {
|
2019-01-19 16:09:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = 'Property ' . $property_id . ' does not have a declared type';
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2020-07-08 01:32:44 +02:00
|
|
|
$suggested_type = $property_storage->suggested_type;
|
|
|
|
|
|
|
|
if (isset($this->inferred_property_types[$property_name])) {
|
|
|
|
$suggested_type = $suggested_type
|
|
|
|
? Type::combineUnionTypes(
|
|
|
|
$suggested_type,
|
|
|
|
$this->inferred_property_types[$property_name],
|
|
|
|
$codebase
|
|
|
|
)
|
|
|
|
: $this->inferred_property_types[$property_name];
|
|
|
|
}
|
|
|
|
|
2020-07-08 20:43:36 +02:00
|
|
|
if ($suggested_type && !$property_storage->has_default && $property_storage->is_static) {
|
|
|
|
$suggested_type->addType(new Type\Atomic\TNull());
|
|
|
|
}
|
|
|
|
|
2020-07-08 01:32:44 +02:00
|
|
|
if ($suggested_type && !$suggested_type->isNull()) {
|
2018-01-14 19:08:24 +01:00
|
|
|
$message .= ' - consider ' . str_replace(
|
2020-07-08 01:32:44 +02:00
|
|
|
['<array-key, mixed>', '<empty, empty>'],
|
2018-01-14 19:08:24 +01:00
|
|
|
'',
|
2020-07-08 01:32:44 +02:00
|
|
|
(string)$suggested_type
|
2018-01-14 19:08:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-08 18:03:12 +02:00
|
|
|
$project_analyzer = ProjectAnalyzer::getInstance();
|
|
|
|
|
|
|
|
if ($codebase->alter_code
|
2020-07-11 01:11:06 +02:00
|
|
|
&& $source === $this
|
2020-07-08 18:03:12 +02:00
|
|
|
&& isset($project_analyzer->getIssuesToFix()['MissingPropertyType'])
|
|
|
|
&& !\in_array('MissingPropertyType', $this->getSuppressedIssues())
|
|
|
|
&& $suggested_type
|
|
|
|
) {
|
|
|
|
if ($suggested_type->hasMixed() || $suggested_type->isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::addOrUpdatePropertyType(
|
|
|
|
$project_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$suggested_type,
|
|
|
|
$this,
|
|
|
|
$suggested_type->from_docblock
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:08:24 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingPropertyType(
|
|
|
|
$message,
|
2020-09-01 15:21:03 +02:00
|
|
|
new CodeLocation($source, $stmt->props[0]->name),
|
|
|
|
$property_id
|
2018-01-14 19:08:24 +01:00
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 18:03:12 +02:00
|
|
|
private static function addOrUpdatePropertyType(
|
|
|
|
ProjectAnalyzer $project_analyzer,
|
|
|
|
PhpParser\Node\Stmt\Property $property,
|
|
|
|
Type\Union $inferred_type,
|
|
|
|
StatementsSource $source,
|
|
|
|
bool $docblock_only = false
|
|
|
|
) : void {
|
|
|
|
$manipulator = PropertyDocblockManipulator::getForProperty(
|
|
|
|
$project_analyzer,
|
|
|
|
$source->getFilePath(),
|
|
|
|
$property
|
|
|
|
);
|
|
|
|
|
|
|
|
$codebase = $project_analyzer->getCodebase();
|
|
|
|
|
|
|
|
$allow_native_type = !$docblock_only
|
|
|
|
&& $codebase->php_major_version >= 7
|
|
|
|
&& ($codebase->php_major_version > 7 || $codebase->php_minor_version >= 4)
|
|
|
|
&& $codebase->allow_backwards_incompatible_changes;
|
|
|
|
|
|
|
|
$manipulator->setType(
|
|
|
|
$allow_native_type
|
|
|
|
? (string) $inferred_type->toPhpString(
|
|
|
|
$source->getNamespace(),
|
|
|
|
$source->getAliasedClassesFlipped(),
|
|
|
|
$source->getFQCLN(),
|
|
|
|
$codebase->php_major_version,
|
|
|
|
$codebase->php_minor_version
|
|
|
|
) : null,
|
|
|
|
$inferred_type->toNamespacedString(
|
|
|
|
$source->getNamespace(),
|
|
|
|
$source->getAliasedClassesFlipped(),
|
|
|
|
$source->getFQCLN(),
|
|
|
|
false
|
|
|
|
),
|
|
|
|
$inferred_type->toNamespacedString(
|
|
|
|
$source->getNamespace(),
|
|
|
|
$source->getAliasedClassesFlipped(),
|
|
|
|
$source->getFQCLN(),
|
|
|
|
true
|
|
|
|
),
|
|
|
|
$inferred_type->canBeFullyExpressedInPhp()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:08:24 +01:00
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @return MethodAnalyzer|null
|
2018-01-14 19:08:24 +01:00
|
|
|
*/
|
|
|
|
private function analyzeClassMethod(
|
|
|
|
PhpParser\Node\Stmt\ClassMethod $stmt,
|
2018-01-26 17:50:29 +01:00
|
|
|
ClassLikeStorage $class_storage,
|
2018-11-06 03:57:36 +01:00
|
|
|
SourceAnalyzer $source,
|
2018-01-14 19:08:24 +01:00
|
|
|
Context $class_context,
|
2020-09-07 01:36:47 +02:00
|
|
|
?Context $global_context = null,
|
|
|
|
bool $is_fake = false
|
2018-01-14 19:08:24 +01:00
|
|
|
) {
|
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2019-01-19 18:19:51 +01:00
|
|
|
if ($stmt->stmts === null && !$stmt->isAbstract()) {
|
|
|
|
\Psalm\IssueBuffer::add(
|
|
|
|
new \Psalm\Issue\ParseError(
|
|
|
|
'Non-abstract class method must have statements',
|
|
|
|
new CodeLocation($this, $stmt)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-01 16:30:10 +02:00
|
|
|
try {
|
|
|
|
$method_analyzer = new MethodAnalyzer($stmt, $source);
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
\Psalm\IssueBuffer::add(
|
|
|
|
new \Psalm\Issue\ParseError(
|
|
|
|
'Problem loading method: ' . $e->getMessage(),
|
|
|
|
new CodeLocation($this, $stmt)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$actual_method_id = $method_analyzer->getMethodId();
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$project_analyzer = $source->getProjectAnalyzer();
|
|
|
|
$codebase = $source->getCodebase();
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-01-26 19:51:00 +01:00
|
|
|
$analyzed_method_id = $actual_method_id;
|
|
|
|
|
2018-10-03 23:11:08 +02:00
|
|
|
$included_file_path = $source->getFilePath();
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if ($class_context->self && strtolower($class_context->self) !== strtolower((string) $source->getFQCLN())) {
|
|
|
|
$analyzed_method_id = $method_analyzer->getMethodId($class_context->self);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($analyzed_method_id);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if ((string) $actual_method_id !== (string) $declaring_method_id) {
|
2018-03-13 23:11:57 +01:00
|
|
|
// the method is an abstract trait method
|
|
|
|
|
2019-03-02 20:17:26 +01:00
|
|
|
$declaring_method_storage = $method_analyzer->getFunctionLikeStorage();
|
2018-03-13 23:11:57 +01:00
|
|
|
|
2019-03-02 20:17:26 +01:00
|
|
|
if (!$declaring_method_storage instanceof \Psalm\Storage\MethodStorage) {
|
2018-03-17 05:19:55 +01:00
|
|
|
throw new \LogicException('This should never happen');
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:17:26 +01:00
|
|
|
if ($declaring_method_id && $declaring_method_storage->abstract) {
|
|
|
|
$implementer_method_storage = $codebase->methods->getStorage($declaring_method_id);
|
2019-03-29 03:47:17 +01:00
|
|
|
$declaring_storage = $codebase->classlike_storage_provider->get(
|
2020-02-15 02:54:26 +01:00
|
|
|
$actual_method_id->fq_class_name
|
2019-03-29 03:47:17 +01:00
|
|
|
);
|
2018-03-13 23:11:57 +01:00
|
|
|
|
2020-03-12 04:38:49 +01:00
|
|
|
MethodComparator::compare(
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase,
|
2020-08-14 22:26:55 +02:00
|
|
|
null,
|
2018-03-13 23:11:57 +01:00
|
|
|
$class_storage,
|
2019-03-29 03:47:17 +01:00
|
|
|
$declaring_storage,
|
2018-03-13 23:11:57 +01:00
|
|
|
$implementer_method_storage,
|
|
|
|
$declaring_method_storage,
|
2019-01-16 21:08:11 +01:00
|
|
|
$this->fq_class_name,
|
2019-01-16 16:59:06 +01:00
|
|
|
$implementer_method_storage->visibility,
|
2018-03-13 23:11:57 +01:00
|
|
|
new CodeLocation($source, $stmt),
|
|
|
|
$implementer_method_storage->suppressed_issues,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:08:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$trait_safe_method_id = strtolower((string) $analyzed_method_id);
|
2018-10-04 00:16:33 +02:00
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$actual_method_id_str = strtolower((string) $actual_method_id);
|
|
|
|
|
|
|
|
if ($actual_method_id_str !== $trait_safe_method_id) {
|
|
|
|
$trait_safe_method_id .= '&' . $actual_method_id_str;
|
2018-10-04 00:16:33 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 02:52:39 +01:00
|
|
|
$method_already_analyzed = $codebase->analyzer->isMethodAlreadyAnalyzed(
|
2018-10-03 23:11:08 +02:00
|
|
|
$included_file_path,
|
2018-10-04 00:16:33 +02:00
|
|
|
$trait_safe_method_id
|
2018-09-26 00:37:24 +02:00
|
|
|
);
|
|
|
|
|
2018-10-26 06:59:14 +02:00
|
|
|
$start = (int)$stmt->getAttribute('startFilePos');
|
|
|
|
$end = (int)$stmt->getAttribute('endFilePos');
|
|
|
|
|
|
|
|
$comments = $stmt->getComments();
|
|
|
|
|
|
|
|
if ($comments) {
|
|
|
|
$start = $comments[0]->getFilePos();
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if ($codebase->diff_methods
|
2018-11-02 02:52:39 +01:00
|
|
|
&& $method_already_analyzed
|
2018-09-26 00:37:24 +02:00
|
|
|
&& !$class_context->collect_initializations
|
|
|
|
&& !$class_context->collect_mutations
|
|
|
|
&& !$is_fake
|
|
|
|
) {
|
2019-05-30 16:30:41 +02:00
|
|
|
$project_analyzer->progress->debug(
|
|
|
|
'Skipping analysis of pre-analyzed method ' . $analyzed_method_id . "\n"
|
|
|
|
);
|
2018-09-26 00:37:24 +02:00
|
|
|
|
2018-09-26 22:33:59 +02:00
|
|
|
$existing_issues = $codebase->analyzer->getExistingIssuesForFile(
|
|
|
|
$source->getFilePath(),
|
|
|
|
$start,
|
|
|
|
$end
|
|
|
|
);
|
|
|
|
|
2020-01-22 03:07:44 +01:00
|
|
|
IssueBuffer::addIssues([$source->getFilePath() => $existing_issues]);
|
2018-09-26 22:33:59 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
return $method_analyzer;
|
2018-09-26 00:37:24 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 06:59:14 +02:00
|
|
|
$codebase->analyzer->removeExistingDataForFile(
|
|
|
|
$source->getFilePath(),
|
|
|
|
$start,
|
|
|
|
$end
|
|
|
|
);
|
|
|
|
|
2018-06-22 07:13:49 +02:00
|
|
|
$method_context = clone $class_context;
|
2019-06-26 06:14:06 +02:00
|
|
|
foreach ($method_context->vars_in_scope as $context_var_id => $context_type) {
|
|
|
|
$method_context->vars_in_scope[$context_var_id] = clone $context_type;
|
|
|
|
}
|
2018-06-22 07:13:49 +02:00
|
|
|
$method_context->collect_exceptions = $config->check_for_throws_docblock;
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$type_provider = new \Psalm\Internal\Provider\NodeDataProvider();
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$method_analyzer->analyze(
|
2018-06-22 07:13:49 +02:00
|
|
|
$method_context,
|
2019-11-25 17:44:54 +01:00
|
|
|
$type_provider,
|
2018-01-14 19:08:24 +01:00
|
|
|
$global_context ? clone $global_context : null
|
|
|
|
);
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($stmt->name->name !== '__construct'
|
2018-03-13 17:52:00 +01:00
|
|
|
&& $config->reportIssueInFile('InvalidReturnType', $source->getFilePath())
|
2018-09-04 20:37:33 +02:00
|
|
|
&& $class_context->self
|
2018-03-13 17:52:00 +01:00
|
|
|
) {
|
2019-07-29 01:03:12 +02:00
|
|
|
self::analyzeClassMethodReturnType(
|
|
|
|
$stmt,
|
|
|
|
$method_analyzer,
|
|
|
|
$source,
|
2019-11-25 17:44:54 +01:00
|
|
|
$type_provider,
|
2019-07-29 01:03:12 +02:00
|
|
|
$codebase,
|
|
|
|
$class_storage,
|
|
|
|
$class_context->self,
|
|
|
|
$analyzed_method_id,
|
2020-03-29 05:37:42 +02:00
|
|
|
$actual_method_id,
|
|
|
|
$method_context->has_returned
|
2019-07-29 01:03:12 +02:00
|
|
|
);
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
if (!$method_already_analyzed
|
|
|
|
&& !$class_context->collect_initializations
|
|
|
|
&& !$class_context->collect_mutations
|
|
|
|
&& !$is_fake
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->setAnalyzedMethod($included_file_path, $trait_safe_method_id);
|
|
|
|
}
|
2018-01-26 16:59:30 +01:00
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
return $method_analyzer;
|
|
|
|
}
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
public static function analyzeClassMethodReturnType(
|
|
|
|
PhpParser\Node\Stmt\ClassMethod $stmt,
|
|
|
|
MethodAnalyzer $method_analyzer,
|
|
|
|
SourceAnalyzer $source,
|
2019-11-25 17:44:54 +01:00
|
|
|
\Psalm\Internal\Provider\NodeDataProvider $type_provider,
|
2019-07-29 01:03:12 +02:00
|
|
|
Codebase $codebase,
|
|
|
|
ClassLikeStorage $class_storage,
|
|
|
|
string $fq_classlike_name,
|
2020-02-15 02:54:26 +01:00
|
|
|
\Psalm\Internal\MethodIdentifier $analyzed_method_id,
|
2020-03-29 05:37:42 +02:00
|
|
|
\Psalm\Internal\MethodIdentifier $actual_method_id,
|
|
|
|
bool $did_explicitly_return
|
2019-07-29 01:03:12 +02:00
|
|
|
) : void {
|
|
|
|
$secondary_return_type_location = null;
|
|
|
|
|
|
|
|
$actual_method_storage = $codebase->methods->getStorage($actual_method_id);
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
$return_type_location = $codebase->methods->getMethodReturnTypeLocation(
|
|
|
|
$actual_method_id,
|
|
|
|
$secondary_return_type_location
|
|
|
|
);
|
2019-01-24 22:09:04 +01:00
|
|
|
|
2019-07-29 01:44:36 +02:00
|
|
|
$original_fq_classlike_name = $fq_classlike_name;
|
|
|
|
|
2019-12-20 02:42:57 +01:00
|
|
|
$return_type = $codebase->methods->getMethodReturnType(
|
|
|
|
$analyzed_method_id,
|
|
|
|
$fq_classlike_name,
|
|
|
|
$method_analyzer
|
|
|
|
);
|
2019-06-25 19:20:30 +02:00
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
if ($return_type && $class_storage->template_type_extends) {
|
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($analyzed_method_id);
|
2019-06-25 19:20:30 +02:00
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
if ($declaring_method_id) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$declaring_class_name = $declaring_method_id->fq_class_name;
|
2019-01-24 22:09:04 +01:00
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($declaring_class_name);
|
2019-01-24 22:43:22 +01:00
|
|
|
}
|
2019-01-24 22:09:04 +01:00
|
|
|
|
2019-11-08 17:56:33 +01:00
|
|
|
if ($class_storage->template_types) {
|
|
|
|
$template_params = [];
|
|
|
|
|
|
|
|
foreach ($class_storage->template_types as $param_name => $template_map) {
|
|
|
|
$key = array_keys($template_map)[0];
|
|
|
|
|
|
|
|
$template_params[] = new Type\Union([
|
|
|
|
new Type\Atomic\TTemplateParam(
|
|
|
|
$param_name,
|
2019-11-08 18:49:53 +01:00
|
|
|
\reset($template_map)[0],
|
2019-11-08 17:56:33 +01:00
|
|
|
$key
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this_object_type = new Type\Atomic\TGenericObject(
|
|
|
|
$original_fq_classlike_name,
|
|
|
|
$template_params
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this_object_type = new Type\Atomic\TNamedObject($original_fq_classlike_name);
|
|
|
|
}
|
|
|
|
|
2020-03-12 04:15:15 +01:00
|
|
|
$class_template_params = ClassTemplateParamCollector::collect(
|
2019-07-29 01:03:12 +02:00
|
|
|
$codebase,
|
|
|
|
$class_storage,
|
2020-05-27 05:29:37 +02:00
|
|
|
$codebase->classlike_storage_provider->get($original_fq_classlike_name),
|
2019-11-08 17:56:33 +01:00
|
|
|
strtolower($stmt->name->name),
|
|
|
|
$this_object_type
|
2019-07-29 01:03:12 +02:00
|
|
|
) ?: [];
|
2018-03-23 21:34:45 +01:00
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
$template_result = new \Psalm\Internal\Type\TemplateResult(
|
|
|
|
$class_template_params ?: [],
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2020-04-17 07:16:15 +02:00
|
|
|
$return_type = UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
2019-11-04 03:27:40 +01:00
|
|
|
$return_type,
|
|
|
|
$template_result,
|
|
|
|
$codebase,
|
2019-11-06 17:20:51 +01:00
|
|
|
null,
|
2020-02-29 21:43:35 +01:00
|
|
|
null,
|
2020-07-03 18:25:33 +02:00
|
|
|
null,
|
|
|
|
$original_fq_classlike_name
|
2019-11-04 03:27:40 +01:00
|
|
|
);
|
2019-07-29 01:03:12 +02:00
|
|
|
}
|
2018-03-23 21:34:45 +01:00
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
$overridden_method_ids = isset($class_storage->overridden_method_ids[strtolower($stmt->name->name)])
|
|
|
|
? $class_storage->overridden_method_ids[strtolower($stmt->name->name)]
|
|
|
|
: [];
|
2018-04-17 18:16:25 +02:00
|
|
|
|
2019-07-29 01:44:36 +02:00
|
|
|
if (!$return_type
|
|
|
|
&& !$class_storage->is_interface
|
2019-12-20 02:42:57 +01:00
|
|
|
&& $overridden_method_ids
|
2019-07-29 01:44:36 +02:00
|
|
|
) {
|
2019-12-20 02:42:57 +01:00
|
|
|
foreach ($overridden_method_ids as $interface_method_id) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$interface_class = $interface_method_id->fq_class_name;
|
2018-01-26 17:50:29 +01:00
|
|
|
|
2019-12-20 02:42:57 +01:00
|
|
|
if (!$codebase->classlikes->interfaceExists($interface_class)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
$interface_return_type = $codebase->methods->getMethodReturnType(
|
|
|
|
$interface_method_id,
|
|
|
|
$interface_class
|
|
|
|
);
|
2019-01-24 22:43:22 +01:00
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
$interface_return_type_location = $codebase->methods->getMethodReturnTypeLocation(
|
|
|
|
$interface_method_id
|
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
FunctionLike\ReturnTypeAnalyzer::verifyReturnType(
|
|
|
|
$stmt,
|
2019-09-01 06:56:46 +02:00
|
|
|
$stmt->getStmts() ?: [],
|
2019-07-29 01:03:12 +02:00
|
|
|
$source,
|
2019-11-25 17:44:54 +01:00
|
|
|
$type_provider,
|
2019-07-29 01:03:12 +02:00
|
|
|
$method_analyzer,
|
|
|
|
$interface_return_type,
|
|
|
|
$interface_class,
|
|
|
|
$interface_return_type_location,
|
2020-03-29 05:37:42 +02:00
|
|
|
[$analyzed_method_id],
|
|
|
|
$did_explicitly_return
|
2019-07-29 01:03:12 +02:00
|
|
|
);
|
|
|
|
}
|
2018-09-26 00:37:24 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 02:54:26 +01:00
|
|
|
if ($actual_method_storage->overridden_downstream) {
|
|
|
|
$overridden_method_ids['overridden::downstream'] = 'overridden::downstream';
|
|
|
|
}
|
|
|
|
|
2019-07-29 01:03:12 +02:00
|
|
|
FunctionLike\ReturnTypeAnalyzer::verifyReturnType(
|
|
|
|
$stmt,
|
2019-09-01 06:56:46 +02:00
|
|
|
$stmt->getStmts() ?: [],
|
2019-07-29 01:03:12 +02:00
|
|
|
$source,
|
2019-11-25 17:44:54 +01:00
|
|
|
$type_provider,
|
2019-07-29 01:03:12 +02:00
|
|
|
$method_analyzer,
|
|
|
|
$return_type,
|
|
|
|
$fq_classlike_name,
|
|
|
|
$return_type_location,
|
2020-03-29 05:37:42 +02:00
|
|
|
$overridden_method_ids,
|
|
|
|
$did_explicitly_return
|
2019-07-29 01:03:12 +02:00
|
|
|
);
|
2018-01-14 19:08:24 +01:00
|
|
|
}
|
2019-01-25 21:31:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function checkTemplateParams(
|
|
|
|
Codebase $codebase,
|
|
|
|
ClassLikeStorage $storage,
|
|
|
|
ClassLikeStorage $parent_storage,
|
2019-01-28 05:12:40 +01:00
|
|
|
CodeLocation $code_location,
|
|
|
|
int $expected_param_count
|
2019-01-25 21:31:17 +01:00
|
|
|
) {
|
|
|
|
$template_type_count = $parent_storage->template_types === null
|
|
|
|
? 0
|
|
|
|
: count($parent_storage->template_types);
|
|
|
|
|
2019-01-28 05:12:40 +01:00
|
|
|
if ($template_type_count > $expected_param_count) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingTemplateParam(
|
|
|
|
$storage->name . ' has missing template params, expecting '
|
|
|
|
. $template_type_count,
|
|
|
|
$code_location
|
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2019-01-28 05:12:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
2019-01-25 21:31:17 +01:00
|
|
|
}
|
2019-01-28 05:12:40 +01:00
|
|
|
} elseif ($template_type_count < $expected_param_count) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new TooManyTemplateParams(
|
|
|
|
$storage->name . ' has too many template params, expecting '
|
|
|
|
. $template_type_count,
|
|
|
|
$code_location
|
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2019-01-28 05:12:40 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 21:31:17 +01:00
|
|
|
|
2019-01-28 05:12:40 +01:00
|
|
|
if ($parent_storage->template_types && $storage->template_type_extends) {
|
2019-05-15 00:51:30 +02:00
|
|
|
$i = 0;
|
|
|
|
|
2020-04-17 07:16:15 +02:00
|
|
|
$previous_extended = [];
|
|
|
|
|
2019-03-22 20:59:10 +01:00
|
|
|
foreach ($parent_storage->template_types as $template_name => $type_map) {
|
2020-04-17 07:16:15 +02:00
|
|
|
foreach ($type_map as $declaring_class => $template_type) {
|
2019-06-25 05:31:06 +02:00
|
|
|
if (isset($storage->template_type_extends[$parent_storage->name][$template_name])) {
|
|
|
|
$extended_type = $storage->template_type_extends[$parent_storage->name][$template_name];
|
2019-01-25 21:31:17 +01:00
|
|
|
|
2019-05-15 00:51:30 +02:00
|
|
|
if (isset($parent_storage->template_covariants[$i])
|
|
|
|
&& !$parent_storage->template_covariants[$i]
|
|
|
|
) {
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($extended_type->getAtomicTypes() as $t) {
|
2019-05-15 00:51:30 +02:00
|
|
|
if ($t instanceof Type\Atomic\TTemplateParam
|
2019-05-15 01:30:10 +02:00
|
|
|
&& $storage->template_types
|
|
|
|
&& $storage->template_covariants
|
2019-05-15 00:51:30 +02:00
|
|
|
&& ($local_offset
|
|
|
|
= array_search($t->param_name, array_keys($storage->template_types)))
|
|
|
|
!== false
|
2019-11-30 15:32:21 +01:00
|
|
|
&& !empty($storage->template_covariants[$local_offset])
|
2019-05-15 00:51:30 +02:00
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidTemplateParam(
|
|
|
|
'Cannot extend an invariant template param ' . $template_name
|
2019-11-30 15:32:21 +01:00
|
|
|
. ' into a covariant context',
|
2019-05-15 00:51:30 +02:00
|
|
|
$code_location
|
|
|
|
),
|
2019-08-18 18:25:48 +02:00
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
2019-05-15 00:51:30 +02:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 07:16:15 +02:00
|
|
|
if (!$template_type[0]->isMixed()) {
|
|
|
|
$template_type_copy = clone $template_type[0];
|
|
|
|
|
|
|
|
$template_result = new \Psalm\Internal\Type\TemplateResult(
|
|
|
|
$previous_extended ?: [],
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
$template_type_copy = UnionTemplateHandler::replaceTemplateTypesWithStandins(
|
|
|
|
$template_type_copy,
|
|
|
|
$template_result,
|
|
|
|
$codebase,
|
|
|
|
null,
|
|
|
|
$extended_type,
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
2020-07-22 01:40:35 +02:00
|
|
|
if (!UnionTypeComparator::isContainedBy($codebase, $extended_type, $template_type_copy)) {
|
2020-04-17 07:16:15 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidTemplateParam(
|
|
|
|
'Extended template param ' . $template_name
|
2020-05-12 21:07:10 +02:00
|
|
|
. ' expects type ' . $template_type_copy->getId()
|
2020-04-17 07:16:15 +02:00
|
|
|
. ', type ' . $extended_type->getId() . ' given',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$storage->suppressed_issues + $this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$previous_extended[$template_name] = [
|
|
|
|
$declaring_class => [$extended_type]
|
|
|
|
];
|
2019-03-22 20:59:10 +01:00
|
|
|
}
|
2020-05-12 21:07:10 +02:00
|
|
|
} else {
|
|
|
|
$previous_extended[$template_name] = [
|
|
|
|
$declaring_class => [$extended_type]
|
|
|
|
];
|
2019-01-25 21:31:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 00:51:30 +02:00
|
|
|
|
|
|
|
$i++;
|
2019-01-25 21:31:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|