1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 18:36:58 +01:00
psalm/src/Psalm/Internal/Analyzer/ClassAnalyzer.php

1909 lines
74 KiB
PHP
Raw Normal View History

<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Analyzer;
use PhpParser;
2018-05-30 19:08:56 +02:00
use Psalm\Aliases;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\FunctionLike\ReturnTypeAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Call\MethodCallAnalyzer;
use Psalm\Internal\Analyzer\TypeAnalyzer;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Config;
use Psalm\Context;
use Psalm\Issue\DeprecatedClass;
use Psalm\Issue\DeprecatedInterface;
use Psalm\Issue\DeprecatedTrait;
use Psalm\Issue\InaccessibleMethod;
use Psalm\Issue\InternalClass;
use Psalm\Issue\InvalidTemplateParam;
use Psalm\Issue\MethodSignatureMismatch;
use Psalm\Issue\MissingConstructor;
use Psalm\Issue\MissingImmutableAnnotation;
use Psalm\Issue\MissingPropertyType;
use Psalm\Issue\MissingTemplateParam;
use Psalm\Issue\OverriddenPropertyAccess;
use Psalm\Issue\PropertyNotSetInConstructor;
use Psalm\Issue\ReservedWord;
use Psalm\Issue\TooManyTemplateParams;
use Psalm\Issue\UndefinedClass;
use Psalm\Issue\UndefinedInterface;
use Psalm\Issue\UndefinedTrait;
use Psalm\Issue\UnimplementedAbstractMethod;
use Psalm\Issue\UnimplementedInterfaceMethod;
use Psalm\IssueBuffer;
use Psalm\StatementsSource;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\FunctionLikeParameter;
use Psalm\Type;
use function preg_replace;
use function preg_match;
use function explode;
use function array_pop;
use function array_merge;
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;
/**
* @internal
*/
2018-11-06 03:57:36 +01:00
class ClassAnalyzer extends ClassLikeAnalyzer
{
2016-10-31 20:42:20 +01:00
/**
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
* @param PhpParser\Node\Stmt\Class_ $class
2018-11-06 03:57:36 +01:00
* @param SourceAnalyzer $source
* @param string|null $fq_class_name
2016-09-09 15:13:41 +02:00
*/
2018-11-06 03:57:36 +01:00
public function __construct(PhpParser\Node\Stmt\Class_ $class, SourceAnalyzer $source, $fq_class_name)
{
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
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
}
parent::__construct($class, $source, $fq_class_name);
if (!$this->class instanceof PhpParser\Node\Stmt\Class_) {
throw new \InvalidArgumentException('Bad');
}
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,
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
$this->source->getAliases()
2016-11-02 07:29:00 +01:00
);
}
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
}
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
/**
* @param PhpParser\Node\Stmt\Class_ $class
* @param string $file_path
*
* @return string
*/
public static function getAnonymousClassName(PhpParser\Node\Stmt\Class_ $class, $file_path)
{
return preg_replace('/[^A-Za-z0-9]/', '_', $file_path)
. '_' . $class->getLine() . '_' . (int)$class->getAttribute('startFilePos');
}
/**
* @param Context|null $class_context
* @param Context|null $global_context
*
* @return null|false
*/
public function analyze(
Context $class_context = null,
Context $global_context = null
) {
2018-04-13 17:28:15 +02:00
$class = $this->class;
if (!$class instanceof PhpParser\Node\Stmt\Class_) {
throw new \LogicException('Something went badly wrong');
}
$fq_class_name = $class_context && $class_context->self ? $class_context->self : $this->fq_class_name;
$storage = $this->storage;
if ($storage->has_visitor_issues) {
return;
}
if ($class->name
&& (preg_match(
'/(^|\\\)(int|float|bool|string|void|null|false|true|object|numeric|mixed)$/i',
$fq_class_name
) || strtolower($fq_class_name) === 'resource')
) {
$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,
$class->name,
null,
true
),
$class_name
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// 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();
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
);
}
$classlike_storage_provider = $codebase->classlike_storage_provider;
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) {
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);
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,
$parent_reference_location,
$storage->suppressed_issues + $this->getSuppressedIssues(),
false
) === false) {
return false;
}
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
);
}
try {
2018-04-13 17:28:15 +02:00
$parent_class_storage = $classlike_storage_provider->get($parent_fq_class_name);
$code_location = new CodeLocation(
$this,
$class->extends,
$class_context ? $class_context->include_location : null,
true
);
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'
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// fall through
}
}
if ($parent_class_storage->deprecated) {
if (IssueBuffer::accepts(
new DeprecatedClass(
2018-04-13 17:28:15 +02:00
$parent_fq_class_name . ' is marked deprecated',
$code_location,
$parent_fq_class_name
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// fall through
}
}
if ($parent_class_storage->internal) {
$code_location = new CodeLocation(
$this,
$class->extends,
$class_context ? $class_context->include_location : null,
true
);
if (! NamespaceAnalyzer::nameSpaceRootsMatch($fq_class_name, $parent_fq_class_name)) {
if (IssueBuffer::accepts(
new InternalClass(
$parent_fq_class_name . ' is marked internal',
$code_location,
$parent_fq_class_name
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// fall through
}
}
}
if ($parent_class_storage->psalm_internal &&
! NamespaceAnalyzer::isWithin($fq_class_name, $parent_class_storage->psalm_internal)
) {
if (IssueBuffer::accepts(
new InternalClass(
$parent_fq_class_name . ' is internal to ' . $parent_class_storage->psalm_internal,
$code_location,
$parent_fq_class_name
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// fall through
}
}
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
}
}
if ($codebase->store_node_types && $parent_fq_class_name) {
$codebase->analyzer->addNodeReference(
$this->getFilePath(),
$class->extends,
$codebase->classlikes->classExists($parent_fq_class_name)
? $parent_fq_class_name
: '*' . implode('\\', $class->extends->parts)
);
}
$code_location = new CodeLocation(
$this,
$class->name ?: $class,
$class_context ? $class_context->include_location : null,
true
);
if ($storage->template_type_extends_count !== null) {
$this->checkTemplateParams(
$codebase,
$storage,
$parent_class_storage,
$code_location,
$storage->template_type_extends_count
);
}
} catch (\InvalidArgumentException $e) {
// do nothing
}
}
2018-04-13 17:28:15 +02:00
foreach ($class->implements as $interface_name) {
$fq_interface_name = self::getFQCLNFromNameObject(
$interface_name,
$this->source->getAliases()
);
$codebase->analyzer->addNodeReference(
$this->getFilePath(),
$interface_name,
$codebase->classlikes->interfaceExists($fq_interface_name)
? $fq_interface_name
: '*' . implode('\\', $interface_name->parts)
);
$interface_location = new CodeLocation($this, $interface_name);
if (self::checkFullyQualifiedClassLikeName(
$this,
$fq_interface_name,
$interface_location,
$this->getSuppressedIssues(),
false
) === false) {
continue;
}
2019-02-24 07:33:25 +01:00
if ($codebase->store_node_types && $fq_class_name) {
$bounds = $interface_location->getSelectionBounds();
$codebase->analyzer->addOffsetReference(
$this->getFilePath(),
$bounds[0],
$bounds[1],
$fq_interface_name
);
}
2019-06-04 22:36:32 +02:00
$codebase->classlikes->handleClassLikeReferenceInMigration(
$codebase,
$this,
$interface_name,
$fq_interface_name,
null
);
try {
$interface_storage = $classlike_storage_provider->get($fq_interface_name);
} catch (\InvalidArgumentException $e) {
continue;
}
$code_location = new CodeLocation(
$this,
$interface_name,
$class_context ? $class_context->include_location : null,
true
);
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',
$code_location,
$fq_interface_name
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// fall through
}
}
if (isset($storage->template_type_implements_count[strtolower($fq_interface_name)])) {
$expected_param_count = $storage->template_type_implements_count[strtolower($fq_interface_name)];
$this->checkTemplateParams(
$codebase,
$storage,
$interface_storage,
$code_location,
$expected_param_count
);
}
}
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
}
}
}
}
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()
);
}
}
}
if ($storage->invalid_dependencies) {
return;
}
$class_interfaces = $storage->class_implements;
2018-04-13 17:28:15 +02:00
if (!$class->isAbstract()) {
foreach ($class_interfaces as $interface_name) {
try {
$interface_storage = $classlike_storage_provider->get($interface_name);
} catch (\InvalidArgumentException $e) {
continue;
}
$code_location = new CodeLocation(
$this,
$class->name ? $class->name : $class,
$class_context ? $class_context->include_location : null,
true
);
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
}
}
2019-09-23 02:42:31 +02: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
}
}
foreach ($interface_storage->methods as $method_name => $interface_method_storage) {
if ($interface_method_storage->visibility === self::VISIBILITY_PUBLIC) {
2018-02-04 00:52:35 +01:00
$implementer_declaring_method_id = $codebase->methods->getDeclaringMethodId(
$this->fq_class_name . '::' . $method_name
);
$implementer_method_storage = null;
$implementer_classlike_storage = null;
if ($implementer_declaring_method_id) {
list($implementer_fq_class_name) = explode('::', $implementer_declaring_method_id);
$implementer_method_storage = $codebase->methods->getStorage(
$implementer_declaring_method_id
);
$implementer_classlike_storage = $classlike_storage_provider->get(
$implementer_fq_class_name
);
}
if (!$implementer_method_storage) {
if (IssueBuffer::accepts(
new UnimplementedInterfaceMethod(
'Method ' . $method_name . ' is not defined on class ' .
$storage->name,
$code_location
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
return false;
}
return null;
}
$implementer_appearing_method_id = $codebase->methods->getAppearingMethodId(
$this->fq_class_name . '::' . $method_name
);
$implementer_visibility = $implementer_method_storage->visibility;
if ($implementer_appearing_method_id
&& $implementer_appearing_method_id !== $implementer_declaring_method_id
) {
list($appearing_fq_class_name, $appearing_method_name) = explode(
'::',
$implementer_appearing_method_id
);
$appearing_class_storage = $classlike_storage_provider->get(
$appearing_fq_class_name
);
if (isset($appearing_class_storage->trait_visibility_map[$appearing_method_name])) {
$implementer_visibility
= $appearing_class_storage->trait_visibility_map[$appearing_method_name];
}
}
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;
}
return null;
}
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-11-06 03:57:36 +01:00
MethodAnalyzer::compareMethods(
$codebase,
$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
);
}
}
}
}
if (!$class_context) {
$class_context = new Context($this->fq_class_name);
$class_context->collect_references = $codebase->collect_references;
2018-04-13 17:28:15 +02:00
$class_context->parent = $parent_fq_class_name;
}
if ($global_context) {
$class_context->strict_types = $global_context->strict_types;
}
if ($this->leftover_stmts) {
(new StatementsAnalyzer(
$this,
new \Psalm\Internal\Provider\NodeDataProvider()
))->analyze(
$this->leftover_stmts,
$class_context
);
}
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);
list($declaring_class_name, $method_name) = explode('::', $declaring_method_id);
if ($method_storage->abstract) {
if (IssueBuffer::accepts(
new UnimplementedAbstractMethod(
'Method ' . $method_name . ' is not defined on class ' .
$this->fq_class_name . ', defined abstract in ' . $declaring_class_name,
new CodeLocation(
$this,
$class->name ? $class->name : $class,
$class_context->include_location,
true
)
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
return false;
}
}
}
}
foreach ($storage->appearing_property_ids as $property_name => $appearing_property_id) {
2018-11-29 06:05:56 +01:00
$property_class_name = $codebase->properties->getDeclaringClassForProperty(
$appearing_property_id,
true
2018-11-29 06:05:56 +01:00
);
$property_class_storage = $classlike_storage_provider->get((string)$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) {
list($guide_class_name) = explode('::$', $overridden_property_id);
$guide_class_storage = $classlike_storage_provider->get($guide_class_name);
$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
)
)) {
return false;
}
return null;
}
}
}
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;
}
}
2018-04-13 17:28:15 +02:00
$property_type_location = $property_storage->type_location;
$fleshed_out_type = !$property_type->isMixed()
2018-11-06 03:57:36 +01:00
? ExpressionAnalyzer::fleshOutType(
$codebase,
$property_type,
$this->fq_class_name,
$this->fq_class_name,
$this->parent_fq_class_name
)
: $property_type;
/**
* @psalm-suppress ReferenceConstraintViolation
*/
$class_template_params = MethodCallAnalyzer::getClassTemplateParams(
$codebase,
$property_class_storage,
$fq_class_name,
null,
new Type\Atomic\TNamedObject($fq_class_name),
'$this'
);
$template_result = new \Psalm\Internal\Type\TemplateResult(
$class_template_params ?: [],
[]
);
if ($class_template_params) {
$fleshed_out_type = \Psalm\Internal\Type\UnionTemplateHandler::replaceTemplateTypesWithStandins(
$fleshed_out_type,
$template_result,
$codebase,
null,
null
);
}
if ($property_type_location && !$fleshed_out_type->isMixed()) {
$fleshed_out_type->check(
$this,
2018-04-13 17:28:15 +02:00
$property_type_location,
$storage->suppressed_issues + $this->getSuppressedIssues(),
[],
false
);
}
if ($property_storage->is_static) {
$property_id = $this->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) {
$fleshed_out_type = !$property_type->isMixed()
? ExpressionAnalyzer::fleshOutType(
$codebase,
$property_type,
$this->fq_class_name,
$this->fq_class_name,
$this->parent_fq_class_name
)
: $property_type;
$class_context->vars_in_scope['$this->' . substr($property_name, 1)] = $fleshed_out_type;
}
2018-11-11 18:01:14 +01:00
$constructor_analyzer = null;
$member_stmts = [];
2018-04-13 17:28:15 +02:00
foreach ($class->stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
2018-11-11 18:01:14 +01:00
$method_analyzer = $this->analyzeClassMethod(
$stmt,
$storage,
$this,
$class_context,
$global_context
);
if ($stmt->name->name === '__construct') {
2018-11-11 18:01:14 +01:00
$constructor_analyzer = $method_analyzer;
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
if ($this->analyzeTraitUse(
2018-05-30 19:08:56 +02:00
$this->source->getAliases(),
$stmt,
2018-11-11 18:01:14 +01:00
$project_analyzer,
$storage,
$class_context,
$global_context,
2018-11-11 18:01:14 +01:00
$constructor_analyzer
) === false) {
return false;
}
2018-01-29 03:43:26 +01:00
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
foreach ($stmt->props as $prop) {
if ($prop->default) {
$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
) {
$replace_type = ExpressionAnalyzer::fleshOutType(
$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
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
$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
);
}
}
}
}
}
$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
$config = Config::getInstance();
$this->checkPropertyInitialization(
$codebase,
$config,
$storage,
$class_context,
$global_context,
2018-11-11 18:01:14 +01:00
$constructor_analyzer
);
foreach ($class->stmts as $stmt) {
2019-01-11 23:28:57 +01:00
if ($stmt instanceof PhpParser\Node\Stmt\Property && !isset($stmt->type)) {
$this->checkForMissingPropertyType($this, $stmt, $class_context);
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
foreach ($stmt->traits as $trait) {
$fq_trait_name = self::getFQCLNFromNameObject(
$trait,
$this->source->getAliases()
);
try {
$trait_file_analyzer = $project_analyzer->getFileAnalyzerForClassLike($fq_trait_name);
} catch (\Exception $e) {
continue;
}
$trait_node = $codebase->classlikes->getTraitNode($fq_trait_name);
$trait_aliases = $codebase->classlikes->getTraitAliases($fq_trait_name);
2018-11-11 18:01:14 +01:00
$trait_analyzer = new TraitAnalyzer(
$trait_node,
2018-11-11 18:01:14 +01:00
$trait_file_analyzer,
$fq_trait_name,
$trait_aliases
);
if (isset($storage->template_type_uses_count[strtolower($fq_trait_name)])) {
$trait_storage = $codebase->classlike_storage_provider->get($fq_trait_name);
$expected_param_count = $storage->template_type_uses_count[strtolower($fq_trait_name)];
$this->checkTemplateParams(
$codebase,
$storage,
$trait_storage,
new CodeLocation(
$this,
$trait
),
$expected_param_count
);
}
foreach ($trait_node->stmts as $trait_stmt) {
if ($trait_stmt instanceof PhpParser\Node\Stmt\Property) {
$this->checkForMissingPropertyType($trait_analyzer, $trait_stmt, $class_context);
}
}
2019-08-02 07:55:24 +02:00
$trait_file_analyzer->clearSourceBeforeDestruction();
}
}
}
foreach ($storage->pseudo_methods as $pseudo_method_name => $pseudo_method_storage) {
$pseudo_method_id = $this->fq_class_name . '::' . $pseudo_method_name;
$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);
list($overridden_fq_class_name) = explode('::', $overridden_method_id);
$parent_storage = $classlike_storage_provider->get($overridden_fq_class_name);
MethodAnalyzer::compareMethods(
$codebase,
$storage,
$parent_storage,
$pseudo_method_storage,
$parent_method_storage,
$this->fq_class_name,
$pseudo_method_storage->visibility ?: 0,
$pseudo_method_storage->location,
$storage->suppressed_issues,
true,
false
);
}
}
}
$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,
$this,
$codebase,
$file_manipulations
) === false) {
return false;
}
}
if ($file_manipulations) {
\Psalm\Internal\FileManipulation\FileManipulationBuffer::add(
$this->getFilePath(),
$file_manipulations
);
}
}
}
/**
* @return void
*/
private function checkPropertyInitialization(
Codebase $codebase,
Config $config,
ClassLikeStorage $storage,
Context $class_context,
Context $global_context = null,
2018-11-11 18:01:14 +01:00
MethodAnalyzer $constructor_analyzer = null
) {
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;
$included_file_path = $this->getFilePath();
if ($class_context->include_location) {
$included_file_path = $class_context->include_location->file_path;
}
2018-11-02 02:52:39 +01:00
$method_already_analyzed = $codebase->analyzer->isMethodAlreadyAnalyzed(
$included_file_path,
strtolower($fq_class_name) . '::__construct',
true
);
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
return;
}
/** @var PhpParser\Node\Stmt\Class_ */
$class = $this->class;
$classlike_storage_provider = $codebase->classlike_storage_provider;
$constructor_appearing_fqcln = $fq_class_name;
$uninitialized_variables = [];
$uninitialized_properties = [];
$uninitialized_typed_properties = [];
foreach ($storage->appearing_property_ids as $property_name => $appearing_property_id) {
$property_class_name = (string) $codebase->properties->getDeclaringClassForProperty(
$appearing_property_id,
true
2018-11-29 06:05:56 +01:00
);
$property_class_storage = $classlike_storage_provider->get($property_class_name);
$property = $property_class_storage->properties[$property_name];
$property_is_initialized = isset($property_class_storage->initialized_properties[$property_name]);
if ($property->is_static) {
continue;
}
if ($property->has_default || $property_is_initialized) {
continue;
}
if ($property->type && $property->type->isNullable() && $property->type->from_docblock) {
continue;
}
if ($codebase->diff_methods && $method_already_analyzed && $property->location) {
list($start, $end) = $property->location->getSelectionBounds();
$existing_issues = $codebase->analyzer->getExistingIssuesForFile(
$this->getFilePath(),
$start,
$end,
'PropertyNotSetInConstructor'
);
if ($existing_issues) {
IssueBuffer::addIssues($existing_issues);
continue;
}
}
$codebase->file_reference_provider->addMethodReferenceToMissingClassMember(
strtolower($fq_class_name) . '::__construct',
strtolower($property_class_name) . '::$' . $property_name
);
$uninitialized_variables[] = '$this->' . $property_name;
$uninitialized_properties[$property_class_name . '::$' . $property_name] = $property;
if ($property->type && !$property->type->isMixed()) {
$uninitialized_typed_properties[$property_class_name . '::$' . $property_name] = $property;
}
}
if (!$uninitialized_properties) {
return;
}
if (!$storage->abstract
2018-11-11 18:01:14 +01:00
&& !$constructor_analyzer
&& isset($storage->declaring_method_ids['__construct'])
&& isset($storage->appearing_method_ids['__construct'])
&& $class->extends
) {
list($constructor_declaring_fqcln) = explode('::', $storage->declaring_method_ids['__construct']);
list($constructor_appearing_fqcln) = explode('::', $storage->appearing_method_ids['__construct']);
$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
&& !$constructor_class_storage->stubbed
&& isset($constructor_class_storage->methods['__construct'])
) {
$constructor_storage = $constructor_class_storage->methods['__construct'];
$fake_constructor_params = array_map(
function (FunctionLikeParameter $param) : PhpParser\Node\Param {
$fake_param = (new PhpParser\Builder\Param($param->name));
if ($param->signature_type) {
/** @psalm-suppress DeprecatedMethod */
$fake_param->setTypeHint((string)$param->signature_type);
}
return $fake_param->getNode();
},
$constructor_storage->params
);
$fake_constructor_stmt_args = array_map(
function (FunctionLikeParameter $param) : PhpParser\Node\Arg {
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(
new PhpParser\Node\Name\FullyQualified($constructor_declaring_fqcln),
new PhpParser\Node\Identifier('__construct'),
$fake_constructor_stmt_args,
[
'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')
)],
]
),
[
'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')
)],
]
),
];
$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,
],
[
'startLine' => $class->extends->getLine(),
'startFilePos' => $class->extends->getAttribute('startFilePos'),
'endFilePos' => $class->extends->getAttribute('endFilePos'),
]
);
$codebase->analyzer->disableMixedCounts();
2018-11-11 18:01:14 +01:00
$constructor_analyzer = $this->analyzeClassMethod(
$fake_stmt,
$storage,
$this,
$class_context,
$global_context,
true
);
$codebase->analyzer->enableMixedCounts();
}
}
2018-11-11 18:01:14 +01:00
if ($constructor_analyzer) {
$method_context = clone $class_context;
$method_context->collect_initializations = true;
$method_context->self = $fq_class_name;
$method_context->vars_in_scope['$this'] = Type::parseString($fq_class_name);
$method_context->vars_possibly_in_scope['$this'] = true;
$constructor_analyzer->analyze(
$method_context,
new \Psalm\Internal\Provider\NodeDataProvider(),
$global_context,
true
);
foreach ($uninitialized_typed_properties as $property_id => $property_storage) {
list(,$property_name) = explode('::$', $property_id);
if (!isset($method_context->vars_in_scope['$this->' . $property_name])) {
$end_type = Type::getVoid();
$end_type->initialized = false;
} else {
$end_type = $method_context->vars_in_scope['$this->' . $property_name];
}
$constructor_class_property_storage = $property_storage;
$error_location = $property_storage->location;
if ($storage->declaring_property_ids[$property_name] !== $fq_class_name) {
$error_location = $storage->location ?: $storage->stmt_location;
}
if ($fq_class_name !== $constructor_appearing_fqcln
&& $property_storage->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE
) {
$a_class_storage = $classlike_storage_provider->get(
$end_type->initialized_class ?: $constructor_appearing_fqcln
);
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];
}
}
if ($property_storage->location
&& $error_location
&& (!$end_type->initialized || $property_storage !== $constructor_class_property_storage)
) {
if (IssueBuffer::accepts(
new PropertyNotSetInConstructor(
'Property ' . $property_id . ' is not defined in constructor of ' .
$this->fq_class_name . ' and in any methods called in the constructor',
$error_location,
$property_id
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
continue;
}
}
}
2018-11-02 02:52:39 +01:00
$codebase->analyzer->setAnalyzedMethod(
$included_file_path,
strtolower($fq_class_name) . '::__construct',
true
);
return;
}
if (!$storage->abstract && $uninitialized_typed_properties) {
$first_uninitialized_property = array_shift($uninitialized_typed_properties);
if ($first_uninitialized_property->location) {
if (IssueBuffer::accepts(
new MissingConstructor(
$fq_class_name . ' has an uninitialized variable ' . $uninitialized_variables[0] .
', but no constructor',
$first_uninitialized_property->location
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// fall through
}
}
}
}
/**
* @return false|null
*/
private function analyzeTraitUse(
2018-05-30 19:08:56 +02:00
Aliases $aliases,
PhpParser\Node\Stmt\TraitUse $stmt,
2018-11-11 18:01:14 +01:00
ProjectAnalyzer $project_analyzer,
ClassLikeStorage $storage,
Context $class_context,
Context $global_context = null,
2018-11-11 18:01:14 +01:00
MethodAnalyzer &$constructor_analyzer = null
) {
2018-11-06 03:57:36 +01:00
$codebase = $this->getCodebase();
$previous_context_include_location = $class_context->include_location;
foreach ($stmt->traits as $trait_name) {
$trait_location = new CodeLocation($this, $trait_name, null, true);
$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
);
if (!$codebase->classlikes->hasFullyQualifiedTraitName($fq_trait_name, $trait_location)) {
if (IssueBuffer::accepts(
new UndefinedTrait(
'Trait ' . $fq_trait_name . ' does not exist',
new CodeLocation($this, $trait_name)
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
return false;
}
} else {
if (!$codebase->traitHasCorrectCase($fq_trait_name)) {
if (IssueBuffer::accepts(
new UndefinedTrait(
'Trait ' . $fq_trait_name . ' has wrong casing',
new CodeLocation($this, $trait_name)
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
return false;
}
continue;
}
$trait_storage = $codebase->classlike_storage_provider->get($fq_trait_name);
if ($trait_storage->deprecated) {
if (IssueBuffer::accepts(
new DeprecatedTrait(
'Trait ' . $fq_trait_name . ' is deprecated',
new CodeLocation($this, $trait_name)
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// fall through
}
}
2018-11-11 18:01:14 +01:00
$trait_file_analyzer = $project_analyzer->getFileAnalyzerForClassLike($fq_trait_name);
$trait_node = $codebase->classlikes->getTraitNode($fq_trait_name);
$trait_aliases = $codebase->classlikes->getTraitAliases($fq_trait_name);
2018-11-11 18:01:14 +01:00
$trait_analyzer = new TraitAnalyzer(
$trait_node,
2018-11-11 18:01:14 +01:00
$trait_file_analyzer,
$fq_trait_name,
$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(
$trait_stmt,
$storage,
2018-11-11 18:01:14 +01:00
$trait_analyzer,
$class_context,
$global_context
);
if ($trait_stmt->name->name === '__construct') {
2018-11-11 18:01:14 +01:00
$constructor_analyzer = $trait_method_analyzer;
}
} elseif ($trait_stmt instanceof PhpParser\Node\Stmt\TraitUse) {
if ($this->analyzeTraitUse(
2018-05-30 19:08:56 +02:00
$trait_aliases,
$trait_stmt,
2018-11-11 18:01:14 +01:00
$project_analyzer,
$storage,
$class_context,
$global_context,
2018-11-11 18:01:14 +01:00
$constructor_analyzer
) === false) {
return false;
}
}
}
2019-08-02 07:55:24 +02:00
$trait_file_analyzer->clearSourceBeforeDestruction();
}
}
$class_context->include_location = $previous_context_include_location;
}
/**
* @param PhpParser\Node\Stmt\Property $stmt
*
* @return void
*/
private function checkForMissingPropertyType(
StatementsSource $source,
PhpParser\Node\Stmt\Property $stmt,
Context $context
) {
$comment = $stmt->getDocComment();
if (!$comment || !$comment->getText()) {
$fq_class_name = $source->getFQCLN();
$property_name = $stmt->props[0]->name->name;
2018-11-06 03:57:36 +01:00
$codebase = $this->getCodebase();
$property_id = $fq_class_name . '::$' . $property_name;
$declaring_property_class = $codebase->properties->getDeclaringClassForProperty(
$property_id,
true
);
if (!$declaring_property_class) {
throw new \UnexpectedValueException(
'Cannot get declaring class for ' . $property_id
);
}
$fq_class_name = $declaring_property_class;
// gets inherited property type
$class_property_type = $codebase->properties->getPropertyType($property_id, false, $source, $context);
if ($class_property_type) {
return;
}
$message = 'Property ' . $property_id . ' does not have a declared type';
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
$property_storage = $class_storage->properties[$property_name];
if ($property_storage->suggested_type && !$property_storage->suggested_type->isNull()) {
$message .= ' - consider ' . str_replace(
['<mixed, mixed>', '<empty, empty>'],
'',
(string)$property_storage->suggested_type
);
}
if (IssueBuffer::accepts(
new MissingPropertyType(
$message,
new CodeLocation($source, $stmt->props[0]->name)
),
$this->source->getSuppressedIssues()
)) {
// fall through
}
}
}
/**
* @param PhpParser\Node\Stmt\ClassMethod $stmt
2018-11-06 03:57:36 +01:00
* @param SourceAnalyzer $source
* @param Context $class_context
* @param Context|null $global_context
* @param bool $is_fake
*
2018-11-06 03:57:36 +01:00
* @return MethodAnalyzer|null
*/
private function analyzeClassMethod(
PhpParser\Node\Stmt\ClassMethod $stmt,
ClassLikeStorage $class_storage,
2018-11-06 03:57:36 +01:00
SourceAnalyzer $source,
Context $class_context,
Context $global_context = null,
$is_fake = false
) {
$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;
}
2018-11-11 18:01:14 +01:00
$method_analyzer = new MethodAnalyzer($stmt, $source);
2018-11-11 18:01:14 +01:00
$actual_method_id = (string)$method_analyzer->getMethodId();
2018-11-06 03:57:36 +01:00
$project_analyzer = $source->getProjectAnalyzer();
$codebase = $source->getCodebase();
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();
if ($class_context->include_location) {
$included_file_path = $class_context->include_location->file_path;
}
if ($class_context->self && $class_context->self !== $source->getFQCLN()) {
2018-11-11 18:01:14 +01:00
$analyzed_method_id = (string)$method_analyzer->getMethodId($class_context->self);
2018-02-04 00:52:35 +01:00
$declaring_method_id = $codebase->methods->getDeclaringMethodId($analyzed_method_id);
if ($actual_method_id !== $declaring_method_id) {
// the method is an abstract trait method
$declaring_method_storage = $method_analyzer->getFunctionLikeStorage();
if (!$declaring_method_storage instanceof \Psalm\Storage\MethodStorage) {
2018-03-17 05:19:55 +01:00
throw new \LogicException('This should never happen');
}
if ($declaring_method_id && $declaring_method_storage->abstract) {
$implementer_method_storage = $codebase->methods->getStorage($declaring_method_id);
$declaring_storage = $codebase->classlike_storage_provider->get(
explode('::', $actual_method_id)[0]
);
2018-11-06 03:57:36 +01:00
MethodAnalyzer::compareMethods(
$codebase,
$class_storage,
$declaring_storage,
$implementer_method_storage,
$declaring_method_storage,
$this->fq_class_name,
$implementer_method_storage->visibility,
new CodeLocation($source, $stmt),
$implementer_method_storage->suppressed_issues,
false
);
}
return;
}
}
2018-10-04 00:16:33 +02:00
$trait_safe_method_id = strtolower($analyzed_method_id);
if (strtolower($actual_method_id) !== $trait_safe_method_id) {
$trait_safe_method_id .= '&' . strtolower($actual_method_id);
}
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
);
$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
&& !$class_context->collect_initializations
&& !$class_context->collect_mutations
&& !$is_fake
) {
$project_analyzer->progress->debug(
'Skipping analysis of pre-analyzed method ' . $analyzed_method_id . "\n"
);
$existing_issues = $codebase->analyzer->getExistingIssuesForFile(
$source->getFilePath(),
$start,
$end
);
IssueBuffer::addIssues($existing_issues);
2018-11-11 18:01:14 +01:00
return $method_analyzer;
}
$codebase->analyzer->removeExistingDataForFile(
$source->getFilePath(),
$start,
$end
);
$method_context = clone $class_context;
foreach ($method_context->vars_in_scope as $context_var_id => $context_type) {
$method_context->vars_in_scope[$context_var_id] = clone $context_type;
}
$method_context->collect_exceptions = $config->check_for_throws_docblock;
$type_provider = new \Psalm\Internal\Provider\NodeDataProvider();
2018-11-11 18:01:14 +01:00
$method_analyzer->analyze(
$method_context,
$type_provider,
$global_context ? clone $global_context : null
);
if ($stmt->name->name !== '__construct'
&& $config->reportIssueInFile('InvalidReturnType', $source->getFilePath())
2018-09-04 20:37:33 +02:00
&& $class_context->self
) {
self::analyzeClassMethodReturnType(
$stmt,
$method_analyzer,
$source,
$type_provider,
$codebase,
$class_storage,
$class_context->self,
$analyzed_method_id,
$actual_method_id
);
}
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
return $method_analyzer;
}
public static function analyzeClassMethodReturnType(
PhpParser\Node\Stmt\ClassMethod $stmt,
MethodAnalyzer $method_analyzer,
SourceAnalyzer $source,
\Psalm\Internal\Provider\NodeDataProvider $type_provider,
Codebase $codebase,
ClassLikeStorage $class_storage,
string $fq_classlike_name,
string $analyzed_method_id,
string $actual_method_id
) : void {
$return_type_location = null;
$secondary_return_type_location = null;
$actual_method_storage = $codebase->methods->getStorage($actual_method_id);
if ($actual_method_id) {
$return_type_location = $codebase->methods->getMethodReturnTypeLocation(
$actual_method_id,
$secondary_return_type_location
);
}
$original_fq_classlike_name = $fq_classlike_name;
$return_type = $codebase->methods->getMethodReturnType($analyzed_method_id, $fq_classlike_name);
2019-06-25 19:20:30 +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
if ($declaring_method_id) {
$declaring_class_name = explode('::', $declaring_method_id)[0];
$class_storage = $codebase->classlike_storage_provider->get($declaring_class_name);
}
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],
$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);
}
$class_template_params = Statements\Expression\Call\MethodCallAnalyzer::getClassTemplateParams(
$codebase,
$class_storage,
$original_fq_classlike_name,
strtolower($stmt->name->name),
$this_object_type
) ?: [];
$template_result = new \Psalm\Internal\Type\TemplateResult(
$class_template_params ?: [],
[]
);
$return_type = \Psalm\Internal\Type\UnionTemplateHandler::replaceTemplateTypesWithStandins(
$return_type,
$template_result,
$codebase,
null,
null
);
}
$overridden_method_ids = isset($class_storage->overridden_method_ids[strtolower($stmt->name->name)])
? $class_storage->overridden_method_ids[strtolower($stmt->name->name)]
: [];
if ($actual_method_storage->overridden_downstream) {
$overridden_method_ids['overridden::downstream'] = 'overridden::downstream';
}
if (!$return_type
&& !$class_storage->is_interface
&& isset($class_storage->interface_method_ids[strtolower($stmt->name->name)])
) {
$interface_method_ids = $class_storage->interface_method_ids[strtolower($stmt->name->name)];
foreach ($interface_method_ids as $interface_method_id) {
list($interface_class) = explode('::', $interface_method_id);
$interface_return_type = $codebase->methods->getMethodReturnType(
$interface_method_id,
$interface_class
);
$interface_return_type_location = $codebase->methods->getMethodReturnTypeLocation(
$interface_method_id
);
FunctionLike\ReturnTypeAnalyzer::verifyReturnType(
$stmt,
$source,
$type_provider,
$method_analyzer,
$interface_return_type,
$interface_class,
$interface_return_type_location,
[$analyzed_method_id]
);
}
}
FunctionLike\ReturnTypeAnalyzer::verifyReturnType(
$stmt,
$source,
$type_provider,
$method_analyzer,
$return_type,
$fq_classlike_name,
$return_type_location,
$overridden_method_ids
);
}
/**
* @return void
*/
private function checkTemplateParams(
Codebase $codebase,
ClassLikeStorage $storage,
ClassLikeStorage $parent_storage,
CodeLocation $code_location,
int $expected_param_count
) {
$template_type_count = $parent_storage->template_types === null
? 0
: count($parent_storage->template_types);
if ($template_type_count > $expected_param_count) {
if (IssueBuffer::accepts(
new MissingTemplateParam(
$storage->name . ' has missing template params, expecting '
. $template_type_count,
$code_location
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// fall through
}
} 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
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// fall through
}
}
if ($parent_storage->template_types && $storage->template_type_extends) {
$i = 0;
foreach ($parent_storage->template_types as $template_name => $type_map) {
foreach ($type_map as $template_type) {
if (isset($storage->template_type_extends[$parent_storage->name][$template_name])) {
$extended_type = $storage->template_type_extends[$parent_storage->name][$template_name];
if (isset($parent_storage->template_covariants[$i])
&& !$parent_storage->template_covariants[$i]
&& $parent_storage->user_defined
) {
foreach ($extended_type->getTypes() as $t) {
if ($t instanceof Type\Atomic\TTemplateParam
2019-05-15 01:30:10 +02:00
&& $storage->template_types
&& $storage->template_covariants
&& ($local_offset
= array_search($t->param_name, array_keys($storage->template_types)))
!== false
2019-07-11 17:07:39 +02:00
&& isset($storage->template_covariants[$local_offset])
&& $storage->template_covariants[$local_offset]
) {
if (IssueBuffer::accepts(
new InvalidTemplateParam(
'Cannot extend an invariant template param ' . $template_name
. ' from an invariant context',
$code_location
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// fall through
}
}
}
}
if (!$template_type[0]->isMixed()
&& !TypeAnalyzer::isContainedBy($codebase, $extended_type, $template_type[0])
) {
if (IssueBuffer::accepts(
new InvalidTemplateParam(
'Extended template param ' . $template_name
. ' expects type ' . $template_type[0]->getId()
. ', type ' . $extended_type->getId() . ' given',
$code_location
),
$storage->suppressed_issues + $this->getSuppressedIssues()
)) {
// fall through
}
}
}
}
$i++;
}
}
}
}