2016-01-08 00:28:27 +01:00
|
|
|
<?php
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
use PhpParser;
|
2017-07-25 22:11:02 +02:00
|
|
|
use Psalm\Aliases;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2017-01-20 05:45:21 +01:00
|
|
|
use Psalm\Issue\DuplicateClass;
|
2017-01-02 01:09:17 +01:00
|
|
|
use Psalm\Issue\InaccessibleMethod;
|
|
|
|
use Psalm\Issue\InaccessibleProperty;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Issue\InvalidClass;
|
2017-11-26 22:03:17 +01:00
|
|
|
use Psalm\Issue\InvalidReturnType;
|
2017-01-27 07:23:12 +01:00
|
|
|
use Psalm\Issue\MissingConstructor;
|
2016-10-31 20:17:54 +01:00
|
|
|
use Psalm\Issue\MissingPropertyType;
|
2017-01-27 07:23:12 +01:00
|
|
|
use Psalm\Issue\PropertyNotSetInConstructor;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\UndefinedClass;
|
|
|
|
use Psalm\Issue\UndefinedTrait;
|
2017-06-30 16:24:47 +02:00
|
|
|
use Psalm\Issue\UnimplementedAbstractMethod;
|
2016-10-30 16:14:36 +01:00
|
|
|
use Psalm\Issue\UnimplementedInterfaceMethod;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\IssueBuffer;
|
2017-08-22 18:38:38 +02:00
|
|
|
use Psalm\Provider\FileReferenceProvider;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\StatementsSource;
|
2016-12-30 18:41:14 +01:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2017-01-02 01:09:17 +01:00
|
|
|
use Psalm\Storage\PropertyStorage;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Type;
|
2016-04-17 17:22:18 +02:00
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionMethod;
|
2016-08-05 21:11:20 +02:00
|
|
|
use ReflectionProperty;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
abstract class ClassLikeChecker extends SourceChecker implements StatementsSource
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2017-01-02 01:09:17 +01:00
|
|
|
const VISIBILITY_PUBLIC = 1;
|
|
|
|
const VISIBILITY_PROTECTED = 2;
|
|
|
|
const VISIBILITY_PRIVATE = 3;
|
|
|
|
|
2016-10-31 20:42:20 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static $SPECIAL_TYPES = [
|
|
|
|
'int' => 'int',
|
|
|
|
'string' => 'stirng',
|
|
|
|
'float' => 'float',
|
|
|
|
'bool' => 'bool',
|
|
|
|
'false' => 'false',
|
|
|
|
'object' => 'object',
|
|
|
|
'empty' => 'empty',
|
|
|
|
'callable' => 'callable',
|
|
|
|
'array' => 'array',
|
|
|
|
'iterable' => 'iterable',
|
|
|
|
'null' => 'null',
|
|
|
|
'mixed' => 'mixed',
|
2016-11-02 07:29:00 +01:00
|
|
|
];
|
2016-07-25 06:31:29 +02:00
|
|
|
|
2017-09-11 17:52:34 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $GETTYPE_TYPES = [
|
|
|
|
'boolean' => true,
|
|
|
|
'integer' => true,
|
|
|
|
'double' => true,
|
|
|
|
'string' => true,
|
|
|
|
'array' => true,
|
|
|
|
'object' => true,
|
|
|
|
'resource' => true,
|
|
|
|
'NULL' => true,
|
|
|
|
'unknown type' => true,
|
|
|
|
];
|
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* @var PhpParser\Node\Stmt\ClassLike
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $class;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
2016-11-21 04:02:26 +01:00
|
|
|
/**
|
|
|
|
* @var StatementsSource
|
|
|
|
*/
|
|
|
|
protected $source;
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
/** @var FileChecker */
|
|
|
|
public $file_checker;
|
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-11-07 23:29:51 +01:00
|
|
|
protected $fq_class_name;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2016-08-14 00:54:49 +02:00
|
|
|
protected $has_custom_get = false;
|
2016-09-09 15:13:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The parent class
|
|
|
|
*
|
2016-10-03 17:38:59 +02:00
|
|
|
* @var string|null
|
2016-09-09 15:13:41 +02:00
|
|
|
*/
|
2017-01-07 20:35:07 +01:00
|
|
|
protected $parent_fq_class_name;
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-07-25 00:02:03 +02:00
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* @var array<string, MethodChecker>
|
2016-07-25 00:02:03 +02:00
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
protected $method_checkers = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, MethodChecker>
|
|
|
|
*/
|
|
|
|
protected $property_types = [];
|
2016-07-25 00:02:03 +02:00
|
|
|
|
2016-12-12 20:29:58 +01:00
|
|
|
/**
|
2016-12-30 18:41:14 +01:00
|
|
|
* @var array<string, array<string, string>>|null
|
2016-12-12 20:29:58 +01:00
|
|
|
*/
|
2016-12-30 18:41:14 +01:00
|
|
|
protected static $property_map;
|
2016-12-12 20:29:58 +01:00
|
|
|
|
2016-09-09 15:13:41 +02:00
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* A lookup table of cached TraitCheckers
|
2016-09-09 15:13:41 +02:00
|
|
|
*
|
2017-01-02 21:31:18 +01:00
|
|
|
* @var array<string, TraitChecker>
|
|
|
|
*/
|
|
|
|
public static $trait_checkers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A lookup table of cached ClassCheckers
|
|
|
|
*
|
|
|
|
* @var array<string, ClassChecker>
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-12-30 18:41:14 +01:00
|
|
|
public static $class_checkers;
|
2016-10-05 01:23:38 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-12-30 18:41:14 +01:00
|
|
|
* @var array<string, array<string, string>>
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-12-30 18:41:14 +01:00
|
|
|
public static $file_classes = [];
|
2016-12-12 19:50:46 +01:00
|
|
|
|
2017-02-02 00:27:24 +01:00
|
|
|
/**
|
|
|
|
* @var PhpParser\Node\Stmt[]
|
|
|
|
*/
|
|
|
|
protected $leftover_stmts = [];
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
/** @var ClassLikeStorage */
|
|
|
|
protected $storage;
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\ClassLike $class
|
|
|
|
* @param StatementsSource $source
|
2016-11-07 23:29:51 +01:00
|
|
|
* @param string $fq_class_name
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-11-07 23:29:51 +01:00
|
|
|
public function __construct(PhpParser\Node\Stmt\ClassLike $class, StatementsSource $source, $fq_class_name)
|
2016-01-08 00:28:27 +01:00
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
$this->class = $class;
|
2016-11-13 00:51:48 +01:00
|
|
|
$this->source = $source;
|
2017-03-20 04:30:20 +01:00
|
|
|
$this->file_checker = $source->getFileChecker();
|
2016-11-07 23:29:51 +01:00
|
|
|
$this->fq_class_name = $fq_class_name;
|
2016-01-08 00:28:27 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$this->storage = $this->file_checker->project_checker->classlike_storage_provider->get($fq_class_name);
|
2017-02-22 23:26:20 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if ($this->storage->location) {
|
|
|
|
$storage_file_path = $this->storage->location->file_path;
|
2017-07-25 22:11:02 +02:00
|
|
|
$source_file_path = $this->source->getCheckedFilePath();
|
2016-12-30 18:41:14 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (!Config::getInstance()->use_case_sensitive_file_names) {
|
|
|
|
$storage_file_path = strtolower($storage_file_path);
|
|
|
|
$source_file_path = strtolower($source_file_path);
|
2017-02-12 19:25:59 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($storage_file_path !== $source_file_path ||
|
2017-07-29 21:05:06 +02:00
|
|
|
$this->storage->location->getLineNumber() !== $class->getLine()
|
2017-01-02 21:31:18 +01:00
|
|
|
) {
|
2017-02-10 02:35:17 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2017-07-25 22:11:02 +02:00
|
|
|
new DuplicateClass(
|
|
|
|
'Class ' . $fq_class_name . ' has already been defined at ' .
|
2017-07-29 21:05:06 +02:00
|
|
|
$storage_file_path . ':' . $this->storage->location->getLineNumber(),
|
2017-07-25 22:11:02 +02:00
|
|
|
new \Psalm\CodeLocation($this, $class, null, true)
|
2017-02-10 02:35:17 +01:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-01-07 21:57:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Context|null $class_context
|
|
|
|
* @param Context|null $global_context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-07 21:57:25 +01:00
|
|
|
* @return null|false
|
|
|
|
*/
|
|
|
|
public function analyze(
|
|
|
|
Context $class_context = null,
|
2017-09-16 18:45:11 +02:00
|
|
|
Context $global_context = null
|
2017-01-07 21:57:25 +01:00
|
|
|
) {
|
|
|
|
$fq_class_name = $class_context && $class_context->self ? $class_context->self : $this->fq_class_name;
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$storage = $this->storage;
|
|
|
|
|
|
|
|
$project_checker = $this->file_checker->project_checker;
|
|
|
|
|
|
|
|
$classlike_storage_provider = $project_checker->classlike_storage_provider;
|
2017-01-07 21:57:25 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($this->class instanceof PhpParser\Node\Stmt\Class_) {
|
|
|
|
if ($this->class->extends) {
|
|
|
|
if (!$this->parent_fq_class_name) {
|
2017-09-20 14:43:54 +02:00
|
|
|
throw new \UnexpectedValueException('Parent class should be filled in for ' . $fq_class_name);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$parent_reference_location = new CodeLocation($this, $this->class->extends);
|
|
|
|
|
|
|
|
if (self::checkFullyQualifiedClassLikeName(
|
2017-07-29 21:05:06 +02:00
|
|
|
$this->getFileChecker()->project_checker,
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->parent_fq_class_name,
|
|
|
|
$parent_reference_location,
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-11 01:36:17 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
foreach ($this->class->implements as $interface_name) {
|
|
|
|
$fq_interface_name = self::getFQCLNFromNameObject(
|
|
|
|
$interface_name,
|
|
|
|
$this->source->getAliases()
|
|
|
|
);
|
2017-03-11 01:36:17 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$interface_location = new CodeLocation($this, $interface_name);
|
|
|
|
|
|
|
|
if (self::checkFullyQualifiedClassLikeName(
|
2017-07-29 21:05:06 +02:00
|
|
|
$this->getFileChecker()->project_checker,
|
2017-07-25 22:11:02 +02:00
|
|
|
$fq_interface_name,
|
|
|
|
$interface_location,
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-11 01:36:17 +01:00
|
|
|
}
|
|
|
|
} elseif ($this->class instanceof PhpParser\Node\Stmt\Interface_ && $this->class->extends) {
|
|
|
|
foreach ($this->class->extends as $extended_interface) {
|
|
|
|
$extended_interface_name = self::getFQCLNFromNameObject(
|
|
|
|
$extended_interface,
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->getAliases()
|
2017-03-11 01:36:17 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$parent_reference_location = new CodeLocation($this, $extended_interface);
|
|
|
|
|
|
|
|
if (!ClassLikeChecker::classOrInterfaceExists(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-03-11 01:36:17 +01:00
|
|
|
$extended_interface_name,
|
|
|
|
$parent_reference_location
|
|
|
|
)) {
|
|
|
|
// we should not normally get here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 21:36:06 +01:00
|
|
|
}
|
|
|
|
|
2017-01-07 21:57:25 +01:00
|
|
|
if ($this instanceof ClassChecker && $this->class instanceof PhpParser\Node\Stmt\Class_) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_interfaces = $storage->class_implements;
|
2017-01-12 03:37:53 +01:00
|
|
|
|
2017-01-16 01:02:36 +01:00
|
|
|
if (!$this->class->isAbstract()) {
|
2017-02-11 01:10:13 +01:00
|
|
|
foreach ($class_interfaces as $interface_name) {
|
2017-07-29 21:05:06 +02:00
|
|
|
try {
|
|
|
|
$interface_storage = $classlike_storage_provider->get($interface_name);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
2017-01-16 01:02:36 +01:00
|
|
|
continue;
|
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-01-16 01:02:36 +01:00
|
|
|
$storage->public_class_constants += $interface_storage->public_class_constants;
|
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
$code_location = new CodeLocation(
|
|
|
|
$this,
|
|
|
|
$this->class,
|
|
|
|
$class_context ? $class_context->include_location : null,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($interface_storage->methods as $method_name => $interface_method_storage) {
|
|
|
|
if ($interface_method_storage->visibility === self::VISIBILITY_PUBLIC) {
|
|
|
|
$implementer_declaring_method_id = MethodChecker::getDeclaringMethodId(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-11-26 22:03:17 +01:00
|
|
|
$this->fq_class_name . '::' . $method_name
|
2017-07-29 21:05:06 +02:00
|
|
|
);
|
2017-01-16 01:02:36 +01:00
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
$implementer_method_storage = $implementer_declaring_method_id
|
|
|
|
? MethodChecker::getStorage($project_checker, $implementer_declaring_method_id)
|
2017-01-16 01:02:36 +01:00
|
|
|
: null;
|
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
$cased_interface_method_id = $interface_storage->name . '::' .
|
|
|
|
$interface_method_storage->cased_name;
|
2017-01-16 01:02:36 +01:00
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
if (!$implementer_method_storage) {
|
2017-01-16 01:02:36 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnimplementedInterfaceMethod(
|
2017-11-26 22:03:17 +01:00
|
|
|
'Method ' . $method_name . ' is not defined on class ' .
|
|
|
|
$storage->name,
|
|
|
|
$code_location
|
2017-01-16 01:02:36 +01:00
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2017-11-26 22:03:17 +01:00
|
|
|
}
|
2017-01-16 01:02:36 +01:00
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
if ($implementer_method_storage->visibility !== self::VISIBILITY_PUBLIC) {
|
2017-01-16 01:02:36 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InaccessibleMethod(
|
2017-11-26 22:03:17 +01:00
|
|
|
'Interface-defined method ' . $implementer_method_storage->cased_name
|
|
|
|
. ' must be public in ' . $storage->name,
|
|
|
|
$code_location
|
2017-01-16 01:02:36 +01:00
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2017-01-02 01:09:17 +01:00
|
|
|
}
|
2017-11-26 22:03:17 +01:00
|
|
|
|
|
|
|
FunctionLikeChecker::compareMethods(
|
|
|
|
$project_checker,
|
|
|
|
$storage,
|
|
|
|
$interface_storage,
|
|
|
|
$implementer_method_storage,
|
|
|
|
$interface_method_storage,
|
|
|
|
$code_location,
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
);
|
2016-10-15 06:12:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-16 00:01:04 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if (!$class_context) {
|
2017-01-17 00:33:04 +01:00
|
|
|
$class_context = new Context($this->fq_class_name);
|
2017-02-27 05:09:18 +01:00
|
|
|
$class_context->collect_references = $this->getFileChecker()->project_checker->collect_references;
|
2017-01-07 20:35:07 +01:00
|
|
|
$class_context->parent = $this->parent_fq_class_name;
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 00:27:24 +01:00
|
|
|
if ($this->leftover_stmts) {
|
|
|
|
(new StatementsChecker($this))->analyze($this->leftover_stmts, $class_context);
|
|
|
|
}
|
|
|
|
|
2017-06-30 16:24:47 +02:00
|
|
|
if (!$storage->abstract) {
|
|
|
|
foreach ($storage->declaring_method_ids as $method_name => $declaring_method_id) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$method_storage = MethodChecker::getStorage($project_checker, $declaring_method_id);
|
2017-06-30 16:24:47 +02:00
|
|
|
|
|
|
|
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,
|
|
|
|
$this->class,
|
|
|
|
$class_context->include_location,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 01:57:59 +01:00
|
|
|
foreach ($storage->appearing_property_ids as $property_name => $appearing_property_id) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$property_class_name = self::getDeclaringClassForProperty($project_checker, $appearing_property_id);
|
|
|
|
$property_class_storage = $classlike_storage_provider->get((string)$property_class_name);
|
|
|
|
$property_class_name = self::getDeclaringClassForProperty($project_checker, $appearing_property_id);
|
2017-01-19 19:11:45 +01:00
|
|
|
|
2017-01-28 01:57:59 +01:00
|
|
|
$property = $property_class_storage->properties[$property_name];
|
2017-01-19 19:11:45 +01:00
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
if ($property->type) {
|
|
|
|
$property_type = clone $property->type;
|
|
|
|
|
|
|
|
if (!$property_type->isMixed() &&
|
|
|
|
!$property->has_default &&
|
|
|
|
!$property->type->isNullable()
|
|
|
|
) {
|
|
|
|
$property_type->initialized = false;
|
|
|
|
}
|
2017-02-12 20:10:25 +01:00
|
|
|
|
|
|
|
if ($storage->template_types) {
|
|
|
|
$generic_types = [];
|
|
|
|
$property_type->replaceTemplateTypes($storage->template_types, $generic_types);
|
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
} else {
|
|
|
|
$property_type = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
2017-03-02 04:27:52 +01:00
|
|
|
if ($property->type_location && !$property_type->isMixed()) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$fleshed_out_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$property_type,
|
|
|
|
$this->fq_class_name,
|
|
|
|
null
|
|
|
|
);
|
2017-03-02 18:19:18 +01:00
|
|
|
$fleshed_out_type->check($this, $property->type_location, $this->getSuppressedIssues(), [], false);
|
2017-03-02 04:27:52 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if ($property->is_static) {
|
2017-01-07 20:35:07 +01:00
|
|
|
$property_id = $this->fq_class_name . '::$' . $property_name;
|
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
$class_context->vars_in_scope[$property_id] = $property_type;
|
2017-01-02 21:31:18 +01:00
|
|
|
} else {
|
2017-01-27 07:23:12 +01:00
|
|
|
$class_context->vars_in_scope['$this->' . $property_name] = $property_type;
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
$constructor_checker = null;
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
foreach ($this->class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
2017-01-27 07:23:12 +01:00
|
|
|
$method_checker = $this->analyzeClassMethod(
|
|
|
|
$stmt,
|
|
|
|
$this,
|
|
|
|
$class_context,
|
2017-09-16 18:45:11 +02:00
|
|
|
$global_context
|
2017-01-27 07:23:12 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($stmt->name === '__construct') {
|
|
|
|
$constructor_checker = $method_checker;
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
|
2017-06-21 20:22:52 +02:00
|
|
|
$previous_context_include_location = $class_context->include_location;
|
2017-01-12 03:37:53 +01:00
|
|
|
foreach ($stmt->traits as $trait) {
|
2017-06-21 20:22:52 +02:00
|
|
|
$class_context->include_location = new CodeLocation($this, $trait, null, true);
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
$fq_trait_name = self::getFQCLNFromNameObject(
|
|
|
|
$trait,
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->source->getAliases()
|
2017-01-12 03:37:53 +01:00
|
|
|
);
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (!TraitChecker::traitExists($fq_trait_name, $this->getFileChecker())) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedTrait(
|
|
|
|
'Trait ' . $fq_trait_name . ' does not exist',
|
|
|
|
new CodeLocation($this, $trait)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!TraitChecker::hasCorrectCase($fq_trait_name, $this->getFileChecker())) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedTrait(
|
|
|
|
'Trait ' . $fq_trait_name . ' has wrong casing',
|
|
|
|
new CodeLocation($this, $trait)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-22 07:25:51 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (!isset(self::$trait_checkers[strtolower($fq_trait_name)])) {
|
2017-09-20 14:43:54 +02:00
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Expecting trait statements to exist for ' . $fq_trait_name
|
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$trait_checker = self::$trait_checkers[strtolower($fq_trait_name)];
|
|
|
|
|
|
|
|
foreach ($trait_checker->class->stmts as $trait_stmt) {
|
|
|
|
if ($trait_stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
|
|
|
|
$trait_method_checker = $this->analyzeClassMethod(
|
|
|
|
$trait_stmt,
|
|
|
|
$trait_checker,
|
|
|
|
$class_context,
|
|
|
|
$global_context
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($trait_stmt->name === '__construct') {
|
|
|
|
$constructor_checker = $trait_method_checker;
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-09 03:19:16 +02:00
|
|
|
|
2017-06-21 20:22:52 +02:00
|
|
|
$class_context->include_location = $previous_context_include_location;
|
2017-01-27 07:23:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2017-07-09 04:19:53 +02:00
|
|
|
if ($this->class instanceof PhpParser\Node\Stmt\Class_
|
|
|
|
&& $config->reportIssueInFile('PropertyNotSetInConstructor', $this->getFilePath())
|
|
|
|
) {
|
2017-01-27 16:28:21 +01:00
|
|
|
$uninitialized_variables = [];
|
|
|
|
$uninitialized_properties = [];
|
|
|
|
|
|
|
|
foreach ($storage->appearing_property_ids as $property_name => $appearing_property_id) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$property_class_name = self::getDeclaringClassForProperty($project_checker, $appearing_property_id);
|
|
|
|
$property_class_storage = $classlike_storage_provider->get((string)$property_class_name);
|
|
|
|
$property_class_name = self::getDeclaringClassForProperty($project_checker, $appearing_property_id);
|
2017-01-27 16:28:21 +01:00
|
|
|
|
|
|
|
$property = $property_class_storage->properties[$property_name];
|
2017-01-27 07:23:12 +01:00
|
|
|
|
2017-09-20 17:22:06 +02:00
|
|
|
if ($property->has_default || $property->is_static || !$property->type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($property->type->isMixed() || $property->type->isNullable()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-09 03:19:16 +02:00
|
|
|
$constructor_class_storage = null;
|
|
|
|
|
2017-09-18 22:10:08 +02:00
|
|
|
if (isset($storage->methods['__construct'])) {
|
|
|
|
$constructor_class_storage = $storage;
|
|
|
|
} elseif (isset($property_class_storage->methods['__construct'])
|
2017-07-09 03:19:16 +02:00
|
|
|
&& $property_class_storage !== $storage
|
|
|
|
) {
|
|
|
|
$constructor_class_storage = $property_class_storage;
|
|
|
|
} elseif (!empty($property_class_storage->overridden_method_ids['__construct'])) {
|
|
|
|
list($construct_fqcln) =
|
|
|
|
explode('::', $property_class_storage->overridden_method_ids['__construct'][0]);
|
2017-07-29 21:05:06 +02:00
|
|
|
$constructor_class_storage = $classlike_storage_provider->get($construct_fqcln);
|
2017-07-09 03:19:16 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 22:10:08 +02:00
|
|
|
if ($constructor_class_storage
|
|
|
|
&& $constructor_class_storage->all_properties_set_in_constructor
|
|
|
|
&& $constructor_class_storage->methods['__construct']->visibility !== self::VISIBILITY_PRIVATE
|
2017-01-27 07:23:12 +01:00
|
|
|
) {
|
2017-09-18 22:10:08 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$uninitialized_variables[] = '$this->' . $property_name;
|
2017-10-07 16:22:52 +02:00
|
|
|
$uninitialized_properties[$property_name] = $property;
|
2017-01-27 07:23:12 +01:00
|
|
|
}
|
|
|
|
|
2017-07-09 04:19:53 +02:00
|
|
|
if ($uninitialized_properties) {
|
2017-07-09 03:19:16 +02:00
|
|
|
if (!$storage->abstract
|
|
|
|
&& !$constructor_checker
|
|
|
|
&& isset($storage->declaring_method_ids['__construct'])
|
2017-07-09 04:19:53 +02:00
|
|
|
&& $this->class->extends
|
2017-07-09 03:19:16 +02:00
|
|
|
) {
|
|
|
|
list($construct_fqcln) = explode('::', $storage->declaring_method_ids['__construct']);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$constructor_class_storage = $classlike_storage_provider->get($construct_fqcln);
|
2017-07-09 03:19:16 +02:00
|
|
|
|
|
|
|
// ignore oldstyle constructors and classes without any declared properties
|
|
|
|
if (isset($constructor_class_storage->methods['__construct'])) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$constructor_storage = $constructor_class_storage->methods['__construct'];
|
2017-07-09 03:19:16 +02:00
|
|
|
|
|
|
|
$fake_constructor_params = array_map(
|
|
|
|
/** @return PhpParser\Node\Param */
|
|
|
|
function (\Psalm\FunctionLikeParameter $param) {
|
2017-09-02 17:18:56 +02:00
|
|
|
$fake_param = (new PhpParser\Builder\Param($param->name));
|
|
|
|
if ($param->signature_type) {
|
|
|
|
$fake_param->setTypehint((string)$param->signature_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fake_param->getNode();
|
2017-07-09 03:19:16 +02:00
|
|
|
},
|
|
|
|
$constructor_storage->params
|
|
|
|
);
|
|
|
|
|
|
|
|
$fake_constructor_stmt_args = array_map(
|
|
|
|
/** @return PhpParser\Node\Arg */
|
|
|
|
function (\Psalm\FunctionLikeParameter $param) {
|
|
|
|
return new PhpParser\Node\Arg(new PhpParser\Node\Expr\Variable($param->name));
|
|
|
|
},
|
|
|
|
$constructor_storage->params
|
|
|
|
);
|
|
|
|
|
|
|
|
$fake_constructor_stmts = [
|
|
|
|
new PhpParser\Node\Expr\StaticCall(
|
|
|
|
new PhpParser\Node\Name(['parent']),
|
|
|
|
'__construct',
|
2017-07-09 04:06:49 +02:00
|
|
|
$fake_constructor_stmt_args,
|
|
|
|
[
|
2017-07-09 04:11:55 +02:00
|
|
|
'line' => $this->class->extends->getLine(),
|
|
|
|
'startFilePos' => $this->class->extends->getAttribute('startFilePos'),
|
|
|
|
'endFilePos' => $this->class->extends->getAttribute('endFilePos'),
|
2017-07-09 04:06:49 +02:00
|
|
|
]
|
2017-07-09 03:19:16 +02:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
$fake_stmt = new PhpParser\Node\Stmt\ClassMethod(
|
|
|
|
'__construct',
|
|
|
|
[
|
|
|
|
'type' => PhpParser\Node\Stmt\Class_::MODIFIER_PUBLIC,
|
|
|
|
'params' => $fake_constructor_params,
|
|
|
|
'stmts' => $fake_constructor_stmts,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$constructor_checker = $this->analyzeClassMethod(
|
|
|
|
$fake_stmt,
|
|
|
|
$this,
|
|
|
|
$class_context,
|
2017-09-16 18:45:11 +02:00
|
|
|
$global_context
|
2017-07-09 03:19:16 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 07:25:41 +02:00
|
|
|
if ($constructor_checker) {
|
2017-01-27 07:23:12 +01:00
|
|
|
$method_context = clone $class_context;
|
|
|
|
$method_context->collect_initializations = true;
|
|
|
|
$method_context->vars_in_scope['$this'] = Type::parseString($fq_class_name);
|
2017-02-08 08:23:17 +01:00
|
|
|
$method_context->vars_possibly_in_scope['$this'] = true;
|
2017-01-27 07:23:12 +01:00
|
|
|
|
2017-02-12 19:16:40 +01:00
|
|
|
$constructor_checker->analyze($method_context, $global_context, true);
|
2017-01-27 07:23:12 +01:00
|
|
|
|
2017-07-09 03:19:16 +02:00
|
|
|
$all_properties_set_in_constructor = true;
|
|
|
|
|
2017-01-27 16:28:21 +01:00
|
|
|
foreach ($uninitialized_properties as $property_name => $property) {
|
2017-01-27 07:23:12 +01:00
|
|
|
if (!isset($method_context->vars_in_scope['$this->' . $property_name])) {
|
|
|
|
throw new \UnexpectedValueException('$this->' . $property_name . ' should be in scope');
|
|
|
|
}
|
|
|
|
|
|
|
|
$end_type = $method_context->vars_in_scope['$this->' . $property_name];
|
|
|
|
|
2017-07-09 03:19:16 +02:00
|
|
|
if (!$end_type->initialized) {
|
|
|
|
$all_properties_set_in_constructor = false;
|
|
|
|
}
|
|
|
|
|
2017-07-10 00:37:30 +02:00
|
|
|
if (!$end_type->initialized && $property->location) {
|
2017-01-27 07:23:12 +01:00
|
|
|
$property_id = $this->fq_class_name . '::$' . $property_name;
|
|
|
|
|
2017-07-10 00:33:56 +02:00
|
|
|
if (!$config->reportIssueInFile(
|
|
|
|
'PropertyNotSetInConstructor',
|
|
|
|
$property->location->file_path
|
2017-07-10 00:37:30 +02:00
|
|
|
) && $this->class->extends
|
|
|
|
) {
|
2017-07-10 00:33:56 +02:00
|
|
|
$error_location = new CodeLocation($this, $this->class->extends);
|
|
|
|
} else {
|
|
|
|
$error_location = $property->location;
|
|
|
|
}
|
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PropertyNotSetInConstructor(
|
|
|
|
'Property ' . $property_id . ' is not defined in constructor of ' .
|
|
|
|
$this->fq_class_name . ' or in any private methods called in the constructor',
|
2017-07-10 00:33:56 +02:00
|
|
|
$error_location
|
2017-01-27 07:23:12 +01:00
|
|
|
),
|
2017-09-13 17:32:13 +02:00
|
|
|
array_merge($this->source->getSuppressedIssues(), $storage->suppressed_issues)
|
2017-01-27 07:23:12 +01:00
|
|
|
)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-09 03:19:16 +02:00
|
|
|
|
|
|
|
$storage->all_properties_set_in_constructor = $all_properties_set_in_constructor;
|
|
|
|
} elseif (!$storage->abstract) {
|
2017-01-27 16:28:21 +01:00
|
|
|
$first_uninitialized_property = array_shift($uninitialized_properties);
|
|
|
|
|
2017-07-10 00:37:30 +02:00
|
|
|
if ($first_uninitialized_property->location) {
|
2017-01-27 16:28:21 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingConstructor(
|
2017-01-29 06:34:42 +01:00
|
|
|
$fq_class_name . ' has an uninitialized variable ' . $uninitialized_variables[0] .
|
2017-01-27 16:28:21 +01:00
|
|
|
', but no constructor',
|
|
|
|
$first_uninitialized_property->location
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
|
2017-04-15 05:09:34 +02:00
|
|
|
foreach ($this->class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Property) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$this->checkForMissingPropertyType($project_checker, $stmt);
|
2017-04-15 06:04:03 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
|
|
|
|
foreach ($stmt->traits as $trait) {
|
|
|
|
$fq_trait_name = self::getFQCLNFromNameObject(
|
|
|
|
$trait,
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->source->getAliases()
|
2017-04-15 06:04:03 +02:00
|
|
|
);
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (!isset(self::$trait_checkers[strtolower($fq_trait_name)])) {
|
2017-09-20 14:43:54 +02:00
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Expecting trait statements to exist for ' . $fq_trait_name
|
|
|
|
);
|
2017-04-15 06:04:03 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$trait_checker = self::$trait_checkers[strtolower($fq_trait_name)];
|
2017-04-15 06:04:03 +02:00
|
|
|
|
|
|
|
foreach ($trait_checker->class->stmts as $trait_stmt) {
|
|
|
|
if ($trait_stmt instanceof PhpParser\Node\Stmt\Property) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$this->checkForMissingPropertyType($project_checker, $trait_stmt);
|
2017-04-15 06:04:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-15 05:09:34 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\ClassMethod $stmt
|
|
|
|
* @param StatementsSource $source
|
|
|
|
* @param Context $class_context
|
|
|
|
* @param Context|null $global_context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-27 07:23:12 +01:00
|
|
|
* @return MethodChecker|null
|
2017-01-12 03:37:53 +01:00
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private function analyzeClassMethod(
|
2017-01-12 03:37:53 +01:00
|
|
|
PhpParser\Node\Stmt\ClassMethod $stmt,
|
|
|
|
StatementsSource $source,
|
|
|
|
Context $class_context,
|
2017-09-16 18:45:11 +02:00
|
|
|
Context $global_context = null
|
2017-01-12 03:37:53 +01:00
|
|
|
) {
|
|
|
|
$config = Config::getInstance();
|
2017-01-08 18:55:32 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
$method_checker = new MethodChecker($stmt, $source);
|
2017-01-06 07:07:11 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
$actual_method_id = (string)$method_checker->getMethodId();
|
2017-01-06 07:07:11 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $source->getFileChecker()->project_checker;
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
if ($class_context->self && $class_context->self !== $source->getFQCLN()) {
|
|
|
|
$analyzed_method_id = (string)$method_checker->getMethodId($class_context->self);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId($project_checker, $analyzed_method_id);
|
2017-01-12 03:37:53 +01:00
|
|
|
|
|
|
|
if ($actual_method_id !== $declaring_method_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$method_checker->analyze(
|
|
|
|
clone $class_context,
|
|
|
|
$global_context ? clone $global_context : null
|
|
|
|
);
|
|
|
|
|
2017-07-09 03:19:16 +02:00
|
|
|
if ($stmt->name !== '__construct' && $config->reportIssueInFile('InvalidReturnType', $source->getFilePath())) {
|
2017-01-16 04:09:32 +01:00
|
|
|
$return_type_location = null;
|
2017-01-12 03:37:53 +01:00
|
|
|
$secondary_return_type_location = null;
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$actual_method_storage = MethodChecker::getStorage($project_checker, $actual_method_id);
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
|
|
if (!$actual_method_storage->has_template_return_type) {
|
|
|
|
if ($actual_method_id) {
|
|
|
|
$return_type_location = MethodChecker::getMethodReturnTypeLocation(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-02-10 02:35:17 +01:00
|
|
|
$actual_method_id,
|
|
|
|
$secondary_return_type_location
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$return_type = MethodChecker::getMethodReturnType($project_checker, $actual_method_id);
|
2017-02-10 02:35:17 +01:00
|
|
|
|
|
|
|
$method_checker->verifyReturnType(
|
2017-09-16 18:45:11 +02:00
|
|
|
$project_checker,
|
2017-03-01 17:56:36 +01:00
|
|
|
$return_type ? clone $return_type : null,
|
2017-02-10 02:35:17 +01:00
|
|
|
$class_context->self,
|
|
|
|
$return_type_location,
|
2017-01-16 04:09:32 +01:00
|
|
|
$secondary_return_type_location
|
|
|
|
);
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
|
|
|
|
return $method_checker;
|
2017-01-12 03:37:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $method_name
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-12 03:37:53 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getMethodMutations(
|
|
|
|
$method_name,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $this->getFileChecker()->project_checker;
|
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
foreach ($this->class->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
|
|
|
|
strtolower($stmt->name) === strtolower($method_name)
|
|
|
|
) {
|
2017-01-12 06:54:41 +01:00
|
|
|
$project_checker = $this->getFileChecker()->project_checker;
|
|
|
|
|
|
|
|
$method_id = $this->fq_class_name . '::' . $stmt->name;
|
|
|
|
|
|
|
|
if ($project_checker->canCache() && isset($project_checker->method_checkers[$method_id])) {
|
|
|
|
$method_checker = $project_checker->method_checkers[$method_id];
|
|
|
|
} else {
|
|
|
|
$method_checker = new MethodChecker($stmt, $this);
|
|
|
|
|
|
|
|
if ($project_checker->canCache()) {
|
|
|
|
$project_checker->method_checkers[$method_id] = $method_checker;
|
|
|
|
}
|
|
|
|
}
|
2017-01-12 03:37:53 +01:00
|
|
|
|
|
|
|
$method_checker->analyze($context, null, true);
|
2017-01-06 07:07:11 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TraitUse) {
|
|
|
|
foreach ($stmt->traits as $trait) {
|
2017-01-12 03:37:53 +01:00
|
|
|
$fq_trait_name = self::getFQCLNFromNameObject(
|
2017-01-06 07:07:11 +01:00
|
|
|
$trait,
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->source->getAliases()
|
2017-01-06 07:07:11 +01:00
|
|
|
);
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (!isset(self::$trait_checkers[strtolower($fq_trait_name)])) {
|
2017-09-20 14:43:54 +02:00
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Expecting trait statements to exist for ' . $fq_trait_name
|
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$trait_checker = self::$trait_checkers[strtolower($fq_trait_name)];
|
2017-01-12 03:37:53 +01:00
|
|
|
|
|
|
|
foreach ($trait_checker->class->stmts as $trait_stmt) {
|
|
|
|
if ($trait_stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
|
|
|
|
strtolower($trait_stmt->name) === strtolower($method_name)
|
|
|
|
) {
|
|
|
|
$method_checker = new MethodChecker($trait_stmt, $trait_checker);
|
|
|
|
|
|
|
|
$actual_method_id = (string)$method_checker->getMethodId();
|
2017-01-06 07:07:11 +01:00
|
|
|
|
2017-01-12 03:37:53 +01:00
|
|
|
if ($context->self && $context->self !== $this->fq_class_name) {
|
|
|
|
$analyzed_method_id = (string)$method_checker->getMethodId($context->self);
|
2017-07-29 21:05:06 +02:00
|
|
|
$declaring_method_id = MethodChecker::getDeclaringMethodId(
|
|
|
|
$project_checker,
|
|
|
|
$analyzed_method_id
|
|
|
|
);
|
2017-01-12 03:37:53 +01:00
|
|
|
|
|
|
|
if ($actual_method_id !== $declaring_method_id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$method_checker->analyze($context, null, true);
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 07:07:11 +01:00
|
|
|
}
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 05:09:34 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Property $stmt
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-04-15 05:09:34 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
private function checkForMissingPropertyType(ProjectChecker $project_checker, PhpParser\Node\Stmt\Property $stmt)
|
2017-04-15 05:09:34 +02:00
|
|
|
{
|
|
|
|
$comment = $stmt->getDocComment();
|
|
|
|
|
|
|
|
if (!$comment || !$comment->getText()) {
|
|
|
|
$fq_class_name = $this->fq_class_name;
|
|
|
|
$property_name = $stmt->props[0]->name;
|
|
|
|
|
2017-04-15 06:04:03 +02:00
|
|
|
$declaring_property_class = ClassLikeChecker::getDeclaringClassForProperty(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-04-15 06:04:03 +02:00
|
|
|
$fq_class_name . '::$' . $property_name
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$declaring_property_class) {
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Cannot get declaring class for ' . $fq_class_name . '::$' . $property_name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fq_class_name = $declaring_property_class;
|
|
|
|
|
2017-04-15 05:09:34 +02:00
|
|
|
$message = 'Property ' . $fq_class_name . '::$' . $property_name . ' does not have a declared type';
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
2017-04-15 06:04:03 +02:00
|
|
|
|
|
|
|
$property_storage = $class_storage->properties[$property_name];
|
2017-04-15 05:09:34 +02:00
|
|
|
|
2017-04-15 06:45:43 +02:00
|
|
|
if ($property_storage->suggested_type && !$property_storage->suggested_type->isNull()) {
|
|
|
|
$message .= ' - consider ' . str_replace(
|
|
|
|
['<mixed, mixed>', '<empty, empty>'],
|
|
|
|
'',
|
|
|
|
(string)$property_storage->suggested_type
|
|
|
|
);
|
2017-04-15 05:09:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingPropertyType(
|
|
|
|
$message,
|
|
|
|
new CodeLocation($this, $stmt)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 06:31:29 +02:00
|
|
|
/**
|
2016-09-09 15:13:41 +02:00
|
|
|
* Check whether a class/interface exists
|
|
|
|
*
|
2017-07-29 21:05:06 +02:00
|
|
|
* @param string $fq_class_name
|
|
|
|
* @param ProjectChecker $project_checker
|
2017-02-27 22:38:43 +01:00
|
|
|
* @param CodeLocation $code_location
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-07-25 06:31:29 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static function classOrInterfaceExists(
|
2017-07-29 21:05:06 +02:00
|
|
|
ProjectChecker $project_checker,
|
2017-01-02 21:31:18 +01:00
|
|
|
$fq_class_name,
|
2017-02-27 22:38:43 +01:00
|
|
|
CodeLocation $code_location = null
|
2017-01-02 21:31:18 +01:00
|
|
|
) {
|
2017-07-29 21:05:06 +02:00
|
|
|
if (!ClassChecker::classExists($project_checker, $fq_class_name) &&
|
|
|
|
!InterfaceChecker::interfaceExists($project_checker, $fq_class_name)
|
2017-02-27 22:35:24 +01:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if ($project_checker->collect_references && $code_location) {
|
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
2017-02-28 00:24:20 +01:00
|
|
|
if ($class_storage->referencing_locations === null) {
|
|
|
|
$class_storage->referencing_locations = [];
|
|
|
|
}
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage->referencing_locations[$code_location->file_path][] = $code_location;
|
2017-02-27 22:35:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-07-25 05:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param string $fq_class_name
|
|
|
|
* @param string $possible_parent
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-07-25 05:38:52 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static function classExtendsOrImplements(
|
2017-07-29 21:05:06 +02:00
|
|
|
ProjectChecker $project_checker,
|
2017-01-02 21:31:18 +01:00
|
|
|
$fq_class_name,
|
|
|
|
$possible_parent
|
|
|
|
) {
|
2017-07-29 21:05:06 +02:00
|
|
|
return ClassChecker::classExtends($project_checker, $fq_class_name, $possible_parent) ||
|
|
|
|
ClassChecker::classImplements($project_checker, $fq_class_name, $possible_parent);
|
2016-07-25 05:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-07 23:29:51 +01:00
|
|
|
* @param string $fq_class_name
|
2017-07-29 21:05:06 +02:00
|
|
|
* @param ProjectChecker $project_checker
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param CodeLocation $code_location
|
2016-11-05 01:10:59 +01:00
|
|
|
* @param array<string> $suppressed_issues
|
2017-03-02 18:19:18 +01:00
|
|
|
* @param bool $inferred - whether or not the type was inferred
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-07-25 05:38:52 +02:00
|
|
|
* @return bool|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-11-08 01:16:51 +01:00
|
|
|
public static function checkFullyQualifiedClassLikeName(
|
2017-07-29 21:05:06 +02:00
|
|
|
ProjectChecker $project_checker,
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name,
|
2016-12-04 01:11:30 +01:00
|
|
|
CodeLocation $code_location,
|
2017-03-02 18:19:18 +01:00
|
|
|
array $suppressed_issues,
|
|
|
|
$inferred = true
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2016-11-07 23:29:51 +01:00
|
|
|
if (empty($fq_class_name)) {
|
2016-04-12 17:59:27 +02:00
|
|
|
throw new \InvalidArgumentException('$class cannot be empty');
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name);
|
2016-03-17 19:06:01 +01:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
if (in_array($fq_class_name, ['callable', 'iterable'], true)) {
|
2016-12-05 03:04:25 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_exists = ClassChecker::classExists($project_checker, $fq_class_name);
|
|
|
|
$interface_exists = InterfaceChecker::interfaceExists($project_checker, $fq_class_name);
|
2016-08-14 00:54:49 +02:00
|
|
|
|
|
|
|
if (!$class_exists && !$interface_exists) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new UndefinedClass(
|
2016-11-07 23:29:51 +01:00
|
|
|
'Class or interface ' . $fq_class_name . ' does not exist',
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
2016-07-24 23:06:36 +02:00
|
|
|
$suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-24 23:06:36 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if ($project_checker->collect_references && !$inferred) {
|
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
2017-02-28 00:24:20 +01:00
|
|
|
if ($class_storage->referencing_locations === null) {
|
|
|
|
$class_storage->referencing_locations = [];
|
|
|
|
}
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage->referencing_locations[$code_location->file_path][] = $code_location;
|
2017-02-27 05:09:18 +01:00
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if (($class_exists && !ClassChecker::hasCorrectCasing($project_checker, $fq_class_name)) ||
|
|
|
|
($interface_exists && !InterfaceChecker::hasCorrectCasing($project_checker, $fq_class_name))
|
2016-08-14 00:54:49 +02:00
|
|
|
) {
|
2017-07-29 21:05:06 +02:00
|
|
|
if (ClassLikeChecker::isUserDefined($project_checker, $fq_class_name)) {
|
2017-01-18 06:33:48 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidClass(
|
|
|
|
'Class or interface ' . $fq_class_name . ' has wrong casing',
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
2017-10-13 01:53:12 +02:00
|
|
|
// fall through here
|
2017-01-18 06:33:48 +01:00
|
|
|
}
|
2016-08-10 07:09:47 +02:00
|
|
|
}
|
2016-03-17 19:06:01 +01:00
|
|
|
}
|
|
|
|
|
2017-08-22 18:38:38 +02:00
|
|
|
FileReferenceProvider::addFileReferenceToClass(
|
|
|
|
$code_location->file_path,
|
|
|
|
strtolower($fq_class_name)
|
|
|
|
);
|
|
|
|
|
2016-07-24 23:06:36 +02:00
|
|
|
return true;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* Gets the fully-qualified class name from a Name object
|
2016-10-09 23:54:58 +02:00
|
|
|
*
|
2016-12-24 19:23:22 +01:00
|
|
|
* @param PhpParser\Node\Name $class_name
|
2017-01-07 20:35:07 +01:00
|
|
|
* @param StatementsSource $source
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-09 23:54:58 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public static function getFQCLNFromNameObject(PhpParser\Node\Name $class_name, Aliases $aliases)
|
|
|
|
{
|
2016-01-08 00:28:27 +01:00
|
|
|
if ($class_name instanceof PhpParser\Node\Name\FullyQualified) {
|
2016-02-18 21:05:13 +01:00
|
|
|
return implode('\\', $class_name->parts);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (in_array($class_name->parts[0], ['self', 'static', 'parent'], true)) {
|
|
|
|
return $class_name->parts[0];
|
|
|
|
}
|
|
|
|
|
2017-01-06 07:07:11 +01:00
|
|
|
return self::getFQCLNFromString(
|
|
|
|
implode('\\', $class_name->parts),
|
2017-07-25 22:11:02 +02:00
|
|
|
$aliases
|
2017-01-06 07:07:11 +01:00
|
|
|
);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
2016-11-05 22:53:30 +01:00
|
|
|
* @param string $class
|
2017-01-07 20:35:07 +01:00
|
|
|
* @param StatementsSource $source
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-12 07:38:29 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public static function getFQCLNFromString($class, Aliases $aliases)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-04-12 17:59:27 +02:00
|
|
|
if (empty($class)) {
|
|
|
|
throw new \InvalidArgumentException('$class cannot be empty');
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
if ($class[0] === '\\') {
|
2016-02-18 21:05:13 +01:00
|
|
|
return substr($class, 1);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$imported_namespaces = $aliases->uses;
|
2017-01-07 20:35:07 +01:00
|
|
|
|
2016-01-08 00:28:27 +01:00
|
|
|
if (strpos($class, '\\') !== false) {
|
|
|
|
$class_parts = explode('\\', $class);
|
|
|
|
$first_namespace = array_shift($class_parts);
|
|
|
|
|
2016-09-10 05:17:56 +02:00
|
|
|
if (isset($imported_namespaces[strtolower($first_namespace)])) {
|
|
|
|
return $imported_namespaces[strtolower($first_namespace)] . '\\' . implode('\\', $class_parts);
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-09-10 05:17:56 +02:00
|
|
|
} elseif (isset($imported_namespaces[strtolower($class)])) {
|
|
|
|
return $imported_namespaces[strtolower($class)];
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$namespace = $aliases->namespace;
|
2017-01-07 20:35:07 +01:00
|
|
|
|
2016-02-27 01:11:11 +01:00
|
|
|
return ($namespace ? $namespace . '\\' : '') . $class;
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-11-26 22:03:17 +01:00
|
|
|
* @return ?string
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getNamespace()
|
|
|
|
{
|
2017-01-07 20:35:07 +01:00
|
|
|
return $this->source->getNamespace();
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 00:51:48 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public function getAliasedClassesFlipped()
|
|
|
|
{
|
2016-11-13 17:24:46 +01:00
|
|
|
if ($this->source instanceof NamespaceChecker || $this->source instanceof FileChecker) {
|
|
|
|
return $this->source->getAliasedClassesFlipped();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
2016-11-13 00:51:48 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-08 01:16:51 +01:00
|
|
|
public function getFQCLN()
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-11-07 23:29:51 +01:00
|
|
|
return $this->fq_class_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-12-04 04:41:45 +01:00
|
|
|
* @return string|null
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function getClassName()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->class->name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2017-01-07 20:35:07 +01:00
|
|
|
public function getParentFQCLN()
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2017-01-07 20:35:07 +01:00
|
|
|
return $this->parent_fq_class_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-01-30 00:48:09 +01:00
|
|
|
public function hasCustomGet()
|
|
|
|
{
|
2016-08-14 00:54:49 +02:00
|
|
|
return $this->has_custom_get;
|
2016-01-30 00:48:09 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @param ReflectionClass $reflected_class
|
2017-01-09 05:58:06 +01:00
|
|
|
* @param ProjectChecker $project_checker
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 14:24:36 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static function registerReflectedClass(
|
|
|
|
$class_name,
|
2017-01-09 05:58:06 +01:00
|
|
|
ReflectionClass $reflected_class,
|
|
|
|
ProjectChecker $project_checker
|
2017-01-02 21:31:18 +01:00
|
|
|
) {
|
2017-01-02 01:50:29 +01:00
|
|
|
$class_name = $reflected_class->name;
|
|
|
|
|
|
|
|
if ($class_name === 'LibXMLError') {
|
|
|
|
$class_name = 'libXMLError';
|
|
|
|
}
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$class_name_lower = strtolower($class_name);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$storage_provider = $project_checker->classlike_storage_provider;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$storage_provider->get($class_name_lower);
|
|
|
|
|
2016-12-30 21:53:35 +01:00
|
|
|
return;
|
2017-07-29 21:05:06 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
// this is fine
|
2016-12-30 21:53:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 01:50:29 +01:00
|
|
|
$reflected_parent_class = $reflected_class->getParentClass();
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$storage = $storage_provider->create($class_name);
|
2017-01-20 06:10:10 +01:00
|
|
|
$storage->abstract = $reflected_class->isAbstract();
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-01-02 01:50:29 +01:00
|
|
|
if ($reflected_parent_class) {
|
|
|
|
$parent_class_name = $reflected_parent_class->getName();
|
2017-01-09 05:58:06 +01:00
|
|
|
self::registerReflectedClass($parent_class_name, $reflected_parent_class, $project_checker);
|
2016-10-27 22:05:27 +02:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$parent_storage = $storage_provider->get($parent_class_name);
|
2017-01-02 01:50:29 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
self::registerInheritedMethods($project_checker, $class_name, $parent_class_name);
|
|
|
|
self::registerInheritedProperties($project_checker, $class_name, $parent_class_name);
|
2017-01-02 01:50:29 +01:00
|
|
|
|
|
|
|
$storage->class_implements = $parent_storage->class_implements;
|
|
|
|
|
|
|
|
$storage->public_class_constants = $parent_storage->public_class_constants;
|
|
|
|
$storage->protected_class_constants = $parent_storage->protected_class_constants;
|
2017-01-15 01:34:10 +01:00
|
|
|
$storage->parent_classes = array_merge([strtolower($parent_class_name)], $parent_storage->parent_classes);
|
2017-01-02 01:50:29 +01:00
|
|
|
|
|
|
|
$storage->used_traits = $parent_storage->used_traits;
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-10-27 22:05:27 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
$class_properties = $reflected_class->getProperties();
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$public_mapped_properties = self::inPropertyMap($class_name)
|
|
|
|
? self::getPropertyMap()[strtolower($class_name)]
|
|
|
|
: [];
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
/** @var \ReflectionProperty $class_property */
|
|
|
|
foreach ($class_properties as $class_property) {
|
2017-01-02 01:09:17 +01:00
|
|
|
$property_name = $class_property->getName();
|
|
|
|
$storage->properties[$property_name] = new PropertyStorage();
|
|
|
|
|
|
|
|
$storage->properties[$property_name]->type = Type::getMixed();
|
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($class_property->isStatic()) {
|
2017-01-02 01:09:17 +01:00
|
|
|
$storage->properties[$property_name]->is_static = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($class_property->isPublic()) {
|
|
|
|
$storage->properties[$property_name]->visibility = self::VISIBILITY_PUBLIC;
|
|
|
|
} elseif ($class_property->isProtected()) {
|
|
|
|
$storage->properties[$property_name]->visibility = self::VISIBILITY_PROTECTED;
|
|
|
|
} elseif ($class_property->isPrivate()) {
|
|
|
|
$storage->properties[$property_name]->visibility = self::VISIBILITY_PRIVATE;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
$property_id = (string)$class_property->class . '::$' . $property_name;
|
|
|
|
|
|
|
|
$storage->declaring_property_ids[$property_name] = $property_id;
|
|
|
|
$storage->appearing_property_ids[$property_name] = $property_id;
|
2017-01-19 19:11:45 +01:00
|
|
|
|
|
|
|
if (!$class_property->isPrivate()) {
|
|
|
|
$storage->inheritable_property_ids[$property_name] = $property_id;
|
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
// have to do this separately as there can be new properties here
|
|
|
|
foreach ($public_mapped_properties as $property_name => $type) {
|
|
|
|
if (!isset($storage->properties[$property_name])) {
|
|
|
|
$storage->properties[$property_name] = new PropertyStorage();
|
|
|
|
$storage->properties[$property_name]->visibility = self::VISIBILITY_PUBLIC;
|
2016-10-27 22:05:27 +02:00
|
|
|
|
2017-01-02 01:50:29 +01:00
|
|
|
$property_id = $class_name . '::$' . $property_name;
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
$storage->declaring_property_ids[$property_name] = $property_id;
|
|
|
|
$storage->appearing_property_ids[$property_name] = $property_id;
|
2017-01-19 19:11:45 +01:00
|
|
|
$storage->inheritable_property_ids[$property_name] = $property_id;
|
2016-10-27 22:05:27 +02:00
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
$storage->properties[$property_name]->type = Type::parseString($type);
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-10-27 22:05:27 +02:00
|
|
|
|
2016-12-17 01:22:30 +01:00
|
|
|
/** @var array<string, int|string|float|null|array> */
|
2016-11-02 14:24:36 +01:00
|
|
|
$class_constants = $reflected_class->getConstants();
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
foreach ($class_constants as $name => $value) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->public_class_constants[$name] = self::getTypeFromValue($value);
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-08-14 18:06:53 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if ($reflected_class->isInterface()) {
|
2017-01-09 05:58:06 +01:00
|
|
|
$project_checker->addFullyQualifiedInterfaceName($class_name);
|
|
|
|
} elseif ($reflected_class->isTrait()) {
|
|
|
|
$project_checker->addFullyQualifiedTraitName($class_name);
|
|
|
|
} else {
|
|
|
|
$project_checker->addFullyQualifiedClassName($class_name);
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
2016-08-15 05:24:16 +02:00
|
|
|
|
2016-11-04 01:51:56 +01:00
|
|
|
$reflection_methods = $reflected_class->getMethods(
|
|
|
|
ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
|
|
|
|
);
|
2016-08-15 05:24:16 +02:00
|
|
|
|
2017-02-10 02:35:17 +01:00
|
|
|
if ($class_name_lower === 'generator') {
|
|
|
|
$storage->template_types = ['TKey' => 'mixed', 'TValue' => 'mixed'];
|
|
|
|
}
|
|
|
|
|
2016-12-30 18:41:14 +01:00
|
|
|
$interfaces = $reflected_class->getInterfaces();
|
|
|
|
|
|
|
|
/** @var \ReflectionClass $interface */
|
|
|
|
foreach ($interfaces as $interface) {
|
|
|
|
$interface_name = $interface->getName();
|
2017-01-09 05:58:06 +01:00
|
|
|
self::registerReflectedClass($interface_name, $interface, $project_checker);
|
2017-01-17 02:47:23 +01:00
|
|
|
|
|
|
|
if ($reflected_class->isInterface()) {
|
2017-07-25 22:11:02 +02:00
|
|
|
$storage->parent_interfaces[strtolower($interface_name)] = $interface_name;
|
2017-01-17 02:47:23 +01:00
|
|
|
} else {
|
|
|
|
$storage->class_implements[strtolower($interface_name)] = $interface_name;
|
|
|
|
}
|
2016-12-30 18:41:14 +01:00
|
|
|
}
|
|
|
|
|
2016-11-21 04:40:19 +01:00
|
|
|
/** @var \ReflectionMethod $reflection_method */
|
2016-11-02 14:24:36 +01:00
|
|
|
foreach ($reflection_methods as $reflection_method) {
|
2017-09-20 05:35:37 +02:00
|
|
|
$method_reflection_class = $reflection_method->getDeclaringClass();
|
|
|
|
$method_class_name = $method_reflection_class->getName();
|
|
|
|
|
|
|
|
self::registerReflectedClass($method_class_name, $method_reflection_class, $project_checker);
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
MethodChecker::extractReflectionMethodInfo($reflection_method, $project_checker);
|
2016-08-15 06:31:34 +02:00
|
|
|
|
2016-11-02 14:24:36 +01:00
|
|
|
if ($reflection_method->class !== $class_name) {
|
|
|
|
MethodChecker::setDeclaringMethodId(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2016-12-24 12:03:55 +01:00
|
|
|
$class_name . '::' . strtolower($reflection_method->name),
|
|
|
|
$reflection_method->class . '::' . strtolower($reflection_method->name)
|
2016-11-02 14:24:36 +01:00
|
|
|
);
|
2016-08-15 05:24:16 +02:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
MethodChecker::setAppearingMethodId(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-01-02 01:09:17 +01:00
|
|
|
$class_name . '::' . strtolower($reflection_method->name),
|
|
|
|
$reflection_method->class . '::' . strtolower($reflection_method->name)
|
|
|
|
);
|
2016-08-15 05:24:16 +02:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
continue;
|
2016-11-02 14:24:36 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-01-02 01:50:29 +01:00
|
|
|
* @param string $fq_class_name
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param string $parent_class
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
protected static function registerInheritedMethods(ProjectChecker $project_checker, $fq_class_name, $parent_class)
|
2016-08-15 06:31:34 +02:00
|
|
|
{
|
2017-07-29 21:05:06 +02:00
|
|
|
$parent_storage = $project_checker->classlike_storage_provider->get($parent_class);
|
|
|
|
$storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
2016-12-12 20:29:58 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
// register where they appear (can never be in a trait)
|
|
|
|
foreach ($parent_storage->appearing_method_ids as $method_name => $appearing_method_id) {
|
2017-01-02 01:50:29 +01:00
|
|
|
$implemented_method_id = $fq_class_name . '::' . $method_name;
|
2016-12-12 20:29:58 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$storage->appearing_method_ids[$method_name] = $appearing_method_id;
|
2016-12-12 20:29:58 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
// register where they're declared
|
2017-11-09 03:27:23 +01:00
|
|
|
foreach ($parent_storage->inheritable_method_ids as $method_name => $declaring_method_id) {
|
2017-01-02 01:50:29 +01:00
|
|
|
$implemented_method_id = $fq_class_name . '::' . $method_name;
|
2016-08-15 06:31:34 +02:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$storage->declaring_method_ids[$method_name] = $declaring_method_id;
|
2017-11-09 03:27:23 +01:00
|
|
|
$storage->inheritable_method_ids[$method_name] = $declaring_method_id;
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
MethodChecker::setOverriddenMethodId($project_checker, $implemented_method_id, $declaring_method_id);
|
2016-08-15 06:31:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
2017-01-02 01:50:29 +01:00
|
|
|
* @param string $fq_class_name
|
2017-01-02 01:09:17 +01:00
|
|
|
* @param string $parent_class
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-02 01:09:17 +01:00
|
|
|
* @return void
|
2016-10-12 07:38:29 +02:00
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
protected static function registerInheritedProperties(
|
|
|
|
ProjectChecker $project_checker,
|
|
|
|
$fq_class_name,
|
|
|
|
$parent_class
|
|
|
|
) {
|
|
|
|
$parent_storage = $project_checker->classlike_storage_provider->get($parent_class);
|
|
|
|
$storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
// register where they appear (can never be in a trait)
|
|
|
|
foreach ($parent_storage->appearing_property_ids as $property_name => $appearing_property_id) {
|
2017-06-30 07:24:45 +02:00
|
|
|
if (!$parent_storage->is_trait
|
|
|
|
&& isset($parent_storage->properties[$property_name])
|
|
|
|
&& $parent_storage->properties[$property_name]->visibility === self::VISIBILITY_PRIVATE
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$storage->appearing_property_ids[$property_name] = $appearing_property_id;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
// register where they're declared
|
|
|
|
foreach ($parent_storage->declaring_property_ids as $property_name => $declaring_property_id) {
|
2017-06-30 07:24:45 +02:00
|
|
|
if (!$parent_storage->is_trait
|
|
|
|
&& isset($parent_storage->properties[$property_name])
|
|
|
|
&& $parent_storage->properties[$property_name]->visibility === self::VISIBILITY_PRIVATE
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$storage->declaring_property_ids[$property_name] = $declaring_property_id;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2017-01-19 19:11:45 +01:00
|
|
|
|
|
|
|
// register where they're declared
|
|
|
|
foreach ($parent_storage->inheritable_property_ids as $property_name => $inheritable_property_id) {
|
2017-06-30 07:24:45 +02:00
|
|
|
if (!$parent_storage->is_trait
|
|
|
|
&& isset($parent_storage->properties[$property_name])
|
|
|
|
&& $parent_storage->properties[$property_name]->visibility === self::VISIBILITY_PRIVATE
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-19 19:11:45 +01:00
|
|
|
$storage->inheritable_property_ids[$property_name] = $inheritable_property_id;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @param mixed $visibility
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-02 01:09:17 +01:00
|
|
|
* @return array<string, PropertyStorage>
|
2016-10-12 07:38:29 +02:00
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function getPropertiesForClass(ProjectChecker $project_checker, $class_name, $visibility)
|
2016-08-07 02:27:13 +02:00
|
|
|
{
|
2017-07-29 21:05:06 +02:00
|
|
|
$storage = $project_checker->classlike_storage_provider->get($class_name);
|
2016-12-30 18:41:14 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
$properties = [];
|
2016-11-02 14:24:36 +01:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
foreach ($storage->properties as $property_name => $property) {
|
|
|
|
if (!$property->is_static) {
|
|
|
|
if ($visibility === ReflectionProperty::IS_PRIVATE ||
|
|
|
|
$property->visibility === ClassLikeChecker::VISIBILITY_PUBLIC ||
|
|
|
|
($property->visibility === ClassLikeChecker::VISIBILITY_PROTECTED &&
|
|
|
|
$visibility === ReflectionProperty::IS_PROTECTED)
|
|
|
|
) {
|
|
|
|
$properties[$property_name] = $property;
|
|
|
|
}
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2016-07-23 16:58:53 +02:00
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
return $properties;
|
2016-01-30 00:48:09 +01:00
|
|
|
}
|
2016-04-12 01:13:50 +02:00
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
/**
|
|
|
|
* Gets the Psalm type from a particular value
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-21 03:49:06 +01:00
|
|
|
* @return Type\Union
|
|
|
|
*/
|
|
|
|
public static function getTypeFromValue($value)
|
|
|
|
{
|
|
|
|
switch (gettype($value)) {
|
|
|
|
case 'boolean':
|
|
|
|
return Type::getBool();
|
|
|
|
|
|
|
|
case 'integer':
|
|
|
|
return Type::getInt();
|
|
|
|
|
|
|
|
case 'double':
|
|
|
|
return Type::getFloat();
|
|
|
|
|
|
|
|
case 'string':
|
|
|
|
return Type::getString();
|
|
|
|
|
|
|
|
case 'array':
|
|
|
|
return Type::getArray();
|
|
|
|
|
|
|
|
case 'NULL':
|
|
|
|
return Type::getNull();
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:38:29 +02:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @param mixed $visibility
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-12 07:38:29 +02:00
|
|
|
* @return array<string,Type\Union>
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function getConstantsForClass(ProjectChecker $project_checker, $class_name, $visibility)
|
2016-08-13 20:20:46 +02:00
|
|
|
{
|
2017-01-09 05:58:06 +01:00
|
|
|
$class_name = strtolower($class_name);
|
|
|
|
|
|
|
|
$class_name = strtolower($class_name);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$storage = $project_checker->classlike_storage_provider->get($class_name);
|
2016-12-30 18:41:14 +01:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
if ($visibility === ReflectionProperty::IS_PUBLIC) {
|
2016-12-30 18:41:14 +01:00
|
|
|
return $storage->public_class_constants;
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 07:44:33 +01:00
|
|
|
if ($visibility === ReflectionProperty::IS_PROTECTED) {
|
|
|
|
return array_merge(
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->public_class_constants,
|
|
|
|
$storage->protected_class_constants
|
2016-12-04 07:44:33 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($visibility === ReflectionProperty::IS_PRIVATE) {
|
|
|
|
return array_merge(
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->public_class_constants,
|
|
|
|
$storage->protected_class_constants,
|
|
|
|
$storage->private_class_constants
|
2016-12-04 07:44:33 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException('Must specify $visibility');
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
|
|
|
* @param string $const_name
|
|
|
|
* @param Type\Union $type
|
2016-12-04 07:44:33 +01:00
|
|
|
* @param int $visibility
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function setConstantType(
|
|
|
|
ProjectChecker $project_checker,
|
|
|
|
$class_name,
|
|
|
|
$const_name,
|
|
|
|
Type\Union $type,
|
|
|
|
$visibility
|
|
|
|
) {
|
|
|
|
$storage = $project_checker->classlike_storage_provider->get($class_name);
|
2016-12-30 18:41:14 +01:00
|
|
|
|
2016-12-04 07:44:33 +01:00
|
|
|
if ($visibility === ReflectionProperty::IS_PUBLIC) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->public_class_constants[$const_name] = $type;
|
2016-12-04 07:44:33 +01:00
|
|
|
} elseif ($visibility === ReflectionProperty::IS_PROTECTED) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->protected_class_constants[$const_name] = $type;
|
2016-12-04 07:44:33 +01:00
|
|
|
} elseif ($visibility === ReflectionProperty::IS_PRIVATE) {
|
2016-12-30 18:41:14 +01:00
|
|
|
$storage->private_class_constants[$const_name] = $type;
|
2016-12-04 07:44:33 +01:00
|
|
|
}
|
2016-08-13 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
/**
|
|
|
|
* Whether or not a given property exists
|
|
|
|
*
|
|
|
|
* @param string $property_id
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-02 01:09:17 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function propertyExists(ProjectChecker $project_checker, $property_id)
|
2017-01-02 01:09:17 +01:00
|
|
|
{
|
|
|
|
// remove trailing backslash if it exists
|
|
|
|
$property_id = preg_replace('/^\\\\/', '', $property_id);
|
|
|
|
|
|
|
|
list($fq_class_name, $property_name) = explode('::$', $property_id);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
if (isset($class_storage->declaring_property_ids[$property_name])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $property_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
|
|
|
*
|
2017-01-02 01:09:17 +01:00
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function checkPropertyVisibility(
|
|
|
|
$property_id,
|
|
|
|
$calling_context,
|
|
|
|
StatementsSource $source,
|
|
|
|
CodeLocation $code_location,
|
|
|
|
array $suppressed_issues
|
|
|
|
) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $source->getFileChecker()->project_checker;
|
|
|
|
|
|
|
|
$declaring_property_class = self::getDeclaringClassForProperty($project_checker, $property_id);
|
|
|
|
$appearing_property_class = self::getAppearingClassForProperty($project_checker, $property_id);
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
if (!$declaring_property_class || !$appearing_property_class) {
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
'Appearing/Declaring classes are not defined for ' . $property_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-11 01:10:13 +01:00
|
|
|
list(, $property_name) = explode('::$', (string)$property_id);
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
// if the calling class is the same, we know the property exists, so it must be visible
|
|
|
|
if ($appearing_property_class === $calling_context) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($source->getSource() instanceof TraitChecker && $declaring_property_class === $source->getFQCLN()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($declaring_property_class);
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-11-24 18:10:30 +01:00
|
|
|
if (!isset($class_storage->properties[$property_name])) {
|
2017-01-02 01:09:17 +01:00
|
|
|
throw new \UnexpectedValueException('$storage should not be null for ' . $property_id);
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:10:30 +01:00
|
|
|
$storage = $class_storage->properties[$property_name];
|
|
|
|
|
2017-01-02 01:09:17 +01:00
|
|
|
switch ($storage->visibility) {
|
|
|
|
case self::VISIBILITY_PUBLIC:
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case self::VISIBILITY_PRIVATE:
|
|
|
|
if (!$calling_context || $appearing_property_class !== $calling_context) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access private property ' . $property_id . ' from context ' . $calling_context,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case self::VISIBILITY_PROTECTED:
|
|
|
|
if ($appearing_property_class === $calling_context) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$calling_context) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access protected property ' . $property_id,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if (ClassChecker::classExtends($project_checker, $appearing_property_class, $calling_context)) {
|
2017-01-02 01:09:17 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
if (!ClassChecker::classExtends($project_checker, $calling_context, $appearing_property_class)) {
|
2017-01-02 01:09:17 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InaccessibleProperty(
|
|
|
|
'Cannot access protected property ' . $property_id . ' from context ' . $calling_context,
|
|
|
|
$code_location
|
|
|
|
),
|
|
|
|
$suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $property_id
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-02 01:09:17 +01:00
|
|
|
* @return string|null
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function getDeclaringClassForProperty(ProjectChecker $project_checker, $property_id)
|
2017-01-02 01:09:17 +01:00
|
|
|
{
|
|
|
|
list($fq_class_name, $property_name) = explode('::$', $property_id);
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$fq_class_name = strtolower($fq_class_name);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
|
|
if (isset($class_storage->declaring_property_ids[$property_name])) {
|
|
|
|
$declaring_property_id = $class_storage->declaring_property_ids[$property_name];
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
return explode('::$', $declaring_property_id)[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the class this property appears in (vs is declared in, which could give a trait)
|
|
|
|
*
|
|
|
|
* @param string $property_id
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-02 01:09:17 +01:00
|
|
|
* @return string|null
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function getAppearingClassForProperty(ProjectChecker $project_checker, $property_id)
|
2017-01-02 01:09:17 +01:00
|
|
|
{
|
|
|
|
list($fq_class_name, $property_name) = explode('::$', $property_id);
|
|
|
|
|
2017-01-09 05:58:06 +01:00
|
|
|
$fq_class_name = strtolower($fq_class_name);
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
|
|
|
|
|
|
|
|
if (isset($class_storage->appearing_property_ids[$property_name])) {
|
|
|
|
$appearing_property_id = $class_storage->appearing_property_ids[$property_name];
|
2017-01-02 01:09:17 +01:00
|
|
|
|
|
|
|
return explode('::$', $appearing_property_id)[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $method_name
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-24 12:03:55 +01:00
|
|
|
* @return string
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-08-15 20:20:06 +02:00
|
|
|
protected function getMappedMethodName($method_name)
|
|
|
|
{
|
|
|
|
return $method_name;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param string $file_path
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-17 00:56:23 +01:00
|
|
|
* @return array<string>
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function getClassesForFile(ProjectChecker $project_checker, $file_path)
|
2016-10-05 01:23:38 +02:00
|
|
|
{
|
2017-07-29 21:05:06 +02:00
|
|
|
try {
|
|
|
|
return $project_checker->file_storage_provider->get($file_path)->classes_in_file;
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-10-05 01:23:38 +02:00
|
|
|
}
|
|
|
|
|
2016-10-30 16:14:36 +01:00
|
|
|
/**
|
2016-11-07 23:29:51 +01:00
|
|
|
* @param string $fq_class_name
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-05-25 04:07:49 +02:00
|
|
|
* @return bool
|
2016-10-30 16:14:36 +01:00
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function isUserDefined(ProjectChecker $project_checker, $fq_class_name)
|
2016-10-25 17:40:09 +02:00
|
|
|
{
|
2017-07-29 21:05:06 +02:00
|
|
|
return $project_checker->classlike_storage_provider->get($fq_class_name)->user_defined;
|
2016-10-25 17:40:09 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 22:05:27 +02:00
|
|
|
/**
|
|
|
|
* Gets the method/function call map
|
|
|
|
*
|
2016-11-05 22:53:30 +01:00
|
|
|
* @return array<string, array<string, string>>
|
2016-11-05 23:46:17 +01:00
|
|
|
* @psalm-suppress MixedInferredReturnType as the use of require buggers things up
|
2017-01-09 05:58:06 +01:00
|
|
|
* @psalm-suppress MixedAssignment
|
2016-10-27 22:05:27 +02:00
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public static function getPropertyMap()
|
2016-10-27 22:05:27 +02:00
|
|
|
{
|
|
|
|
if (self::$property_map !== null) {
|
|
|
|
return self::$property_map;
|
|
|
|
}
|
|
|
|
|
2017-01-17 02:00:51 +01:00
|
|
|
/** @var array<string, array<string, string>> */
|
2017-05-25 04:07:49 +02:00
|
|
|
$property_map = require_once(__DIR__ . '/../PropertyMap.php');
|
2016-10-27 22:05:27 +02:00
|
|
|
|
|
|
|
self::$property_map = [];
|
|
|
|
|
|
|
|
foreach ($property_map as $key => $value) {
|
|
|
|
$cased_key = strtolower($key);
|
|
|
|
self::$property_map[$cased_key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$property_map;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $class_name
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-27 22:05:27 +02:00
|
|
|
public static function inPropertyMap($class_name)
|
|
|
|
{
|
|
|
|
return isset(self::getPropertyMap()[strtolower($class_name)]);
|
|
|
|
}
|
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
/**
|
|
|
|
* @return FileChecker
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public function getFileChecker()
|
|
|
|
{
|
|
|
|
return $this->file_checker;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-07-25 00:02:03 +02:00
|
|
|
public static function clearCache()
|
|
|
|
{
|
2016-12-30 18:41:14 +01:00
|
|
|
self::$file_classes = [];
|
2016-10-21 00:12:13 +02:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
self::$trait_checkers = [];
|
|
|
|
|
2016-08-14 00:54:49 +02:00
|
|
|
self::$class_checkers = [];
|
2016-07-25 00:02:03 +02:00
|
|
|
}
|
2016-01-08 00:28:27 +01:00
|
|
|
}
|