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;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2018-12-02 00:37:49 +01:00
|
|
|
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;
|
2019-05-14 21:44:46 +02:00
|
|
|
use Psalm\Issue\ImplementedParamTypeMismatch;
|
2018-02-22 00:59:31 +01:00
|
|
|
use Psalm\Issue\ImplementedReturnTypeMismatch;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Issue\InaccessibleMethod;
|
2018-12-02 00:37:49 +01:00
|
|
|
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;
|
2018-04-21 19:36:18 +02:00
|
|
|
use Psalm\Issue\MethodSignatureMustOmitReturnType;
|
2018-02-22 00:59:31 +01:00
|
|
|
use Psalm\Issue\MoreSpecificImplementedParamType;
|
2018-04-17 20:06:27 +02:00
|
|
|
use Psalm\Issue\LessSpecificImplementedReturnType;
|
2016-12-31 15:20:10 +01:00
|
|
|
use Psalm\Issue\NonStaticSelfCall;
|
2018-02-22 00:59:31 +01:00
|
|
|
use Psalm\Issue\OverriddenMethodAccess;
|
2019-03-29 19:09:06 +01:00
|
|
|
use Psalm\Issue\TraitMethodSignatureMismatch;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Issue\UndefinedMethod;
|
|
|
|
use Psalm\IssueBuffer;
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\StatementsSource;
|
2018-02-22 00:59:31 +01:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
|
|
|
use Psalm\Storage\MethodStorage;
|
|
|
|
use Psalm\Type;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
|
|
|
use function explode;
|
|
|
|
use function is_string;
|
|
|
|
use function in_array;
|
2019-09-22 15:26:26 +02:00
|
|
|
use Psalm\Issue\MissingImmutableAnnotation;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class MethodAnalyzer extends FunctionLikeAnalyzer
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2018-12-18 05:29:27 +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
|
|
|
}
|
|
|
|
|
2018-12-18 05:29:27 +01:00
|
|
|
parent::__construct($function, $source, $storage);
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether a given method is static or not
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2016-12-24 19:23:22 +01:00
|
|
|
* @param string $method_id
|
2016-12-31 15:20:10 +01:00
|
|
|
* @param bool $self_call
|
2018-06-04 01:11:07 +02:00
|
|
|
* @param bool $is_context_dynamic
|
2016-12-24 19:23:22 +01:00
|
|
|
* @param CodeLocation $code_location
|
|
|
|
* @param array<string> $suppressed_issues
|
2018-06-04 01:11:07 +02:00
|
|
|
* @param bool $is_dynamic_this_method
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return bool
|
2016-04-16 22:28:25 +02:00
|
|
|
*/
|
2017-01-12 03:37:53 +01:00
|
|
|
public static function checkStatic(
|
2016-12-31 15:20:10 +01:00
|
|
|
$method_id,
|
|
|
|
$self_call,
|
2018-06-04 01:11:07 +02:00
|
|
|
$is_context_dynamic,
|
2018-11-06 03:57:36 +01:00
|
|
|
Codebase $codebase,
|
2016-12-31 15:20:10 +01:00
|
|
|
CodeLocation $code_location,
|
2018-06-04 01:11:07 +02:00
|
|
|
array $suppressed_issues,
|
|
|
|
&$is_dynamic_this_method = false
|
2016-12-31 15:20:10 +01:00
|
|
|
) {
|
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);
|
2016-04-16 22:28:25 +02:00
|
|
|
|
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);
|
2016-12-30 21:53:35 +01:00
|
|
|
|
|
|
|
if (!$storage->is_static) {
|
2016-12-31 15:20:10 +01:00
|
|
|
if ($self_call) {
|
2018-06-04 01:11:07 +02:00
|
|
|
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;
|
2016-12-31 15:20:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidStaticInvocation(
|
2018-02-04 00:52:35 +01:00
|
|
|
'Method ' . $codebase_methods->getCasedMethodId($method_id) .
|
2017-07-29 21:05:06 +02:00
|
|
|
' is not static, but is called ' .
|
2017-04-28 06:31:55 +02:00
|
|
|
'statically',
|
2016-12-31 15:20:10 +01:00
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-22 19:29:46 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return true;
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param string $method_id
|
|
|
|
* @param CodeLocation $code_location
|
2019-06-16 15:12:32 +02:00
|
|
|
* @param string[] $suppressed_issues
|
2019-12-29 00:37:55 +01:00
|
|
|
* @param string|null $calling_function_id
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-07-24 22:21:42 +02:00
|
|
|
* @return bool|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2017-01-30 05:44:05 +01:00
|
|
|
public static function checkMethodExists(
|
2018-11-06 03:57:36 +01:00
|
|
|
Codebase $codebase,
|
2017-01-30 05:44:05 +01:00
|
|
|
$method_id,
|
|
|
|
CodeLocation $code_location,
|
2018-06-19 22:14:51 +02:00
|
|
|
array $suppressed_issues,
|
2019-12-29 00:37:55 +01:00
|
|
|
$calling_function_id = null
|
2017-01-30 05:44:05 +01:00
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if ($codebase->methods->methodExists(
|
2018-06-19 22:14:51 +02:00
|
|
|
$method_id,
|
2019-12-29 00:37:55 +01:00
|
|
|
$calling_function_id,
|
|
|
|
$calling_function_id !== $method_id ? $code_location : null,
|
2019-04-16 22:07:48 +02:00
|
|
|
null,
|
|
|
|
$code_location->file_path
|
2018-06-19 22:14:51 +02:00
|
|
|
)) {
|
2016-10-23 18:24:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
2018-03-21 03:36:03 +01:00
|
|
|
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
|
|
|
/**
|
2016-12-04 01:11:30 +01:00
|
|
|
* @param string $method_id
|
|
|
|
* @param CodeLocation $code_location
|
2019-06-16 15:12:32 +02:00
|
|
|
* @param string[] $suppressed_issues
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-15 06:12:57 +02:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2018-12-02 00:37:49 +01:00
|
|
|
public static function checkMethodNotDeprecatedOrInternal(
|
2018-11-06 03:57:36 +01:00
|
|
|
Codebase $codebase,
|
2018-12-02 00:37:49 +01:00
|
|
|
Context $context,
|
2017-07-29 21:05:06 +02:00
|
|
|
$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);
|
2016-12-30 21:53:35 +01:00
|
|
|
|
|
|
|
if ($storage->deprecated) {
|
2016-07-22 19:29:46 +02:00
|
|
|
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) .
|
2017-07-29 21:05:06 +02:00
|
|
|
' has been marked as deprecated',
|
2018-03-21 03:36:03 +01:00
|
|
|
$code_location,
|
|
|
|
$method_id
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
|
|
|
$suppressed_issues
|
2016-07-22 19:29:46 +02:00
|
|
|
)) {
|
2018-11-09 20:18:46 +01:00
|
|
|
// continue
|
2016-07-22 19:29:46 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2019-05-11 21:39:53 +02:00
|
|
|
if ($storage->psalm_internal
|
2019-05-11 16:15:50 +02:00
|
|
|
&& $context->self
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
) {
|
2019-05-14 23:30:16 +02:00
|
|
|
if (! NamespaceAnalyzer::isWithin($context->self, $storage->psalm_internal)
|
|
|
|
) {
|
2019-05-11 16:15:50 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InternalMethod(
|
|
|
|
'The method ' . $codebase_methods->getCasedMethodId($method_id) .
|
2019-05-11 21:39:53 +02:00
|
|
|
' has been marked as internal to ' . $storage->psalm_internal,
|
2019-05-11 16:15:50 +02:00
|
|
|
$code_location,
|
|
|
|
$method_id
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
if ($storage->internal
|
|
|
|
&& $context->self
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
) {
|
|
|
|
$declaring_class = explode('::', $method_id)[0];
|
2019-05-14 23:50:21 +02:00
|
|
|
if (! NamespaceAnalyzer::nameSpaceRootsMatch($context->self, $declaring_class)) {
|
2018-12-02 00:37:49 +01:00
|
|
|
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;
|
2016-07-22 19:29:46 +02:00
|
|
|
}
|
|
|
|
|
2017-11-11 00:08:17 +01:00
|
|
|
/**
|
|
|
|
* @param string $method_id
|
2019-03-01 14:57:10 +01:00
|
|
|
* @param Context $context
|
2017-11-11 00:08:17 +01:00
|
|
|
* @param StatementsSource $source
|
|
|
|
* @param CodeLocation $code_location
|
2019-06-16 15:12:32 +02:00
|
|
|
* @param string[] $suppressed_issues
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-06-14 01:57:32 +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,
|
2019-03-01 14:57:10 +01:00
|
|
|
Context $context,
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsSource $source,
|
2016-12-04 01:11:30 +01:00
|
|
|
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;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
list($fq_classlike_name, $method_name) = explode('::', $method_id);
|
|
|
|
|
|
|
|
if ($codebase_methods->visibility_provider->has($fq_classlike_name)) {
|
|
|
|
$method_visible = $codebase_methods->visibility_provider->isMethodVisible(
|
|
|
|
$source,
|
|
|
|
$fq_classlike_name,
|
|
|
|
$method_name,
|
|
|
|
$context,
|
|
|
|
$code_location
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($method_visible === false) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InaccessibleMethod(
|
|
|
|
'Cannot access method ' . $codebase_methods->getCasedMethodId($method_id) .
|
|
|
|
' from context ' . $context->self,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($method_visible === true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-11-09 05:14:27 +01:00
|
|
|
$method_name = explode('::', $method_id)[1];
|
2017-07-09 20:36:06 +02:00
|
|
|
|
2019-01-21 23:29:12 +01:00
|
|
|
if ($method_name === '__construct'
|
|
|
|
|| $method_id === 'Closure::__invoke'
|
|
|
|
|| $method_id === 'Closure::fromcallable'
|
|
|
|
) {
|
2017-07-09 20:36:06 +02:00
|
|
|
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);
|
2016-12-12 20:29:58 +01:00
|
|
|
|
2017-07-09 20:50:57 +02:00
|
|
|
$appearing_method_class = null;
|
2019-01-16 16:59:06 +01:00
|
|
|
$appearing_class_storage = null;
|
2019-01-07 17:53:22 +01:00
|
|
|
$appearing_method_name = null;
|
2017-07-09 20:50:57 +02:00
|
|
|
|
|
|
|
if ($appearing_method_id) {
|
2019-01-07 17:53:22 +01:00
|
|
|
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
|
2019-03-01 14:57:10 +01:00
|
|
|
if ($appearing_method_class === $context->self) {
|
2017-07-09 20:50:57 +02:00
|
|
|
return null;
|
|
|
|
}
|
2019-01-07 17:53:22 +01:00
|
|
|
|
2019-01-16 16:59:06 +01:00
|
|
|
$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);
|
2019-01-07 17:53:22 +01:00
|
|
|
$visibility = $storage->visibility;
|
|
|
|
|
|
|
|
if ($appearing_method_name
|
2019-01-16 16:59:06 +01:00
|
|
|
&& isset($appearing_class_storage->trait_visibility_map[$appearing_method_name])
|
2019-01-07 17:53:22 +01:00
|
|
|
) {
|
2019-01-16 16:59:06 +01:00
|
|
|
$visibility = $appearing_class_storage->trait_visibility_map[$appearing_method_name];
|
2019-01-07 17:53:22 +01:00
|
|
|
}
|
2016-12-30 21:53:35 +01:00
|
|
|
|
2019-01-07 17:53:22 +01:00
|
|
|
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:
|
2019-03-01 14:57:10 +01:00
|
|
|
if (!$context->self || $appearing_method_class !== $context->self) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new InaccessibleMethod(
|
2018-02-04 00:52:35 +01:00
|
|
|
'Cannot access private method ' . $codebase_methods->getCasedMethodId($method_id) .
|
2019-03-01 14:57:10 +01:00
|
|
|
' from context ' . $context->self,
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-07-22 19:29:46 +02:00
|
|
|
),
|
2016-11-02 07:29:00 +01:00
|
|
|
$suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
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:
|
2019-03-01 14:57:10 +01:00
|
|
|
if (!$context->self) {
|
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,
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
|
|
|
$suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if ($appearing_method_class
|
2019-03-01 14:57:10 +01:00
|
|
|
&& $codebase_classlikes->classExtends($appearing_method_class, $context->self)
|
2017-07-29 21:05:06 +02:00
|
|
|
) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-04-30 20:14:22 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if ($appearing_method_class
|
2019-03-01 14:57:10 +01:00
|
|
|
&& !$codebase_classlikes->classExtends($context->self, $appearing_method_class)
|
2017-07-29 21:05:06 +02:00
|
|
|
) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new InaccessibleMethod(
|
2018-02-04 00:52:35 +01:00
|
|
|
'Cannot access protected method ' . $codebase_methods->getCasedMethodId($method_id) .
|
2019-03-01 14:57:10 +01:00
|
|
|
' from context ' . $context->self,
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-07-22 19:29:46 +02:00
|
|
|
),
|
2016-11-02 07:29:00 +01:00
|
|
|
$suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-18 19:31:59 +02:00
|
|
|
}
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-04-16 22:28:25 +02:00
|
|
|
}
|
2016-04-17 18:27:47 +02:00
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
2018-02-04 00:52:35 +01:00
|
|
|
* @param string $method_id
|
2019-03-01 14:57:10 +01:00
|
|
|
* @param Context $context
|
2018-02-04 00:52:35 +01:00
|
|
|
* @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(
|
2018-01-21 19:38:51 +01:00
|
|
|
$method_id,
|
2019-03-01 14:57:10 +01:00
|
|
|
Context $context,
|
2018-02-04 00:52:35 +01:00
|
|
|
StatementsSource $source
|
2018-01-21 19:38:51 +01:00
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase = $source->getCodebase();
|
2017-01-12 03:37:53 +01:00
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
list($fq_classlike_name, $method_name) = explode('::', $method_id);
|
|
|
|
|
|
|
|
if ($codebase->methods->visibility_provider->has($fq_classlike_name)) {
|
|
|
|
$method_visible = $codebase->methods->visibility_provider->isMethodVisible(
|
|
|
|
$source,
|
|
|
|
$fq_classlike_name,
|
|
|
|
$method_name,
|
|
|
|
$context,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($method_visible !== null) {
|
|
|
|
return $method_visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-11-11 02:56:31 +01:00
|
|
|
// this can happen for methods in the callmap that were not reflected
|
|
|
|
return true;
|
2016-12-30 21:53:35 +01:00
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
|
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);
|
2017-01-06 07:07:11 +01:00
|
|
|
|
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
|
2019-03-01 14:57:10 +01:00
|
|
|
if ($appearing_method_class === $context->self) {
|
2018-02-04 00:52:35 +01:00
|
|
|
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);
|
2016-12-30 21:53:35 +01:00
|
|
|
|
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;
|
|
|
|
}
|
2017-07-29 21:05:06 +02:00
|
|
|
|
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;
|
2016-12-30 21:53:35 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
case ClassLikeAnalyzer::VISIBILITY_PRIVATE:
|
2019-03-01 14:57:10 +01:00
|
|
|
return $context->self && $appearing_method_class === $context->self;
|
2016-12-31 01:36:35 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
case ClassLikeAnalyzer::VISIBILITY_PROTECTED:
|
2019-03-01 14:57:10 +01:00
|
|
|
if (!$context->self) {
|
2018-02-04 00:52:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-15 06:12:57 +02:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
if ($appearing_method_class
|
2019-03-01 14:57:10 +01:00
|
|
|
&& $codebase->classExtends($appearing_method_class, $context->self)
|
2018-02-04 00:52:35 +01:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-27 02:06:05 +01:00
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
if ($appearing_method_class
|
2019-03-01 14:57:10 +01:00
|
|
|
&& !$codebase->classExtends($context->self, $appearing_method_class)
|
2018-02-04 00:52:35 +01:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-27 02:06:05 +01:00
|
|
|
}
|
|
|
|
|
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
|
2019-06-16 15:12:32 +02:00
|
|
|
* @param string[] $suppressed_issues
|
2018-03-13 23:11:57 +01:00
|
|
|
* @param bool $prevent_abstract_override
|
2018-11-30 21:13:25 +01:00
|
|
|
* @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,
|
2019-01-16 21:08:11 +01:00
|
|
|
string $implementer_called_class_name,
|
2019-01-16 16:59:06 +01:00
|
|
|
int $implementer_visibility,
|
2018-02-22 00:59:31 +01:00
|
|
|
CodeLocation $code_location,
|
2018-03-13 23:11:57 +01:00
|
|
|
array $suppressed_issues,
|
2018-11-30 21:13:25 +01:00
|
|
|
$prevent_abstract_override = true,
|
|
|
|
$prevent_method_signature_mismatch = true
|
2018-02-22 00:59:31 +01:00
|
|
|
) {
|
2019-12-20 18:57:31 +01:00
|
|
|
$config = $codebase->config;
|
|
|
|
|
2018-02-22 00:59:31 +01:00
|
|
|
$implementer_method_id = $implementer_classlike_storage->name . '::'
|
2019-11-25 21:20:31 +01:00
|
|
|
. strtolower($guide_method_storage->cased_name ?: '');
|
2018-02-22 00:59:31 +01:00
|
|
|
|
|
|
|
$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;
|
|
|
|
|
2019-01-16 16:59:06 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-13 23:11:57 +01:00
|
|
|
if ($prevent_abstract_override
|
|
|
|
&& !$guide_method_storage->abstract
|
2018-03-13 17:52:00 +01:00
|
|
|
&& $implementer_method_storage->abstract
|
2018-03-13 18:50:53 +01:00
|
|
|
&& !$guide_classlike_storage->abstract
|
|
|
|
&& !$guide_classlike_storage->is_interface
|
2018-03-13 17:52:00 +01:00
|
|
|
) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:13:25 +01:00
|
|
|
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,
|
2019-05-25 17:51:09 +02:00
|
|
|
$guide_classlike_storage->name,
|
|
|
|
$guide_classlike_storage->parent_class
|
2018-02-22 00:59:31 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$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,
|
2019-05-25 17:51:09 +02:00
|
|
|
$implementer_classlike_storage->name,
|
|
|
|
$implementer_classlike_storage->parent_class
|
2018-02-22 00:59:31 +01:00
|
|
|
) : null;
|
|
|
|
|
2019-11-27 06:19:36 +01:00
|
|
|
$is_contained_by = $codebase->php_major_version >= 7
|
|
|
|
&& $codebase->php_minor_version >= 4
|
|
|
|
&& $implementer_signature_return_type
|
|
|
|
? TypeAnalyzer::isContainedBy(
|
|
|
|
$codebase,
|
|
|
|
$implementer_signature_return_type,
|
|
|
|
$guide_signature_return_type
|
|
|
|
)
|
|
|
|
: TypeAnalyzer::isContainedByInPhp($implementer_signature_return_type, $guide_signature_return_type);
|
|
|
|
|
|
|
|
if (!$is_contained_by) {
|
2019-12-20 19:08:35 +01:00
|
|
|
if ($guide_classlike_storage->is_trait === $implementer_classlike_storage->is_trait
|
|
|
|
|| (!$implementer_method_storage->abstract
|
2019-12-20 20:37:36 +01:00
|
|
|
&& !$guide_method_storage->abstract)
|
2019-12-20 19:08:35 +01:00
|
|
|
) {
|
2019-01-13 19:27:07 +01:00
|
|
|
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(
|
2019-03-29 19:09:06 +01:00
|
|
|
new TraitMethodSignatureMismatch(
|
2019-01-13 19:27:07 +01:00
|
|
|
'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;
|
|
|
|
}
|
2019-01-24 16:58:59 +01:00
|
|
|
}
|
|
|
|
|
2019-09-22 15:26:26 +02:00
|
|
|
if ($guide_method_storage->external_mutation_free
|
|
|
|
&& !$implementer_method_storage->external_mutation_free
|
2019-09-25 23:39:32 +02:00
|
|
|
&& !$guide_method_storage->mutation_free_inferred
|
2019-09-22 15:26:26 +02:00
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingImmutableAnnotation(
|
|
|
|
$cased_guide_method_id . ' is marked immutable, but '
|
|
|
|
. $implementer_method_id . ' is not marked immutable',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 16:58:59 +01:00
|
|
|
if ($guide_method_storage->return_type
|
2018-02-22 00:59:31 +01:00
|
|
|
&& $implementer_method_storage->return_type
|
2019-01-24 21:03:13 +01:00
|
|
|
&& ($guide_method_storage->signature_return_type !== $guide_method_storage->return_type
|
|
|
|
|| $implementer_method_storage->signature_return_type !== $implementer_method_storage->return_type)
|
2018-02-22 00:59:31 +01:00
|
|
|
&& $implementer_classlike_storage->user_defined
|
2019-01-24 21:03:13 +01:00
|
|
|
&& (!$guide_classlike_storage->stubbed || $guide_classlike_storage->template_types)
|
2018-02-22 00:59:31 +01:00
|
|
|
) {
|
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,
|
2019-05-25 17:51:09 +02:00
|
|
|
$implementer_called_class_name,
|
|
|
|
$implementer_classlike_storage->parent_class
|
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,
|
2019-09-01 21:45:28 +02:00
|
|
|
$guide_classlike_storage->is_trait
|
|
|
|
? $implementer_classlike_storage->name
|
|
|
|
: $guide_classlike_storage->name,
|
|
|
|
$guide_classlike_storage->is_trait
|
|
|
|
? $implementer_called_class_name
|
|
|
|
: $guide_classlike_storage->name,
|
2019-05-25 17:51:09 +02:00
|
|
|
$guide_classlike_storage->parent_class
|
2018-02-22 00:59:31 +01:00
|
|
|
);
|
|
|
|
|
2019-06-25 05:31:06 +02:00
|
|
|
$guide_class_name = $guide_classlike_storage->name;
|
2019-12-18 14:58:37 +01:00
|
|
|
$implementer_class_name = $implementer_classlike_storage->name;
|
|
|
|
|
|
|
|
$implementer_called_class_storage = $implementer_classlike_storage;
|
|
|
|
|
|
|
|
if ($implementer_called_class_name !== $implementer_class_name) {
|
|
|
|
$implementer_called_class_storage = $codebase->classlike_storage_provider->get(
|
|
|
|
$implementer_called_class_name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($implementer_called_class_storage !== $implementer_classlike_storage
|
2019-12-20 20:37:36 +01:00
|
|
|
&& $implementer_called_class_storage->template_type_extends
|
2019-12-18 14:58:37 +01:00
|
|
|
) {
|
2019-12-20 20:37:36 +01:00
|
|
|
self::transformTemplates(
|
|
|
|
$implementer_called_class_storage->template_type_extends,
|
|
|
|
$implementer_class_name,
|
|
|
|
$implementer_method_storage_return_type,
|
2019-12-18 14:58:37 +01:00
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
|
2019-12-20 20:37:36 +01:00
|
|
|
self::transformTemplates(
|
|
|
|
$implementer_called_class_storage->template_type_extends,
|
|
|
|
$guide_class_name,
|
|
|
|
$guide_method_storage_return_type,
|
2019-12-18 14:58:37 +01:00
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
}
|
2019-01-11 01:01:30 +01:00
|
|
|
|
2019-12-20 20:37:36 +01:00
|
|
|
if ($implementer_classlike_storage->template_type_extends) {
|
|
|
|
self::transformTemplates(
|
|
|
|
$implementer_classlike_storage->template_type_extends,
|
|
|
|
$guide_class_name,
|
|
|
|
$implementer_method_storage_return_type,
|
2019-06-08 00:42:26 +02:00
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
|
2019-12-20 20:37:36 +01:00
|
|
|
self::transformTemplates(
|
|
|
|
$implementer_classlike_storage->template_type_extends,
|
|
|
|
$guide_class_name,
|
|
|
|
$guide_method_storage_return_type,
|
2019-01-28 05:12:40 +01:00
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-25 05:31:06 +02:00
|
|
|
$guide_trait_name = null;
|
2019-01-28 05:12:40 +01:00
|
|
|
|
|
|
|
if ($guide_classlike_storage === $implementer_classlike_storage) {
|
2019-06-25 05:31:06 +02:00
|
|
|
$guide_trait_name = $implementer_method_storage->defining_fqcln;
|
2019-01-28 05:12:40 +01:00
|
|
|
}
|
|
|
|
|
2019-06-25 05:31:06 +02:00
|
|
|
if ($guide_trait_name
|
|
|
|
&& isset($implementer_classlike_storage->template_type_extends[$guide_trait_name])
|
2019-01-28 05:12:40 +01:00
|
|
|
) {
|
2019-06-25 05:31:06 +02:00
|
|
|
$map = $implementer_classlike_storage->template_type_extends[$guide_trait_name];
|
2019-01-28 05:12:40 +01:00
|
|
|
|
|
|
|
$template_types = [];
|
|
|
|
|
2019-03-16 16:15:25 +01:00
|
|
|
foreach ($map as $key => $type) {
|
2019-11-25 21:20:31 +01:00
|
|
|
if (is_string($key) && $implementer_method_storage->defining_fqcln) {
|
2019-03-22 20:59:10 +01:00
|
|
|
$template_types[$key][$implementer_method_storage->defining_fqcln] = [
|
2019-03-16 16:15:25 +01:00
|
|
|
$type,
|
2019-01-28 05:12:40 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 03:27:40 +01:00
|
|
|
$template_result = new \Psalm\Internal\Type\TemplateResult($template_types, []);
|
|
|
|
|
2019-01-28 05:12:40 +01:00
|
|
|
$implementer_method_storage_return_type->replaceTemplateTypesWithArgTypes(
|
2019-11-04 03:27:40 +01:00
|
|
|
$template_result->template_types,
|
2019-01-11 01:18:02 +01:00
|
|
|
$codebase
|
2019-01-11 01:01:30 +01:00
|
|
|
);
|
2019-05-06 22:38:08 +02:00
|
|
|
|
|
|
|
$guide_method_storage_return_type->replaceTemplateTypesWithArgTypes(
|
2019-11-04 03:27:40 +01:00
|
|
|
$template_result->template_types,
|
2019-05-06 22:38:08 +02:00
|
|
|
$codebase
|
|
|
|
);
|
2019-01-11 01:01:30 +01:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
$union_comparison_results = new TypeComparisonResult();
|
|
|
|
|
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,
|
2019-07-10 07:35:57 +02:00
|
|
|
$union_comparison_results
|
2018-02-22 00:59:31 +01:00
|
|
|
)) {
|
|
|
|
// is the declared return type more specific than the inferred one?
|
2019-07-10 07:35:57 +02:00
|
|
|
if ($union_comparison_results->type_coerced) {
|
2018-02-22 00:59:31 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2018-04-17 20:06:27 +02:00
|
|
|
new LessSpecificImplementedReturnType(
|
2019-12-18 14:58:37 +01:00
|
|
|
'The inherited return type \'' . $guide_method_storage_return_type->getId()
|
2019-03-19 02:05:37 +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
|
2019-12-20 20:37:36 +01:00
|
|
|
?: $code_location
|
2018-02-22 00:59:31 +01:00
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImplementedReturnTypeMismatch(
|
2019-12-18 14:58:37 +01:00
|
|
|
'The inherited return type \'' . $guide_method_storage_return_type->getId()
|
2019-03-19 02:05:37 +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
|
2019-12-20 20:37:36 +01:00
|
|
|
?: $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])) {
|
2018-03-14 15:51:13 +01:00
|
|
|
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(
|
2018-07-14 02:17:29 +02:00
|
|
|
'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];
|
|
|
|
|
2019-05-14 21:44:46 +02:00
|
|
|
if ($prevent_method_signature_mismatch
|
|
|
|
&& !$guide_classlike_storage->user_defined
|
|
|
|
&& $guide_param->type
|
|
|
|
) {
|
|
|
|
$implementer_param_type = $implementer_method_storage->params[$i]->signature_type;
|
|
|
|
|
|
|
|
$guide_param_signature_type = $guide_param->type;
|
|
|
|
|
|
|
|
$or_null_guide_param_signature_type = $guide_param->signature_type
|
|
|
|
? clone $guide_param->signature_type
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if ($or_null_guide_param_signature_type) {
|
|
|
|
$or_null_guide_param_signature_type->addType(new Type\Atomic\TNull);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cased_guide_method_id === 'Serializable::unserialize') {
|
|
|
|
$guide_param_signature_type = null;
|
|
|
|
$or_null_guide_param_signature_type = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$guide_param->type->hasMixed()
|
|
|
|
&& !$guide_param->type->from_docblock
|
|
|
|
&& ($implementer_param_type || $guide_param_signature_type)
|
|
|
|
) {
|
|
|
|
if ($implementer_param_type
|
|
|
|
&& (!$guide_param_signature_type
|
|
|
|
|| strtolower($implementer_param_type->getId())
|
|
|
|
!== strtolower($guide_param_signature_type->getId()))
|
|
|
|
&& (!$or_null_guide_param_signature_type
|
|
|
|
|| strtolower($implementer_param_type->getId())
|
|
|
|
!== strtolower($or_null_guide_param_signature_type->getId()))
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMismatch(
|
|
|
|
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id . ' has wrong type \'' .
|
|
|
|
$implementer_param_type . '\', expecting \'' .
|
|
|
|
$guide_param_signature_type . '\' as defined by ' .
|
|
|
|
$cased_guide_method_id,
|
|
|
|
$implementer_method_storage->params[$i]->location
|
2019-12-20 18:57:31 +01:00
|
|
|
&& $config->isInProjectDirs(
|
|
|
|
$implementer_method_storage->params[$i]->location->file_path
|
|
|
|
)
|
|
|
|
? $implementer_method_storage->params[$i]->location
|
|
|
|
: $code_location
|
2019-05-14 21:44:46 +02:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:13:25 +01:00
|
|
|
if ($prevent_method_signature_mismatch
|
|
|
|
&& $guide_classlike_storage->user_defined
|
2018-02-22 00:59:31 +01:00
|
|
|
&& $implementer_param->signature_type
|
|
|
|
) {
|
2019-03-17 16:31:04 +01:00
|
|
|
$guide_param_signature_type = $guide_param->signature_type
|
|
|
|
? ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
$guide_param->signature_type,
|
|
|
|
$guide_classlike_storage->name,
|
2019-05-25 17:51:09 +02:00
|
|
|
$guide_classlike_storage->name,
|
|
|
|
$guide_classlike_storage->parent_class
|
2018-02-22 00:59:31 +01:00
|
|
|
)
|
2019-03-17 16:31:04 +01:00
|
|
|
: null;
|
|
|
|
|
|
|
|
$implementer_param_signature_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
$implementer_param->signature_type,
|
|
|
|
$implementer_classlike_storage->name,
|
2019-05-25 17:51:09 +02:00
|
|
|
$implementer_classlike_storage->name,
|
2019-05-25 18:11:46 +02:00
|
|
|
$implementer_classlike_storage->parent_class
|
2019-03-17 16:31:04 +01:00
|
|
|
);
|
|
|
|
|
2019-11-27 06:19:36 +01:00
|
|
|
$is_contained_by = $codebase->php_major_version >= 7
|
|
|
|
&& $codebase->php_minor_version >= 4
|
|
|
|
&& $guide_param_signature_type
|
|
|
|
? TypeAnalyzer::isContainedBy(
|
|
|
|
$codebase,
|
|
|
|
$guide_param_signature_type,
|
|
|
|
$implementer_param_signature_type
|
|
|
|
)
|
|
|
|
: TypeAnalyzer::isContainedByInPhp(
|
|
|
|
$guide_param_signature_type,
|
|
|
|
$implementer_param_signature_type
|
|
|
|
);
|
|
|
|
if (!$is_contained_by) {
|
2019-12-20 19:08:35 +01:00
|
|
|
if ($guide_classlike_storage->is_trait === $implementer_classlike_storage->is_trait
|
|
|
|
|| (!$implementer_method_storage->abstract
|
2019-12-20 20:37:36 +01:00
|
|
|
&& !$guide_method_storage->abstract)
|
2019-12-20 19:08:35 +01:00
|
|
|
) {
|
2019-03-29 19:09:06 +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
|
2019-12-20 18:57:31 +01:00
|
|
|
&& $config->isInProjectDirs(
|
|
|
|
$implementer_method_storage->params[$i]->location->file_path
|
|
|
|
)
|
|
|
|
? $implementer_method_storage->params[$i]->location
|
|
|
|
: $code_location
|
2019-03-29 19:09:06 +01:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new TraitMethodSignatureMismatch(
|
|
|
|
'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
|
2019-12-20 18:57:31 +01:00
|
|
|
&& $config->isInProjectDirs(
|
|
|
|
$implementer_method_storage->params[$i]->location->file_path
|
|
|
|
)
|
|
|
|
? $implementer_method_storage->params[$i]->location
|
|
|
|
: $code_location
|
2019-03-29 19:09:06 +01:00
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-17 16:31:04 +01:00
|
|
|
}
|
2018-02-22 00:59:31 +01:00
|
|
|
|
2019-03-17 16:31:04 +01:00
|
|
|
return null;
|
|
|
|
}
|
2018-02-22 00:59:31 +01:00
|
|
|
}
|
|
|
|
|
2019-05-14 21:44:46 +02:00
|
|
|
if ($implementer_param->type
|
2018-02-22 00:59:31 +01:00
|
|
|
&& $guide_param->type
|
|
|
|
&& $implementer_param->type->getId() !== $guide_param->type->getId()
|
|
|
|
) {
|
2019-01-05 14:43:37 +01:00
|
|
|
$implementer_method_storage_param_type = ExpressionAnalyzer::fleshOutType(
|
2018-02-22 00:59:31 +01:00
|
|
|
$codebase,
|
|
|
|
$implementer_param->type,
|
2019-01-05 14:43:37 +01:00
|
|
|
$implementer_classlike_storage->name,
|
2019-05-25 17:51:09 +02:00
|
|
|
$implementer_called_class_name,
|
2019-05-25 18:18:12 +02:00
|
|
|
$implementer_classlike_storage->parent_class
|
2019-01-05 14:43:37 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$guide_method_storage_param_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
$guide_param->type,
|
|
|
|
$guide_classlike_storage->name,
|
2019-05-25 17:51:09 +02:00
|
|
|
$guide_classlike_storage->name,
|
2019-05-25 18:18:12 +02:00
|
|
|
$guide_classlike_storage->parent_class
|
2019-01-05 14:43:37 +01:00
|
|
|
);
|
|
|
|
|
2019-06-25 05:31:06 +02:00
|
|
|
$guide_class_name = $guide_classlike_storage->name;
|
2019-12-18 14:58:37 +01:00
|
|
|
$implementer_class_name = $implementer_classlike_storage->name;
|
|
|
|
|
|
|
|
$implementer_called_class_storage = $implementer_classlike_storage;
|
|
|
|
|
|
|
|
if ($implementer_called_class_name !== $implementer_class_name) {
|
|
|
|
$implementer_called_class_storage = $codebase->classlike_storage_provider->get(
|
|
|
|
$implementer_called_class_name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($implementer_called_class_storage !== $implementer_classlike_storage
|
2019-12-20 20:37:36 +01:00
|
|
|
&& $implementer_called_class_storage->template_type_extends
|
2019-12-18 14:58:37 +01:00
|
|
|
) {
|
2019-12-20 20:37:36 +01:00
|
|
|
self::transformTemplates(
|
|
|
|
$implementer_called_class_storage->template_type_extends,
|
|
|
|
$implementer_class_name,
|
|
|
|
$implementer_method_storage_param_type,
|
2019-12-18 14:58:37 +01:00
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
}
|
2019-01-11 17:18:51 +01:00
|
|
|
|
2019-12-20 20:37:36 +01:00
|
|
|
if ($implementer_called_class_storage->template_type_extends) {
|
|
|
|
self::transformTemplates(
|
|
|
|
$implementer_called_class_storage->template_type_extends,
|
|
|
|
$guide_class_name,
|
|
|
|
$guide_method_storage_param_type,
|
2019-01-11 17:18:51 +01:00
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:35:57 +02:00
|
|
|
$union_comparison_results = new TypeComparisonResult();
|
|
|
|
|
2019-01-05 14:43:37 +01:00
|
|
|
if (!TypeAnalyzer::isContainedBy(
|
|
|
|
$codebase,
|
|
|
|
$guide_method_storage_param_type,
|
|
|
|
$implementer_method_storage_param_type,
|
2019-05-14 21:44:46 +02:00
|
|
|
!$guide_classlike_storage->user_defined,
|
|
|
|
!$guide_classlike_storage->user_defined,
|
2019-07-10 07:35:57 +02:00
|
|
|
$union_comparison_results
|
2018-02-22 00:59:31 +01:00
|
|
|
)) {
|
2019-05-14 21:44:46 +02:00
|
|
|
// is the declared return type more specific than the inferred one?
|
2019-07-10 07:35:57 +02:00
|
|
|
if ($union_comparison_results->type_coerced) {
|
2019-05-14 21:44:46 +02:00
|
|
|
if ($guide_classlike_storage->user_defined) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MoreSpecificImplementedParamType(
|
|
|
|
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id
|
|
|
|
. ' has the more specific type \'' .
|
|
|
|
$implementer_method_storage_param_type->getId() . '\', expecting \'' .
|
|
|
|
$guide_method_storage_param_type->getId() . '\' as defined by ' .
|
|
|
|
$cased_guide_method_id,
|
|
|
|
$implementer_method_storage->params[$i]->location
|
2019-12-20 20:37:36 +01:00
|
|
|
?: $code_location
|
2019-05-14 21:44:46 +02:00
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-14 22:15:31 +02:00
|
|
|
if (TypeAnalyzer::isContainedBy(
|
|
|
|
$codebase,
|
|
|
|
$implementer_method_storage_param_type,
|
|
|
|
$guide_method_storage_param_type,
|
|
|
|
!$guide_classlike_storage->user_defined,
|
|
|
|
!$guide_classlike_storage->user_defined
|
2019-05-14 21:44:46 +02:00
|
|
|
)) {
|
2019-05-14 22:15:31 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MoreSpecificImplementedParamType(
|
|
|
|
'Argument ' . ($i + 1) . ' of ' . $cased_implementer_method_id
|
|
|
|
. ' has the more specific type \'' .
|
|
|
|
$implementer_method_storage_param_type->getId() . '\', expecting \'' .
|
|
|
|
$guide_method_storage_param_type->getId() . '\' as defined by ' .
|
|
|
|
$cased_guide_method_id,
|
|
|
|
$implementer_method_storage->params[$i]->location
|
2019-12-20 20:37:36 +01:00
|
|
|
?: $code_location
|
2019-05-14 22:15:31 +02:00
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImplementedParamTypeMismatch(
|
|
|
|
'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 ' .
|
|
|
|
$cased_guide_method_id,
|
|
|
|
$implementer_method_storage->params[$i]->location
|
2019-12-20 20:37:36 +01:00
|
|
|
?: $code_location
|
2019-05-14 22:15:31 +02:00
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-14 21:44:46 +02:00
|
|
|
}
|
2018-02-22 00:59:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-12-20 20:37:36 +01:00
|
|
|
&& $config->isInProjectDirs(
|
|
|
|
$implementer_method_storage->params[$i]->location->file_path
|
|
|
|
)
|
|
|
|
? $implementer_method_storage->params[$i]->location
|
|
|
|
: $code_location
|
2018-02-22 00:59:31 +01:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($guide_classlike_storage->user_defined
|
2018-12-13 04:35:27 +01:00
|
|
|
&& ($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(
|
2018-07-14 02:17:29 +02:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}
|
2018-04-21 19:36:18 +02:00
|
|
|
|
|
|
|
/**
|
2019-12-20 20:37:36 +01:00
|
|
|
* @param array<string, array<int|string, Type\Union>> $template_type_extends
|
|
|
|
*/
|
|
|
|
private static function transformTemplates(
|
|
|
|
array $template_type_extends,
|
|
|
|
string $base_class_name,
|
|
|
|
Type\Union $templated_type,
|
|
|
|
Codebase $codebase
|
|
|
|
) : void {
|
|
|
|
if (isset($template_type_extends[$base_class_name])) {
|
|
|
|
$map = $template_type_extends[$base_class_name];
|
|
|
|
|
|
|
|
$template_types = [];
|
|
|
|
|
|
|
|
foreach ($map as $key => $mapped_type) {
|
|
|
|
if (is_string($key)) {
|
|
|
|
$new_bases = [];
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($mapped_type->getAtomicTypes() as $mapped_atomic_type) {
|
2019-12-30 13:25:27 +01:00
|
|
|
if ($mapped_atomic_type instanceof Type\Atomic\TTemplateParam) {
|
2019-12-20 20:37:36 +01:00
|
|
|
$new_bases[] = $mapped_atomic_type->defining_class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($new_bases) {
|
|
|
|
$mapped_type = clone $mapped_type;
|
|
|
|
|
|
|
|
foreach ($new_bases as $new_base_class_name) {
|
|
|
|
self::transformTemplates(
|
|
|
|
$template_type_extends,
|
|
|
|
$new_base_class_name,
|
|
|
|
$mapped_type,
|
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$template_types[$key][$base_class_name] = [$mapped_type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$template_result = new \Psalm\Internal\Type\TemplateResult($template_types, []);
|
|
|
|
|
|
|
|
$templated_type->replaceTemplateTypesWithArgTypes(
|
|
|
|
$template_result->template_types,
|
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-21 22:01:10 +02:00
|
|
|
* Check that __clone, __construct, and __destruct do not have a return type
|
|
|
|
* hint in their signature.
|
|
|
|
*
|
2018-04-21 19:36:18 +02:00
|
|
|
* @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;
|
2018-04-21 22:01:10 +02:00
|
|
|
$methodsOfInterest = ['__clone', '__construct', '__destruct'];
|
|
|
|
if (in_array($cased_method_name, $methodsOfInterest)) {
|
2018-04-21 19:36:18 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MethodSignatureMustOmitReturnType(
|
|
|
|
'Method ' . $cased_method_name . ' must not declare a return type',
|
|
|
|
$code_location
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-04-21 22:01:10 +02:00
|
|
|
|
|
|
|
return null;
|
2018-04-21 19:36:18 +02:00
|
|
|
}
|
2016-02-04 15:22:46 +01:00
|
|
|
}
|