1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 10:57:08 +01:00
psalm/src/Psalm/Internal/Analyzer/MethodAnalyzer.php

845 lines
31 KiB
PHP
Raw Normal View History

2016-01-08 00:28:27 +01:00
<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Analyzer;
2016-01-08 00:28:27 +01:00
2016-11-02 07:29:00 +01:00
use PhpParser;
2018-11-06 03:57:36 +01:00
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
2016-07-26 00:37:44 +02:00
use Psalm\Issue\DeprecatedMethod;
2018-02-22 00:59:31 +01:00
use Psalm\Issue\ImplementedReturnTypeMismatch;
2016-11-02 07:29:00 +01:00
use Psalm\Issue\InaccessibleMethod;
use Psalm\Issue\InternalMethod;
2016-07-26 00:37:44 +02:00
use Psalm\Issue\InvalidStaticInvocation;
2018-02-22 00:59:31 +01:00
use Psalm\Issue\MethodSignatureMismatch;
use Psalm\Issue\MethodSignatureMustOmitReturnType;
2018-02-22 00:59:31 +01:00
use Psalm\Issue\MoreSpecificImplementedParamType;
use Psalm\Issue\LessSpecificImplementedReturnType;
use Psalm\Issue\NonStaticSelfCall;
2018-02-22 00:59:31 +01:00
use Psalm\Issue\OverriddenMethodAccess;
2016-11-02 07:29:00 +01:00
use Psalm\Issue\UndefinedMethod;
use Psalm\IssueBuffer;
use Psalm\StatementsSource;
2018-02-22 00:59:31 +01:00
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\MethodStorage;
use Psalm\Type;
2016-01-08 00:28:27 +01:00
/**
* @internal
*/
2018-11-06 03:57:36 +01:00
class MethodAnalyzer extends FunctionLikeAnalyzer
2016-01-08 00:28:27 +01:00
{
2016-10-30 17:46:18 +01:00
/**
2016-12-17 06:48:31 +01:00
* @psalm-suppress MixedAssignment
2016-10-30 17:46:18 +01:00
*/
public function __construct(
PhpParser\Node\Stmt\ClassMethod $function,
SourceAnalyzer $source,
MethodStorage $storage = null
) {
$codebase = $source->getCodebase();
$real_method_id = $source->getFQCLN() . '::' . strtolower((string) $function->name);
if (!$storage) {
try {
$storage = $codebase->methods->getStorage($real_method_id);
} catch (\UnexpectedValueException $e) {
$class_storage = $codebase->classlike_storage_provider->get((string) $source->getFQCLN());
if (!$class_storage->parent_classes) {
throw $e;
}
$declaring_method_id = $codebase->methods->getDeclaringMethodId($real_method_id);
if (!$declaring_method_id) {
throw $e;
}
// happens for fake constructors
$storage = $codebase->methods->getStorage($declaring_method_id);
}
2016-10-15 06:12:57 +02:00
}
parent::__construct($function, $source, $storage);
}
/**
* Determines whether a given method is static or not
2016-11-02 07:29:00 +01:00
*
* @param string $method_id
* @param bool $self_call
* @param bool $is_context_dynamic
* @param CodeLocation $code_location
* @param array<string> $suppressed_issues
* @param bool $is_dynamic_this_method
2017-05-27 02:16:18 +02:00
*
2016-11-02 07:29:00 +01:00
* @return bool
*/
2017-01-12 03:37:53 +01:00
public static function checkStatic(
$method_id,
$self_call,
$is_context_dynamic,
2018-11-06 03:57:36 +01:00
Codebase $codebase,
CodeLocation $code_location,
array $suppressed_issues,
&$is_dynamic_this_method = false
) {
2018-11-06 03:57:36 +01:00
$codebase_methods = $codebase->methods;
2018-02-04 00:52:35 +01:00
2019-01-22 00:01:15 +01:00
if ($method_id === 'Closure::fromcallable') {
return true;
}
$original_method_id = $method_id;
2018-02-04 00:52:35 +01:00
$method_id = $codebase_methods->getDeclaringMethodId($method_id);
2018-03-17 05:19:55 +01:00
if (!$method_id) {
2019-01-22 00:01:15 +01:00
throw new \LogicException('Declaring method for ' . $original_method_id . ' should not be null');
2018-03-17 05:19:55 +01:00
}
2018-02-04 00:52:35 +01:00
$storage = $codebase_methods->getStorage($method_id);
if (!$storage->is_static) {
if ($self_call) {
if (!$is_context_dynamic) {
if (IssueBuffer::accepts(
new NonStaticSelfCall(
'Method ' . $codebase_methods->getCasedMethodId($method_id) .
' is not static, but is called ' .
'using self::',
$code_location
),
$suppressed_issues
)) {
return false;
}
} else {
$is_dynamic_this_method = true;
}
} else {
if (IssueBuffer::accepts(
new InvalidStaticInvocation(
2018-02-04 00:52:35 +01:00
'Method ' . $codebase_methods->getCasedMethodId($method_id) .
' is not static, but is called ' .
'statically',
$code_location
),
$suppressed_issues
)) {
return false;
}
}
}
2016-11-02 07:29:00 +01:00
return true;
}
2016-04-27 00:42:48 +02:00
/**
* @param string $method_id
* @param CodeLocation $code_location
* @param array $suppressed_issues
* @param string|null $calling_method_id
2017-05-27 02:16:18 +02:00
*
* @return bool|null
2016-04-27 00:42:48 +02:00
*/
public static function checkMethodExists(
2018-11-06 03:57:36 +01:00
Codebase $codebase,
$method_id,
CodeLocation $code_location,
array $suppressed_issues,
$calling_method_id = null
) {
2018-11-06 03:57:36 +01:00
if ($codebase->methods->methodExists(
$method_id,
$calling_method_id,
$calling_method_id !== $method_id ? $code_location : null
)) {
2016-10-23 18:24:53 +02:00
return true;
}
if (IssueBuffer::accepts(
new UndefinedMethod('Method ' . $method_id . ' does not exist', $code_location, $method_id),
2016-11-02 07:29:00 +01:00
$suppressed_issues
2016-10-23 18:24:53 +02:00
)) {
return false;
}
2016-11-02 07:29:00 +01:00
return null;
2016-10-23 18:24:53 +02:00
}
2016-10-15 06:12:57 +02:00
/**
* @param string $method_id
* @param CodeLocation $code_location
* @param array $suppressed_issues
2017-05-27 02:16:18 +02:00
*
2016-10-15 06:12:57 +02:00
* @return false|null
*/
public static function checkMethodNotDeprecatedOrInternal(
2018-11-06 03:57:36 +01:00
Codebase $codebase,
Context $context,
$method_id,
CodeLocation $code_location,
array $suppressed_issues
) {
2018-11-06 03:57:36 +01:00
$codebase_methods = $codebase->methods;
2018-02-04 00:52:35 +01:00
$method_id = (string) $codebase_methods->getDeclaringMethodId($method_id);
$storage = $codebase_methods->getStorage($method_id);
if ($storage->deprecated) {
if (IssueBuffer::accepts(
2016-11-02 07:29:00 +01:00
new DeprecatedMethod(
2018-02-04 00:52:35 +01:00
'The method ' . $codebase_methods->getCasedMethodId($method_id) .
' has been marked as deprecated',
$code_location,
$method_id
2016-11-02 07:29:00 +01:00
),
$suppressed_issues
)) {
// continue
}
}
2016-11-02 07:29:00 +01:00
if ($storage->internal
&& $context->self
&& !$context->collect_initializations
&& !$context->collect_mutations
) {
$declaring_class = explode('::', $method_id)[0];
$self_root = preg_replace('/^([^\\\]+).*/', '$1', $context->self);
$declaring_root = preg_replace('/^([^\\\]+).*/', '$1', $declaring_class);
if (strtolower($self_root) !== strtolower($declaring_root)) {
if (IssueBuffer::accepts(
new InternalMethod(
'The method ' . $codebase_methods->getCasedMethodId($method_id) .
' has been marked as internal',
$code_location,
$method_id
),
$suppressed_issues
)) {
// fall through
}
}
}
2016-11-02 07:29:00 +01:00
return null;
}
/**
* @param string $method_id
* @param string|null $calling_context
* @param StatementsSource $source
* @param CodeLocation $code_location
* @param array $suppressed_issues
2017-05-27 02:16:18 +02:00
*
* @return false|null
2016-04-27 00:42:48 +02:00
*/
2016-11-02 07:29:00 +01:00
public static function checkMethodVisibility(
$method_id,
$calling_context,
StatementsSource $source,
CodeLocation $code_location,
2016-11-02 07:29:00 +01:00
array $suppressed_issues
) {
2018-11-06 03:57:36 +01:00
$codebase = $source->getCodebase();
2018-02-04 00:52:35 +01:00
$codebase_methods = $codebase->methods;
$codebase_classlikes = $codebase->classlikes;
2018-02-04 00:52:35 +01:00
$declaring_method_id = $codebase_methods->getDeclaringMethodId($method_id);
2016-04-18 19:31:59 +02:00
2017-07-09 20:50:57 +02:00
if (!$declaring_method_id) {
$method_name = explode('::', $method_id)[1];
if ($method_name === '__construct'
|| $method_id === 'Closure::__invoke'
|| $method_id === 'Closure::fromcallable'
) {
return null;
}
throw new \UnexpectedValueException('$declaring_method_id not expected to be null here');
}
2018-02-04 00:52:35 +01:00
$appearing_method_id = $codebase_methods->getAppearingMethodId($method_id);
2017-07-09 20:50:57 +02:00
$appearing_method_class = null;
$appearing_class_storage = null;
$appearing_method_name = null;
2017-07-09 20:50:57 +02:00
if ($appearing_method_id) {
list($appearing_method_class, $appearing_method_name) = explode('::', $appearing_method_id);
2017-07-09 20:50:57 +02:00
// if the calling class is the same, we know the method exists, so it must be visible
if ($appearing_method_class === $calling_context) {
return null;
}
$appearing_class_storage = $codebase->classlike_storage_provider->get($appearing_method_class);
2016-12-12 19:50:46 +01:00
}
2017-07-09 20:50:57 +02:00
list($declaring_method_class) = explode('::', $declaring_method_id);
2018-11-06 03:57:36 +01:00
if ($source->getSource() instanceof TraitAnalyzer && $declaring_method_class === $source->getFQCLN()) {
2016-11-02 07:29:00 +01:00
return null;
2016-08-13 17:10:43 +02:00
}
2018-11-06 03:57:36 +01:00
$storage = $codebase->methods->getStorage($declaring_method_id);
$visibility = $storage->visibility;
if ($appearing_method_name
&& isset($appearing_class_storage->trait_visibility_map[$appearing_method_name])
) {
$visibility = $appearing_class_storage->trait_visibility_map[$appearing_method_name];
}
switch ($visibility) {
2018-11-06 03:57:36 +01:00
case ClassLikeAnalyzer::VISIBILITY_PUBLIC:
2016-11-02 07:29:00 +01:00
return null;
2016-04-18 19:31:59 +02:00
2018-11-06 03:57:36 +01:00
case ClassLikeAnalyzer::VISIBILITY_PRIVATE:
if (!$calling_context || $appearing_method_class !== $calling_context) {
2016-06-26 21:18:40 +02:00
if (IssueBuffer::accepts(
new InaccessibleMethod(
2018-02-04 00:52:35 +01:00
'Cannot access private method ' . $codebase_methods->getCasedMethodId($method_id) .
2016-11-02 07:29:00 +01:00
' from context ' . $calling_context,
$code_location
),
2016-11-02 07:29:00 +01:00
$suppressed_issues
)) {
return false;
}
2016-04-18 19:31:59 +02:00
}
2016-11-02 07:29:00 +01:00
return null;
2016-04-18 19:31:59 +02:00
2018-11-06 03:57:36 +01:00
case ClassLikeAnalyzer::VISIBILITY_PROTECTED:
2016-04-18 19:31:59 +02:00
if (!$calling_context) {
2016-06-26 21:18:40 +02:00
if (IssueBuffer::accepts(
2016-11-02 07:29:00 +01:00
new InaccessibleMethod(
'Cannot access protected method ' . $method_id,
$code_location
2016-11-02 07:29:00 +01:00
),
$suppressed_issues
)) {
return false;
}
2016-11-01 05:39:41 +01:00
2016-11-02 07:29:00 +01:00
return null;
2016-04-18 19:31:59 +02:00
}
if ($appearing_method_class
2018-02-04 00:52:35 +01:00
&& $codebase_classlikes->classExtends($appearing_method_class, $calling_context)
) {
2016-11-02 07:29:00 +01:00
return null;
2016-04-30 20:14:22 +02:00
}
if ($appearing_method_class
2018-02-04 00:52:35 +01:00
&& !$codebase_classlikes->classExtends($calling_context, $appearing_method_class)
) {
2016-06-26 21:18:40 +02:00
if (IssueBuffer::accepts(
new InaccessibleMethod(
2018-02-04 00:52:35 +01:00
'Cannot access protected method ' . $codebase_methods->getCasedMethodId($method_id) .
2016-11-02 07:29:00 +01:00
' from context ' . $calling_context,
$code_location
),
2016-11-02 07:29:00 +01:00
$suppressed_issues
)) {
return false;
}
2016-04-18 19:31:59 +02:00
}
}
2016-11-02 07:29:00 +01:00
return null;
}
2016-10-15 06:12:57 +02:00
/**
2018-02-04 00:52:35 +01:00
* @param string $method_id
* @param string|null $calling_context
* @param StatementsSource $source
2017-05-27 02:16:18 +02:00
*
2018-02-04 00:52:35 +01:00
* @return bool
2016-12-31 17:49:04 +01:00
*/
2018-02-04 00:52:35 +01:00
public static function isMethodVisible(
$method_id,
2018-02-04 00:52:35 +01:00
$calling_context,
StatementsSource $source
) {
2018-11-06 03:57:36 +01:00
$codebase = $source->getCodebase();
2017-01-12 03:37:53 +01:00
2018-02-04 00:52:35 +01:00
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
2016-11-07 05:29:54 +01:00
2018-02-04 00:52:35 +01:00
if (!$declaring_method_id) {
// this can happen for methods in the callmap that were not reflected
return true;
}
2018-02-04 00:52:35 +01:00
$appearing_method_id = $codebase->methods->getAppearingMethodId($method_id);
2016-12-31 17:49:04 +01:00
2018-02-04 00:52:35 +01:00
$appearing_method_class = null;
2017-01-09 05:58:06 +01:00
2018-02-04 00:52:35 +01:00
if ($appearing_method_id) {
list($appearing_method_class) = explode('::', $appearing_method_id);
2018-02-04 00:52:35 +01:00
// if the calling class is the same, we know the method exists, so it must be visible
if ($appearing_method_class === $calling_context) {
return true;
}
2016-12-31 17:49:04 +01:00
}
2018-02-04 00:52:35 +01:00
list($declaring_method_class) = explode('::', $declaring_method_id);
2018-11-06 03:57:36 +01:00
if ($source->getSource() instanceof TraitAnalyzer && $declaring_method_class === $source->getFQCLN()) {
2018-02-04 00:52:35 +01:00
return true;
}
2018-02-04 00:52:35 +01:00
$storage = $codebase->methods->getStorage($declaring_method_id);
2016-10-15 06:12:57 +02:00
2018-02-04 00:52:35 +01:00
switch ($storage->visibility) {
2018-11-06 03:57:36 +01:00
case ClassLikeAnalyzer::VISIBILITY_PUBLIC:
2018-02-04 00:52:35 +01:00
return true;
2018-11-06 03:57:36 +01:00
case ClassLikeAnalyzer::VISIBILITY_PRIVATE:
2018-11-04 18:56:53 +01:00
return $calling_context && $appearing_method_class === $calling_context;
2016-12-31 01:36:35 +01:00
2018-11-06 03:57:36 +01:00
case ClassLikeAnalyzer::VISIBILITY_PROTECTED:
2018-02-04 00:52:35 +01:00
if (!$calling_context) {
return false;
}
2016-10-15 06:12:57 +02:00
2018-02-04 00:52:35 +01:00
if ($appearing_method_class
&& $codebase->classExtends($appearing_method_class, $calling_context)
) {
return true;
}
2018-02-04 00:52:35 +01:00
if ($appearing_method_class
&& !$codebase->classExtends($calling_context, $appearing_method_class)
) {
return false;
}
}
2018-02-04 00:52:35 +01:00
return true;
2016-08-15 19:37:21 +02:00
}
2018-02-22 00:59:31 +01:00
/**
* @param ClassLikeStorage $implementer_classlike_storage
* @param ClassLikeStorage $guide_classlike_storage
* @param MethodStorage $implementer_method_storage
* @param MethodStorage $guide_method_storage
* @param CodeLocation $code_location
* @param array $suppressed_issues
* @param bool $prevent_abstract_override
* @param bool $prevent_method_signature_mismatch
2018-02-22 00:59:31 +01:00
*
* @return false|null
*/
public static function compareMethods(
2018-11-06 03:57:36 +01:00
Codebase $codebase,
2018-02-22 00:59:31 +01:00
ClassLikeStorage $implementer_classlike_storage,
ClassLikeStorage $guide_classlike_storage,
MethodStorage $implementer_method_storage,
MethodStorage $guide_method_storage,
string $implementer_called_class_name,
int $implementer_visibility,
2018-02-22 00:59:31 +01:00
CodeLocation $code_location,
array $suppressed_issues,
$prevent_abstract_override = true,
$prevent_method_signature_mismatch = true
2018-02-22 00:59:31 +01:00
) {
$implementer_method_id = $implementer_classlike_storage->name . '::'
. strtolower($guide_method_storage->cased_name);
$implementer_declaring_method_id = $codebase->methods->getDeclaringMethodId($implementer_method_id);
$cased_implementer_method_id = $implementer_classlike_storage->name . '::'
. $implementer_method_storage->cased_name;
$cased_guide_method_id = $guide_classlike_storage->name . '::' . $guide_method_storage->cased_name;
if ($implementer_visibility > $guide_method_storage->visibility) {
2018-02-22 00:59:31 +01:00
if (IssueBuffer::accepts(
new OverriddenMethodAccess(
'Method ' . $cased_implementer_method_id . ' has different access level than '
. $cased_guide_method_id,
$code_location
)
)) {
return false;
}
return null;
}
if ($prevent_abstract_override
&& !$guide_method_storage->abstract
&& $implementer_method_storage->abstract
2018-03-13 18:50:53 +01:00
&& !$guide_classlike_storage->abstract
&& !$guide_classlike_storage->is_interface
) {
if (IssueBuffer::accepts(
new MethodSignatureMismatch(
'Method ' . $cased_implementer_method_id . ' cannot be abstract when inherited method '
. $cased_guide_method_id . ' is non-abstract',
$code_location
)
)) {
return false;
}
return null;
}
if ($guide_method_storage->signature_return_type && $prevent_method_signature_mismatch) {
2018-11-06 03:57:36 +01:00
$guide_signature_return_type = ExpressionAnalyzer::fleshOutType(
$codebase,
2018-02-22 00:59:31 +01:00
$guide_method_storage->signature_return_type,
$guide_classlike_storage->name,
$guide_classlike_storage->name
);
$implementer_signature_return_type = $implementer_method_storage->signature_return_type
2018-11-06 03:57:36 +01:00
? ExpressionAnalyzer::fleshOutType(
$codebase,
2018-02-22 00:59:31 +01:00
$implementer_method_storage->signature_return_type,
$implementer_classlike_storage->name,
$implementer_classlike_storage->name
) : null;
2018-11-06 03:57:36 +01:00
if (!TypeAnalyzer::isContainedByInPhp($implementer_signature_return_type, $guide_signature_return_type)) {
if ($guide_classlike_storage !== $implementer_classlike_storage) {
if (IssueBuffer::accepts(
new MethodSignatureMismatch(
'Method ' . $cased_implementer_method_id . ' with return type \''
. $implementer_signature_return_type . '\' is different to return type \''
. $guide_signature_return_type . '\' of inherited method ' . $cased_guide_method_id,
$code_location
),
$suppressed_issues
)) {
return false;
}
} else {
if (IssueBuffer::accepts(
new ImplementedReturnTypeMismatch(
'Method ' . $cased_implementer_method_id . ' with return type \''
. $implementer_signature_return_type . '\' is different to return type \''
. $guide_signature_return_type . '\' of inherited method ' . $cased_guide_method_id,
$code_location
),
$suppressed_issues
)) {
return false;
}
2018-02-22 00:59:31 +01:00
}
return null;
}
}
if ($guide_method_storage->return_type
2018-02-22 00:59:31 +01:00
&& $implementer_method_storage->return_type
&& $implementer_classlike_storage->user_defined
&& !$guide_classlike_storage->stubbed
) {
2018-11-06 03:57:36 +01:00
$implementer_method_storage_return_type = ExpressionAnalyzer::fleshOutType(
$codebase,
2018-02-22 00:59:31 +01:00
$implementer_method_storage->return_type,
$implementer_classlike_storage->name,
$implementer_called_class_name
2018-02-22 00:59:31 +01:00
);
2018-11-06 03:57:36 +01:00
$guide_method_storage_return_type = ExpressionAnalyzer::fleshOutType(
$codebase,
2018-02-22 00:59:31 +01:00
$guide_method_storage->return_type,
$guide_classlike_storage->name,
$guide_classlike_storage->name
);
$guide_class_name_lc = strtolower($guide_classlike_storage->name);
if (isset($implementer_classlike_storage->template_type_extends[$guide_class_name_lc])) {
$map = $implementer_classlike_storage->template_type_extends[$guide_class_name_lc];
$template_types = [];
foreach ($map as $key => $atomic_type) {
if (is_string($key)) {
$template_types[$key] = [new Type\Union([$atomic_type]), $guide_classlike_storage->name];
}
}
$guide_method_storage_return_type->replaceTemplateTypesWithArgTypes(
$template_types,
$codebase
);
}
2018-02-22 00:59:31 +01:00
// treat void as null when comparing against docblock implementer
if ($implementer_method_storage_return_type->isVoid()) {
$implementer_method_storage_return_type = Type::getNull();
}
if ($guide_method_storage_return_type->isVoid()) {
$guide_method_storage_return_type = Type::getNull();
}
2018-11-06 03:57:36 +01:00
if (!TypeAnalyzer::isContainedBy(
2018-02-22 00:59:31 +01:00
$codebase,
$implementer_method_storage_return_type,
$guide_method_storage_return_type,
false,
false,
$has_scalar_match,
$type_coerced,
$type_coerced_from_mixed
)) {
// is the declared return type more specific than the inferred one?
if ($type_coerced) {
if (IssueBuffer::accepts(
new LessSpecificImplementedReturnType(
'The return type \'' . $guide_method_storage_return_type->getId()
2018-02-22 00:59:31 +01:00
. '\' for ' . $cased_guide_method_id . ' is more specific than the implemented '
. 'return type for ' . $implementer_declaring_method_id . ' \''
. $implementer_method_storage_return_type->getId() . '\'',
$implementer_method_storage->return_type_location ?: $code_location
2018-02-22 00:59:31 +01:00
),
$suppressed_issues
)) {
return false;
}
} else {
if (IssueBuffer::accepts(
new ImplementedReturnTypeMismatch(
'The return type \'' . $guide_method_storage_return_type->getId()
2018-02-22 00:59:31 +01:00
. '\' for ' . $cased_guide_method_id . ' is different to the implemented '
. 'return type for ' . $implementer_declaring_method_id . ' \''
. $implementer_method_storage_return_type->getId() . '\'',
$implementer_method_storage->return_type_location ?: $code_location
2018-02-22 00:59:31 +01:00
),
$suppressed_issues
)) {
return false;
}
}
}
}
foreach ($guide_method_storage->params as $i => $guide_param) {
if (!isset($implementer_method_storage->params[$i])) {
if (!$prevent_abstract_override && $i >= $guide_method_storage->required_param_count) {
continue;
}
2018-02-22 00:59:31 +01:00
if (IssueBuffer::accepts(
new MethodSignatureMismatch(
'Method ' . $cased_implementer_method_id . ' has fewer parameters than parent method ' .
2018-02-22 00:59:31 +01:00
$cased_guide_method_id,
$code_location
)
)) {
return false;
}
return null;
}
$implementer_param = $implementer_method_storage->params[$i];
if ($prevent_method_signature_mismatch
&& $guide_classlike_storage->user_defined
2018-02-22 00:59:31 +01:00
&& $implementer_param->signature_type
2018-11-06 03:57:36 +01:00
&& !TypeAnalyzer::isContainedByInPhp($guide_param->signature_type, $implementer_param->signature_type)
2018-02-22 00:59:31 +01:00
) {
if (IssueBuffer::accepts(
new MethodSignatureMismatch(
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id . ' has wrong type \'' .
$implementer_param->signature_type . '\', expecting \'' .
$guide_param->signature_type . '\' as defined by ' .
$cased_guide_method_id,
$implementer_method_storage->params[$i]->location
?: $code_location
)
)) {
return false;
}
return null;
}
if ($guide_classlike_storage->user_defined
&& $implementer_param->type
&& $guide_param->type
&& $implementer_param->type->getId() !== $guide_param->type->getId()
) {
$implementer_method_storage_param_type = ExpressionAnalyzer::fleshOutType(
2018-02-22 00:59:31 +01:00
$codebase,
$implementer_param->type,
$implementer_classlike_storage->name,
$implementer_called_class_name
);
$guide_method_storage_param_type = ExpressionAnalyzer::fleshOutType(
$codebase,
$guide_param->type,
$guide_classlike_storage->name,
$guide_classlike_storage->name
);
$guide_class_name_lc = strtolower($guide_classlike_storage->name);
if (isset($implementer_classlike_storage->template_type_extends[$guide_class_name_lc])) {
$map = $implementer_classlike_storage->template_type_extends[$guide_class_name_lc];
$template_types = [];
foreach ($map as $key => $atomic_type) {
if (is_string($key)) {
$template_types[$key] = [new Type\Union([$atomic_type]), $guide_classlike_storage->name];
}
}
$guide_method_storage_param_type->replaceTemplateTypesWithArgTypes(
$template_types,
$codebase
);
}
if (!TypeAnalyzer::isContainedBy(
$codebase,
$guide_method_storage_param_type,
$implementer_method_storage_param_type,
2018-02-22 00:59:31 +01:00
false,
false
)) {
if (IssueBuffer::accepts(
new MoreSpecificImplementedParamType(
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id . ' has wrong type \'' .
$implementer_method_storage_param_type->getId() . '\', expecting \'' .
$guide_method_storage_param_type->getId() . '\' as defined by ' .
2018-02-22 00:59:31 +01:00
$cased_guide_method_id,
$implementer_method_storage->params[$i]->location
?: $code_location
),
$suppressed_issues
)) {
return false;
}
}
}
if ($guide_classlike_storage->user_defined && $implementer_param->by_ref !== $guide_param->by_ref) {
if (IssueBuffer::accepts(
new MethodSignatureMismatch(
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id . ' is' .
($implementer_param->by_ref ? '' : ' not') . ' passed by reference, but argument ' .
($i + 1) . ' of ' . $cased_guide_method_id . ' is' . ($guide_param->by_ref ? '' : ' not'),
$implementer_method_storage->params[$i]->location
?: $code_location
)
)) {
return false;
}
return null;
}
$implemeneter_param_type = $implementer_method_storage->params[$i]->type;
$or_null_guide_type = $guide_param->signature_type
? clone $guide_param->signature_type
: null;
if ($or_null_guide_type) {
$or_null_guide_type->addType(new Type\Atomic\TNull);
}
if (!$guide_classlike_storage->user_defined
&& $guide_param->type
&& !$guide_param->type->hasMixed()
2018-02-22 00:59:31 +01:00
&& !$guide_param->type->from_docblock
&& (
!$implemeneter_param_type
|| (
strtolower($implemeneter_param_type->getId()) !== strtolower($guide_param->type->getId())
2018-02-22 00:59:31 +01:00
&& (
!$or_null_guide_type
|| strtolower($implemeneter_param_type->getId())
!== strtolower($or_null_guide_type->getId())
2018-02-22 00:59:31 +01:00
)
)
)
) {
if (IssueBuffer::accepts(
new MethodSignatureMismatch(
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id . ' has wrong type \'' .
$implementer_method_storage->params[$i]->type . '\', expecting \'' .
$guide_param->type . '\' as defined by ' .
$cased_guide_method_id,
$implementer_method_storage->params[$i]->location
?: $code_location
)
)) {
return false;
}
return null;
}
}
if ($guide_classlike_storage->user_defined
&& ($guide_classlike_storage->is_interface || $implementer_method_storage->cased_name !== '__construct')
2018-02-22 00:59:31 +01:00
&& $implementer_method_storage->required_param_count > $guide_method_storage->required_param_count
) {
if (IssueBuffer::accepts(
new MethodSignatureMismatch(
'Method ' . $cased_implementer_method_id . ' has more required parameters than parent method ' .
2018-02-22 00:59:31 +01:00
$cased_guide_method_id,
$code_location
)
)) {
return false;
}
return null;
}
}
/**
* Check that __clone, __construct, and __destruct do not have a return type
* hint in their signature.
*
* @param MethodStorage $method_storage
* @param CodeLocation $code_location
* @return false|null
*/
public static function checkMethodSignatureMustOmitReturnType(
MethodStorage $method_storage,
CodeLocation $code_location
) {
if ($method_storage->signature_return_type === null) {
return null;
}
$cased_method_name = $method_storage->cased_name;
$methodsOfInterest = ['__clone', '__construct', '__destruct'];
if (in_array($cased_method_name, $methodsOfInterest)) {
if (IssueBuffer::accepts(
new MethodSignatureMustOmitReturnType(
'Method ' . $cased_method_name . ' must not declare a return type',
$code_location
)
)) {
return false;
}
}
return null;
}
2016-02-04 15:22:46 +01:00
}